简体   繁体   English

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

[英]How to upload an object into S3 in Lambda?

Can't seem to upload an object into S3 in Lambda. 似乎无法将对象上传到Lambda中的S3。 Everything works fine locally. 一切都在当地很好。 No errors in logs that would show what's going wrong... 日志中没有错误可以显示出错的地方......

Code below: 代码如下:

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();
};

Runs successfully w/o error, but the callback in s3.upload doesn't seem to be called. 运行成功没有错误,但似乎没有调用s3.upload中的回调。 No object in bucket is created. 桶中没有对象被创建。

Verified IAM role permissions weren't a problem by granting full access, as well as testing out locally. 通过授予完全访问权限以及在本地测试,已验证的IAM角色权限不是问题。

Output 产量

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

I suspect you are calling the context.done() function before s3.upload() has a chance to return. 我怀疑你在s3.upload()有机会返回之前调用了context.done()函数。 If you move context.done() into the upload response code block, it should work. 如果将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();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM