AWS Lambda function write to S3

后端 未结 4 1568
星月不相逢
星月不相逢 2020-12-08 06:31

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

4条回答
  •  难免孤独
    2020-12-08 07:02

    Yes it is absolutely possible!

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

    Make sure that you give your Lambda function the required write permissions to the target s3 bucket / key path by selecting or updating the IAM Role your lambda executes under.

    IAM Statement to add:

    {
        "Sid": "Stmt1468366974000",
        "Effect": "Allow",
        "Action": "s3:*",
        "Resource": [
            "arn:aws:s3:::my-bucket-name-goes-here/optional-path-before-allow/*"
        ]
    }
    

    Further reading:

    • AWS JavaScript SDK
    • The specific "Put Object" details

提交回复
热议问题