简体   繁体   中英

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

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: 拉姆达输出

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

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));
  }
};

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));
    }

};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM