Ronan shows you how
to use AWS Lambda
to copy Amazon S3 objects

ronan_dub_0317

I want to copy an object from one S3 bucket to another S3 bucket. How can I do this?

You can copy Amazon Simple Storage Service (Amazon S3) objects from one bucket to another using a Node.js Lambda function.

Create two S3 buckets, one to use as the source, and the other to use as the destination.

Create the IAM Policy to assign to the Lambda function role

As a best practice you should use a Grant Least Privilege IAM policy. For information on creating custom policies, see Creating IAM Policies.

1.    In the Amazon IAM console, choose Policies, Create policy.

2.    Select the JSON tab, paste in this policy, and then choose Review policy.
Note: Replace YOUR-SOURCE-BUCKET and YOUR-DESTINATION-BUCKET with your source and destination bucket names.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "ListSourceAndDestinationBuckets",
            "Effect": "Allow",
            "Action": [
                "s3:ListBucket",
                "s3:ListBucketVersions"
            ],
            "Resource": [
                "arn:aws:s3:::YOUR-SOURCE-BUCKET",
                "arn:aws:s3:::YOUR-DESTINATION-BUCKET"
            ]
        },
        {
            "Sid": "SourceBucketGetObjectAccess",
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:GetObjectVersion"
            ],
            "Resource": "arn:aws:s3:::YOUR-SOURCE-BUCKET/*"
        },
        {
            "Sid": "DestinationBucketPutObjectAccess",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject"
            ],
            "Resource": "arn:aws:s3:::YOUR-DESTINATION-BUCKET/*"
        }
    ]
 }

3.    Enter a name and description that is meaningful to you, and then choose Create policy.

Create the IAM Role to assign to the Lambda function

1.    In the Amazon IAM console, choose Roles, Create role.

2.    For AWS Services role, select Lambda, and then choose Next: Permissions.

3.    Select the policy created in the previous step (you can use the search bar) and then choose Next: Review.

4.    Enter a name and description that is meaningful to you, and then choose Create role.

Create the Lambda Function

1.    In the Lambda console, choose Create function.

2.    Choose Author from scratch, and then choose Next.

3.    Enter a Name, and then for Role select Choose an existing role.

4.    For Existing role, select the role that you created, and then choose Create function.

5.    In the Lambda Function code section, enter your API calls. You can build your own custom API calls and reference AWS.S3, or consider this sample template.
Note: Replace "YOUR-SOURCE-BUCKET" and "YOUR-DESTINATION-BUCKET" with the names of your S3 buckets. You will also need to replace "YOUR-SOURCE-OBJECT" with your object name, for example "folder-name/object-name.jpg".

// Load the AWS SDK
const aws = require('aws-sdk');

// Construct the AWS S3 Object - 
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor-property
const s3 = new aws.S3({
            apiVersion: '2006-03-01'
 });
        
// Define 2 new variables for the source and destination buckets
var srcBucket = "YOUR-SOURCE-BUCKET";
var destBucket = "YOUR-DESTINATION-BUCKET";
var sourceObject = "YOUR-SOURCE-OBJECT";

//Main function
exports.handler = (event, context, callback) => {
        
//Copy the current object to the destination bucket
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#copyObject-property
s3.copyObject({ 
    CopySource: srcBucket + '/' + sourceObject,
    Bucket: destBucket,
    Key: sourceObject
    }, function(copyErr, copyData){
       if (copyErr) {
            console.log("Error: " + copyErr);
         } else {
            console.log('Copied OK');
         } 
    });
  callback(null, 'All done!');
};
        

6.    Under Role, verify that Choose an existing role is selected, choose your Existing role, choose Next, and then choose Create function.

7.Choose Test, and then choose Save and test.

After you run the function, wait a few seconds and check your destination bucket. It should have the object copied from the source bucket.


Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center.

Published: 2018-02-08