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,540 points
1,230 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,170 points

Related Questions In AWS

0 votes
1 answer

How to upload a file from S3 in Lambda?

Yes there is an option where you ...READ MORE

answered Jan 24, 2019 in AWS by Priyaj
• 58,020 points
5,173 views
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,670 points
2,573 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
4,669 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
• 58,020 points
13,001 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,170 points

edited Aug 8, 2019 by Archana 5,139 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
• 58,020 points
10,150 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,170 points
7,522 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,170 points
3,292 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,170 points
947 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP