26 September 2014

You cannot rename an S3 bucket directly, but there are various reasons that you might desperately need to make such a change. For instance, if you created bucket "bucket.example.com" years ago with dots, you might need to rename it to "bucket-example-com" to eliminate the dots so that the SSL validates appropriately when accessed as a subdomain of s3.amazonaws.com. Such access methods do not appear to be necessary versus referencing the bucket as the path prefix, but it has been standardized in some software like ec2-upload-bundle.

The Solution

  1. Create the new bucket "new-bucket" (can be done online)
  2. Copy all objects from "old.bucket" to "new-bucket" from the command-line (ideally from EC2):
    aws s3 sync s3://old.bucket s3://new-bucket
  3. Update your system to point at new-bucket
  4. Empty the old bucket using an appropriate strategy:
    1. If you your objects are in folders, you can empty the bucket by deleting the root objects and folders through the online management console.
    2. If you have significant amounts of content in the bucket, you can set a lifecycle rule to delete content after 0 days. It may take a day for the bucket to get emptied, but it is easy if you are patient. (can be done online)
    3. If you're in a hurry and did not use folders for easy manual deletion, you can use a quick script like:
      aws s3 ls s3://old.bucket | sed 's/.* //' | while read f; do aws s3 rm "s3://old.bucket/$f"; done
  5. Delete the old bucket (can be done online)

blog comments powered by Disqus