74

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

Gray
  • 115,027
  • 24
  • 293
  • 354
felix
  • 741
  • 1
  • 5
  • 3
  • 1
    Voted to re-open, this is a pretty simple question that's not that ambigious. Going to edit to explicitly mention that file means s3 key. – David Aug 23 '13 at 17:32
  • 1
    In case anybody is wondering, you can do this via the AWS web interface (right click on file -> Rename) – scrowler Sep 21 '14 at 23:00

4 Answers4

70

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.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • 10
    Just a note that the copy feature looks like it's instant (perhaps a sym link). So no problem with speed doing this. – Mauvis Ledford May 31 '12 at 19:12
  • 18
    At the time of this comment, it does NOT seem to be instant. I'm copying a 2GB file and it is taking several minutes. – dtbarne Oct 01 '13 at 03:33
  • 1) Can somebody tell me , how much time it would take to copy and paste a 3 GB file in the the same bucket and at the same location. and then delete the original file. 2) It seems like the user interface has renaming functionality. But is the user interface also works in the same way ( copying and deleting ) in the background. – Yogesh Yadav Mar 10 '16 at 23:02
39

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)
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
garnaat
  • 44,310
  • 7
  • 123
  • 103
0

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. Thank you

Prasad Shigwan
  • 520
  • 5
  • 14
-6
//Copy the object
AmazonS3Client s3 = new AmazonS3Client("AWSAccesKey", "AWSSecretKey");

CopyObjectRequest copyRequest = new CopyObjectRequest()
      .WithSourceBucket("SourceBucket")
      .WithSourceKey("SourceKey")
      .WithDestinationBucket("DestinationBucket")
      .WithDestinationKey("DestinationKey")
      .WithCannedACL(S3CannedACL.PublicRead);
s3.CopyObject(copyRequest);

//Delete the original
DeleteObjectRequest deleteRequest = new DeleteObjectRequest()
       .WithBucketName("SourceBucket")
       .WithKey("SourceKey");
s3.DeleteObject(deleteRequest);
Gray
  • 115,027
  • 24
  • 293
  • 354
prasadNVM
  • 61
  • 1
  • 21
    -1: this is a solution with the [AWS SDK for .NET](http://aws.amazon.com/sdkfornet/) rather than the requested solution with [boto](https://github.com/boto/boto), which _is a Python package that provides interfaces to Amazon Web Services_. – Steffen Opel Apr 28 '12 at 16:25