Home > Mobile >  How to read CSV data from S3 using Node.js AWS Lambda function
How to read CSV data from S3 using Node.js AWS Lambda function

Time:01-08

I have a Node.js AWS Lambda function and I am trying to read records from a CSV file in S3 and print its contents.

Below is my code to achieve the same however I am getting Null as output.

Code:

const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const csv = require('csv-parser');
const bucket = 'awslambdabuckets';
const objectkey = 'record.csv';
const params = { Bucket: bucket, Key: objectkey };
const results = [];

exports.handler = async function (event, ctx, callback) {
    try {
        const file = s3.getObject(params).createReadStream();

        file
            .pipe(csv())
            .on('data', function (data) {
                results.push;
            })
            .on('end', () => {
                console.log(results);
                callback(null, results);
            });
    } catch (err) {
        console.log(err);
        callback(Error(err));
    }

};

Output: Lambda Output

Can someone help me point out what's the problem and how to fix it.

CodePudding user response:

You are not pushing the data to the result, see and make changes as below

exports.handler = async function (event, ctx, callback) {
  try {
      const file = s3.getObject(params).createReadStream();

      file
          .pipe(csv())
          .on('data', function (data) {
              results.push(data);
          })
          .on('end', () => {
              console.log(results);
              callback(null, results);
          });
  } catch (err) {
      console.log(err);
      callback(Error(err));
  }
};

CodePudding user response:

You are not pushing data to the array:

const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const csv = require('csv-parser');
const bucket = 'awslambdabuckets';
const objectkey = 'record.csv';
const params = { Bucket: bucket, Key: objectkey };
const results = [];

exports.handler = function (event, ctx, callback) {
    try {
        const file = s3.getObject(params).createReadStream();

        file
            .pipe(csv())
            .on('data', function (data) {
                results.push(data); // --> here
            })
            .on('end', () => {
                console.log(results);
                callback(null, results);
            });
    } catch (err) {
        console.log(err);
        callback(Error(err));
    }

};
  •  Tags:  
  • Related