我使用AWS服务从上传到S3桶上的CSV文件中读取内容,并将其转换为JSON格式。我从csv到json阶段被堵住了。
const AWS = require('aws-sdk');
const csvtojson = require('csvtojson');
const S3 = new AWS.S3();
exports.handler = async (event,content,callback) => {
console.log(' inside trigger ')
const src_bkt = event.Records[0].s3.bucket.name;
const src_key = event.Records[0].s3.object.key;
try {
const params = {Bucket: src_bkt, Key:src_key}
// get csv file and create stream
const stream = S3.getObject(params).createReadStream();
// convert csv file (stream) to JSON format data
const json = await csvtojson().fromStream(stream);
console.log(json);
}
catch (err) {
console.log(JSON.stringify(err))
return {
statusCode: err.statusCode || 400,
body: err.message || JSON.stringify(err.message)
}
}
};错误消息: "errorType":"Runtime.ImportModuleError","errorMessage":“错误:找不到模块'csvtojson'",
我试图转换lambda上的节点模块来检查运气。请分享任何建议。
发布于 2020-01-04 12:42:39
您需要将lambda打包并部署为zip文件。该文件应该包含带有处理程序函数的javascript文件以及所有依赖项的node_modules目录。
压缩文件结构。
handler.js
节点模块/
./
如何设置项目
node_modules
my-project初始化一个项目文件夹,它将为您创建一个package.json文件,使用npm install csv2json --save在handler.js
node_modules目录创建了H 212H 113 zip my-project目录,这样您就有了my-project.zip,这个目录应该直接包含handler.js和
参考资料:
https://aws.amazon.com/premiumsupport/knowledge-center/lambda-deployment-package-nodejs/
希望这能有所帮助。
https://stackoverflow.com/questions/59590490
复制相似问题