I want to automatically move objects from first s3 bucket to second bucket. As and when a file is created or uploaded to first bucket, that should be moved across to the second bucket. There shouldn’t be any copy of the file on the source bucket after the transfer.
I have seen examples of aws s3 sync but that leaves a copy on the source bucket and it’s not automated.
aws mv command from cli will move the files across but how to automate the process. Creating a lambda notification and send the files to second bucket could solve but I am looking for a more automated simpler solution. Not sure if there is anything we could do with SQS? Is there anything we can set on the source bucket which would automatically send the object to the second? Appreciate any ideas
2
Answers
There is no "move" command in Amazon S3. Instead, it involves
CopyObject()
andDeleteObject()
. Even the AWS CLIaws mv
command does a Copy and Delete.The easiest way to achieve your object is:
Here’s some sample code:
It will copy the object to the same path in the destination bucket and then delete the source object.
The Lambda function should be assigned an IAM Role that has sufficient permission to
GetObject
from the source bucket andCopyObject
+PutObject
in the destination bucket.A followup question over this. How about an approach of setting up a S3 Replication to the destination S3 bucket. The file landing on the destination bucket can trigger a Lambda to cleanup the source bucket.
Bottomline question: Is S3 replication more efficient compared to a S3 copy()?