How to upload an object into Amazon S3 in Lambda?

0 votes

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:

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 without error, but the callback in s3.upload doesn't seem to be called. No object in the bucket is created.

Verified IAM role permissions weren't a problem by granting full access, as well as testing out locally.

Sep 27, 2018 in AWS by datageek
• 2,440 points
53 views

1 answer to this question.

0 votes

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();
};
answered Sep 27, 2018 by Archana
• 4,090 points

Related Questions In AWS

0 votes
1 answer
0 votes
1 answer

Trying to upload data stream in Amazon S3

Try adding the ContentType & ACL in ...READ MORE

answered Jun 28, 2018 in AWS by Cloud gunner
• 4,260 points
328 views
0 votes
1 answer

How do I write an S3 Object to a file?

While IOUtils.copy() and IOUtils.copyLarge() are great, I would prefer the old ...READ MORE

answered Jul 13, 2018 in AWS by Hammer
• 360 points
123 views
+2 votes
1 answer

How to combine multiple S3 objects in the target S3 object w/o leaving S3

"However sometimes our raw objects are not ...READ MORE

answered Aug 28, 2018 in AWS by Priyaj
• 56,520 points
1,701 views
0 votes
1 answer

Upload an image from Android to Amazon S3

Take a look at the Amazon S3 ...READ MORE

answered Sep 21, 2018 in AWS by Archana
• 4,090 points

edited Aug 8 by Archana 622 views
0 votes
1 answer

how to access AWS S3 from Lambda in VPC

With boto3, the S3 urls are virtual by default, ...READ MORE

answered Sep 28, 2018 in AWS by Priyaj
• 56,520 points
1,617 views
0 votes
1 answer

How to list all the objects in Amazon S3?

As stated already, Amazon S3 indeed requires ...READ MORE

answered Oct 5, 2018 in AWS by Archana
• 4,090 points
314 views
0 votes
1 answer

How to make multiple files in Amazon S3 public?

I had to change several hundred thousand ...READ MORE

answered Oct 17, 2018 in AWS by Archana
• 4,090 points
62 views
0 votes
1 answer

How to upload a file to Amazon S3 without passing it my server?

This article pretty much explains the entire ...READ MORE

answered Aug 14, 2018 in AWS by Archana
• 4,090 points
73 views
0 votes
1 answer

How to upload a lib for Tomcat in Amazon EC2?

You need to set the proper privileges ...READ MORE

answered Aug 20, 2018 in AWS by Archana
• 4,090 points
43 views