Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I have a Node 4.3 Lambda function in AWS. I want to be able to write a text file to S3 and have read many tutorials about how to integrate with S3. However, all of them are about how to call Lambda functions after writing to S3.

How can I create a text file in S3 from Lambda using node? Is this possible? Amazons documentation doesn't seem to cover it.

closed

1 Answer

0 votes
by (44.4k points)
selected by
 
Best answer

Use this code for doing that:

var AWS = require('aws-sdk');

function putObjectToS3(bucket, key, data){

    var s3 = new AWS.S3();

        var params = {

            Bucket : bucket,

            Key : key,

            Body : data

        }

        s3.putObject(params, function(err, data) {

          if (err) console.log(err, err.stack); // an error occurred

          else     console.log(data);           // successful response

        });

}

Also, the Lambda requires S3 write permissions for that particular S3 bucket and it’s key.

IAM Statement to add:

{

    "Sid": "Stmt1468366974000",

    "Effect": "Allow",

    "Action": "s3:*",

    "Resource": [

        "arn:aws:s3:::bucket_name/optional_path_before_allow/*"

    ]

}

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

Browse Categories

...