简体   繁体   English

如何使用 Node.js AWS Lambda ZC1C425268E68385D1AB5074C17A94 从 S3 读取 CSV 数据

[英]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.我有一个 Node.js AWS Lambda function 并且我正在尝试从 ZCC8D68C551C4A9A6D5313E 中的 ZCC8D68C551C4A9A6D5313E 文件中读取记录并打印其内容。

Below is my code to achieve the same however I am getting Null as output.下面是我实现相同的代码,但是我得到 Null 为 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: 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));
    }

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Node.js 将文件从 AWS Lambda 中的 /tmp 文件夹上传到 S3 - How to upload a file to S3 from the /tmp folder in AWS Lambda using Node.js Node.js Lambda函数中的AWS S3 ListObjects - AWS S3 ListObjects in Node.js Lambda Function 从AWS Lambda Node.JS流式传输并压缩到S3 - Stream and zip to S3 from AWS Lambda Node.JS 如何从 node.js 中的 s3 获取 HEAD 对象? (在 aws lambda 中运行) - How do you get the HEAD object from an s3 in node.js? (running in an aws lambda) 使用Lambda函数从S3存储桶中检索数据以链接到node.js中的alexa技能 - Retrieve data from S3 bucket with Lambda function link to an alexa skill in node.js 如何将 Zip 文件从 s3 存储桶下载到 node.js lambda 函数中的本地目录 - How to download Zip File from an s3 bucket to a local directory within a node.js lambda function 使用Node JS从AWS lambda函数上载S3上的映像 - Uploading an image on S3 from AWS lambda function using Node JS 使用node.js将JSON从AWS Lambda保存到AWS S3 - Save a JSON from AWS Lambda to AWS S3 with node.js 如何在 node.js 中从 AWS Lambda function 制作 mongodump? - How to make mongodump from AWS Lambda function in node.js? AWS Lambda使用node.js将文件上传到S3时需要花费时间 - AWS Lambda Consumes time when uploading files to S3 using node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM