无法从AWS Lambda中的csv文件读取数据

时间:2019-10-23 08:43:25

标签: node.js amazon-s3 aws-lambda

我正在尝试使用NodeJS从Lambda的S3中读取csv文件。

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

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

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

我得到的结果是null,而不是 csv数据

1 个答案:

答案 0 :(得分:0)

您在代码中的任何时候都没有返回数据。完成后,您需要致电.succeed()

const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const csv = require('csv-parser');
const bucket = '';
const objectkey = '';
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(data)
            })
            .on('end', () => {
                console.log(results);
                callback(null, results);
            })
    } catch (err) {
        console.log(err);
        callback(Error(err))
    }
}