Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 boto: How do you rename a file in a bucket?

Tags:

amazon-s3

boto

How do you rename a S3 key in a bucket with boto?

like image 677
felix Avatar asked Mar 20 '10 02:03

felix


People also ask

How do I rename a file in S3 bucket?

There is no direct method to rename the file in s3. what do you have to do is copy the existing file with new name (Just set the target key) and delete the old one.

How do I rename a S3 file in Python?

There is no direct command available to rename or move objects in S3 from Python SDK. Using AWS CLI, we have direct commands available, see the below example for the same. Using Python: We need to copy files from source location to destination location and then delete the file(object) from the source location.

Can we edit file in S3 bucket?

s3 doesn't support editing files... it stores objects that must be rewritten any time you want to update them.


2 Answers

You can't rename files in Amazon S3. You can copy them with a new name, then delete the original, but there's no proper rename function.

like image 168
ceejayoz Avatar answered Dec 06 '22 14:12

ceejayoz


Here is an example of a Python function that will copy an S3 object using Boto 2:

import boto  def copy_object(src_bucket_name,                 src_key_name,                 dst_bucket_name,                 dst_key_name,                 metadata=None,                 preserve_acl=True):     """     Copy an existing object to another location.      src_bucket_name   Bucket containing the existing object.     src_key_name      Name of the existing object.     dst_bucket_name   Bucket to which the object is being copied.     dst_key_name      The name of the new object.     metadata          A dict containing new metadata that you want                       to associate with this object.  If this is None                       the metadata of the original object will be                       copied to the new object.     preserve_acl      If True, the ACL from the original object                       will be copied to the new object.  If False                       the new object will have the default ACL.     """     s3 = boto.connect_s3()     bucket = s3.lookup(src_bucket_name)      # Lookup the existing object in S3     key = bucket.lookup(src_key_name)      # Copy the key back on to itself, with new metadata     return key.copy(dst_bucket_name, dst_key_name,                     metadata=metadata, preserve_acl=preserve_acl) 
like image 38
garnaat Avatar answered Dec 06 '22 15:12

garnaat