我正在尝试使用AWS Lambda中的csvtojson库从s3存储桶中读取CSV,但无法正常工作。在本地,我的代码有效。但是,当我将其上传到Lambda时,它不会返回任何内容。 Lambda控制台中没有错误,因此我很难调试。我的代码如下。

const AWS = require('aws-sdk');
const csvtojson = require('csvtojson');

const s3 = new AWS.S3();

const params = {
   Bucket: bucketName,
   Key: pathToFile
};

const stream = s3.getObject(params).createReadStream();

csvtojson()
   .fromStream(stream)
   .then((json) => {
     console.log('Locally, this returns the CSV as JSON. On Lambda, it does not.');
   });


csvtojson是否由于某种原因在Lambda上不起作用?我应该使用其他方法来解析CSV吗?谢谢!

最佳答案

您的lambda将在Promise完成之前完成。以此替换最后一部分:

const json = await csvtojson().fromStream(stream);
console.log('Locally, this returns the CSV as JSON. On Lambda, it does not.');

关于node.js - 如何解析S3存储桶中的CSV以在JavaScript AWS Lambda函数中使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58065640/

10-16 14:17