Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to copy all files from one S3 bucket to another with s3cmd?

I'm pretty happy with s3cmd, but there is one issue: How to copy all files from one S3 bucket to another? Is it even possible?

EDIT: I've found a way to copy files between buckets using Python with boto:

from boto.s3.connection import S3Connection  def copyBucket(srcBucketName, dstBucketName, maxKeys = 100):   conn = S3Connection(awsAccessKey, awsSecretKey)    srcBucket = conn.get_bucket(srcBucketName);   dstBucket = conn.get_bucket(dstBucketName);    resultMarker = ''   while True:     keys = srcBucket.get_all_keys(max_keys = maxKeys, marker = resultMarker)      for k in keys:       print 'Copying ' + k.key + ' from ' + srcBucketName + ' to ' + dstBucketName        t0 = time.clock()       dstBucket.copy_key(k.key, srcBucketName, k.key)       print time.clock() - t0, ' seconds'      if len(keys) < maxKeys:       print 'Done'       break      resultMarker = keys[maxKeys - 1].key 

Syncing is almost as straight forward as copying. There are fields for ETag, size, and last-modified available for keys.

Maybe this helps others as well.

like image 395
Jan Deinhard Avatar asked Mar 04 '11 13:03

Jan Deinhard


People also ask

What is the difference between S3 sync and S3 copy?

S3 cp – Will read all the files from the source location and write into the new location. S3 sync – Will scan the new location and only overwrite the file from the source location if the file is newly created or updated(via file size and modified timestamp comparison).

Why can't I copy an object between two Amazon S3 buckets?

If the object that you can't copy between buckets is owned by another account, then the object owner can do one of the following: The object owner can grant the bucket owner full control of the object. After the bucket owner owns the object, the bucket policy applies to the object.


2 Answers

s3cmd sync s3://from/this/bucket/ s3://to/this/bucket/

For available options, please use: $s3cmd --help

like image 85
amit_saxena Avatar answered Sep 19 '22 17:09

amit_saxena


AWS CLI seems to do the job perfectly, and has the bonus of being an officially supported tool.

aws s3 sync s3://mybucket s3://backup-mybucket 

http://docs.aws.amazon.com/cli/latest/reference/s3/sync.html

like image 21
python1981 Avatar answered Sep 19 '22 17:09

python1981