Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone a key in Amazon S3 using Python (and boto)?

I have a file contained in a key in my S3 bucket. I want to create a new key, which will contain the same file. Is it possible to do without downloading that file? I'm looking for a solution in Python (and preferably boto library).

like image 887
Thotep Avatar asked Sep 23 '09 09:09

Thotep


People also ask

How do I copy files from one S3 bucket to another in Python?

You can use the Boto3 Session and bucket. copy() method to copy files between S3 buckets. You need your AWS account credentials for performing copy or move operations.


2 Answers

Where bucket is the destination bucket:

bucket.copy_key(new_key,source_bucket,source_key)
like image 157
Adam Nelson Avatar answered Sep 27 '22 18:09

Adam Nelson


from boto.s3.key import Key

#Get source key from bucket by name
source_key = source_bucket.get_key(source_key_name)

#Copy source key to a new bucket with a new key name (can be the same as source)
#Note: source_key is Key
source_key.copy(dest_bucket_name,dest_key_name)

#The signature of boto's Key class:
def copy(self, dst_bucket, dst_key, metadata=None,
             reduced_redundancy=False, preserve_acl=False,
             encrypt_key=False, validate_dst_bucket=True)

#set preserve_acl=True to copy the acl from the source key
like image 31
Amit Talmor Avatar answered Sep 27 '22 19:09

Amit Talmor