queirozsc
8/30/2017 - 6:43 PM

Copying Objects in S3 From One Bucket to Another Automatically w/ AWS Lambda https://www.aaronmedacco.com/blog/post/2017/01/11/copying-objec

var AWS = require("aws-sdk");
 
exports.handler = (event, context, callback) => {
    var s3 = new AWS.S3();
    var sourceBucket = "Source Bucket Name";
    var destinationBucket = "Destination Bucket Name";
    var objectKey = event.Records[0].s3.object.key;
    var copySource = encodeURI(sourceBucket + "/" + objectKey);
    var copyParams = { Bucket: destinationBucket, CopySource: copySource, Key: objectKey };
    s3.copyObject(copyParams, function(err, data) {
        if (err) {
            console.log(err, err.stack);
        } else {
            console.log("S3 object copy successful.");
        }
    });
};
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject"
            ],
            "Resource": [
                "Your Source Bucket ARN/*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "s3:PutObject"
            ],
            "Resource": [
                "Your Destination Bucket ARN/*"
            ]
        }
    ]
}