如何在Lambda中将对象上传到S3?

16

无法在Lambda中将对象上传到S3。 在本地一切运行正常。 日志中没有错误信息显示出了什么问题...

以下是代码:

console.log('Loading function');
var AWS = require('aws-sdk');
var s3 = new AWS.S3();

exports.handler = function(event, context) {
    //console.log(JSON.stringify(event, null, 2));
    var s3 = new AWS.S3();
    var param = {Bucket: 'flow-logs', Key: 'test-lambda-x', Body: 'me me me'};
    console.log("s3");
    s3.upload(param, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else console.log(data);           // successful response
    });
    console.log('done');
    context.done();
};

程序成功运行且无错误,但 s3.upload 中的回调函数似乎没有被调用。桶中没有创建对象。

通过授予完全访问权限以及本地测试来验证 IAM 角色权限不是问题。

输出

START RequestId: d4847fdb-160c-11e5-8a8c-b555b123e14d
2015-06-18T22:53:29.750Z    d4847fdb-160c-11e5-8a8c-b555b123e14d    s3
2015-06-18T22:53:30.271Z    d4847fdb-160c-11e5-8a8c-b555b123e14d    done
END RequestId: d4847fdb-160c-11e5-8a8c-b555b123e14d

1
为什么 S3 对象会被创建两次?(本地和全局) - Katharine Osborne
我已经有一个完整的工作版本,解释在这里:http://stackoverflow.com/questions/33631229/how-to-set-open-download-permissions-on-a-file-created-in-s3-with-amazon-lambda/33632736 - Katharine Osborne
1个回答

46

我怀疑你在s3.upload()有机会返回前就调用了context.done()函数。如果将context.done()移动到上传响应代码块中,它应该可以正常工作。

var AWS = require('aws-sdk');
var s3 = new AWS.S3();

exports.handler = function(event, context) {
    //console.log(JSON.stringify(event, null, 2));
    var s3 = new AWS.S3();
    var param = {Bucket: 'flow-logs', Key: 'test-lambda-x', Body: 'me me me'};
    console.log("s3");
    s3.upload(param, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else console.log(data);           // successful response

        console.log('actually done!');
        context.done();
    });

    console.log('done?');
    //context.done();
};

1
@KatharineOsborne 如果它起作用了,请接受答案。这有助于社区中的其他人。 - Parthapratim Neog
@ParthapratimNeog 我可以翻译,但我没有提出这个问题;-) - Katharine Osborne
@KatharineOsborne 我明白了,是我不好。 :) - Parthapratim Neog
挽救了我的生命 :) - Bikesh M

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接