How to upload an object into S3 in Lambda?

前端 未结 1 1108
清歌不尽
清歌不尽 2021-02-07 09:33

Can\'t seem to upload an object into S3 in Lambda. Everything works fine locally. No errors in logs that would show what\'s going wrong...

Code below:

co         


        
相关标签:
1条回答
  • 2021-02-07 10:09

    I suspect you are calling the context.done() function before s3.upload() has a chance to return. If you move context.done() into the upload response code block, it should work.

    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();
    };
    
    0 讨论(0)
提交回复
热议问题