下载S3文件时Node.js Lambda函数不起作用

时间:2015-07-15 21:57:15

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

我尝试按照AWS Lambda示例下载S3文件并处理结果,但我在结果中不断收到以下错误消息:

Process exited before completing request

我的代码如下:

'use strict';

var async = require('async');
var AWS = require('aws-sdk');

AWS.config.region = 'us-west-2';
var s3 = new AWS.S3();

console.log('Loading event');

exports.handler = function(event, context) {
    console.log('Getting values from event.');
    var tenant = event.tenant;
    var key = event.key

    async.waterfall([
        function download(next) {
            console.log('Creating request.');
            var keyFile = "secret";
            var sourceBucket = "mybucket";
            console.log('Retrieving secret key using bucket ' + sourceBucket + ' and keyfile ' + keyFile);
            var params = {Bucket: sourceBucket, Key: key};
            s3.getObject(params, next);
        },
        function returnFile(response, next) {
            console.log('Executing returnFile.');
            // Do return stuff here.
            console.log(response);
        }, function (err) {
            if (err) {
                console.error('Error: ' + err);
            } else {
                console.log('Successfully completed.');
            }
            context.done();
        }
    ]);
}

返回的日志文件:

START RequestId: 15327019-2b3b-11e5-a94b-e72d67a1fe04
2015-07-15T21:47:26.627Z    15327019-2b3b-11e5-a94b-e72d67a1fe04    Getting values from event.
2015-07-15T21:47:26.628Z    15327019-2b3b-11e5-a94b-e72d67a1fe04    Creating request.
2015-07-15T21:47:26.628Z    15327019-2b3b-11e5-a94b-e72d67a1fe04    Retrieving secret key using bucket mybucket and keyfile secret
END RequestId: 15327019-2b3b-11e5-a94b-e72d67a1fe04
REPORT RequestId: 15327019-2b3b-11e5-a94b-e72d67a1fe04  Duration: 921.08 ms Billed Duration: 1000 ms    Memory Size: 128 MB Max Memory Used: 14 MB  
Process exited before completing request

我对如何进一步解决此问题感到有点困惑。任何帮助或指示将不胜感激!

修改

好的,谢谢你的回复。我错误地使用了异步瀑布方法。我没有正确调用回调,我没有正确指定瀑布方法的回调。如果有人遇到类似的问题,下面是工作代码:

'use strict';

var waterfall = require('async-waterfall');
var AWS = require('aws-sdk');

AWS.config.region = 'us-west-2';
var s3 = new AWS.S3();

var keyFile = "secret";
var sourceBucket = "mybucket";

exports.handler = function(event, context) {
    console.log('Loading event');
    console.log('Getting values from event.');
    var fileType = event.fileType;
    var key = event.key;
    console.log('fileType: "' + fileType + '", key: "' + key + '"');

    waterfall([
        function downloadSecret(next) {
            console.log('Retrieving secret key using bucket "' + sourceBucket + '" and keyfile "' + keyFile + '"');
            var params = {Bucket: sourceBucket, Key: keyFile};
            s3.getObject(params, next);
        },
        function extractKey(response, next) {
            console.log('Executing checkKey.');
            var secretKey = response.Body.toString('utf-8').trim();

            console.log('key: "' + key + '", secretKey: "' + secretKey + '"');
            if (key!=secretKey) {
                context.fail("Invalid key specified.");
            } else {
                next();
            }
        },
        function getFile(next) {
            console.log('Executing getFile.');
            var file;
            if (fileType == "1") {
                file = "file1";
            } else {
                file = "file2";
            }
            console.log('file: "' + file + '"');
            var params = {Bucket: sourceBucket, Key: file};
            s3.getObject(params, next);
        },
        function returnFile(response) {
            console.log('Executing returnFile.');
            var output = JSON.stringify(response);
            console.log(output);
            context.succeed(response.Body.toString('utf-8').trim());
        }
    ]);
}

1 个答案:

答案 0 :(得分:0)

我很确定您错误地使用了async库。 returnFile函数回调参数似乎不正确。

此外,请确保您的Lambda具有访问通过其IAM Policy授予的存储分区的适当权限。然后,您无需向AWS SDK提供访问凭据,它会自动提取它们。