Deepak Singhal
Deepak Singhal

Reputation: 10866

Copy objects between buckets using aws sdk

Using AWS CLI; we can copy or sync files directly from one bucket to other. Using SDK; I can see api for download and upload. But can we directly copy files from one bucket to other bucket ( in different aws account) using sdk !

Upvotes: 2

Views: 1475

Answers (2)

John Rotenstein
John Rotenstein

Reputation: 269101

Yes. The CopyObject API call can copy an object between Amazon S3 buckets, including bucket in different regions and different accounts.

To copy objects between accounts, the one set of credentials requires sufficient permission to Read from the source bucket and Write to the destination bucket. You can either:

  • Use credentials from the destination account, and use a Bucket Policy on the source bucket to grant Read access, or
  • Use credentials from the source account, and use a Bucket policy on the destination bucket to grant Write access. Make sure you set ACL=public-read to pass ownership of the object to the destination Account.

Please note that it only copies one object at a time, so you would need to loop through a list of objects and call CopyObject for each one individually if you wish to copy multiple objects.

Upvotes: 3

Jeremy Thompson
Jeremy Thompson

Reputation: 65534

It's easy, see all the CLI commands with the help:

aws s3 --help

Upload a file:

aws s3 cp <path-to-file-from-local> s3://<S3_BUCKET_NAME>/<file-name> 

Download a file:

aws s3 cp s3://<S3_BUCKET_NAME>/<file-name> <path-to-file-from-local> 

Move a file:

aws s3 mv s3://<S3_BUCKET_NAME>/<file-name> s3://<S3_BUCKET_NAME>/<file-name>

You can use . to specify the current directory, eg:

aws s3 cp s3://MyBucket/Test.txt .

Upvotes: 1

Related Questions