NodeJs, AWS, From a lambda how to read a file contents of S3 bucket, using AWS SDK V3

1.8k Views Asked by At

Using the AWS SDK version 3, how to read a file of S3 bucket from a nodejs based lambda? I'm creating this for my self reference in the future.

I use middy middleware in my lambdas. Hence, used the same template. Please note that it's optional. Similarly csvtojson is also optional in the scope of this discussion.

1

There are 1 best solutions below

0
On

There are a couple of solutions, the solution that I'm showing is using node-fetch npm.

import Log from '@dazn/lambda-powertools-logger'; // it will be modified after integrating with lambda-powertools
import { S3Event } from 'aws-lambda';
import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
import middy from '@middy/core';
import csv from 'csvtojson';
import { Response } from 'node-fetch';
import internal from 'stream';
import { incomingEventLogger, onErrorHandler } from '../helpers/middleware';

const { BUCKET: Bucket, REGION } = process.env;
const client = new S3Client( { region: REGION } );

export const index = async (event: S3Event) => {
    const s3GetObjectInput = {
        Bucket,
        Key: 'PrivateCSVFiles/test.csv',  // hardcoded for the sake of example
    };
    const getObjectCommand = new GetObjectCommand(s3GetObjectInput);
    const { Body } = await client.send(getObjectCommand);
    const csvFileContentResponse = new Response(Body as internal.Readable);
    const csvFileContent = await csvFileContentResponse.text();
    Log.debug('csvFileContent', { csvFileContent });
    const jsonObj = await csv({ delimiter: ','})
        .fromString(csvFileContent);
    Log.debug('jsonObj', { jsonObj });
};

export const handler = middy(index);

handler.before(incomingEventLogger);
handler.onError(onErrorHandler);