Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename AWS S3 Bucket name

People also ask

Can I rename S3 bucket name?

An Amazon S3 bucket is owned by the AWS account that created it. Bucket ownership is not transferable to another account. When you create a bucket, you choose its name and the AWS Region to create it in. After you create a bucket, you can't change its name or Region.

Can we rename a bucket name?

Your answer Yes, There's no option to rename bucket functionality for S3 because there are technically no folders in S3 so we have to handle every file within the bucket. Create a new bucket, copy the contents from the new bucket and delete the old bucket.

How set S3 bucket name?

The following rules apply for naming buckets in Amazon S3: Bucket names must be between 3 (min) and 63 (max) characters long. Bucket names can consist only of lowercase letters, numbers, dots (.), and hyphens (-). Bucket names must begin and end with a letter or number.

Can we rename object 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.


Solution

aws s3 mb s3://[new-bucket]
aws s3 sync s3://[old-bucket] s3://[new-bucket]
aws s3 rb --force s3://[old-bucket]

Explanation

There's no rename bucket functionality for S3 because there are technically no folders in S3 so we have to handle every file within the bucket.

The code above will 1. create a new bucket, 2. copy files over and 3. delete the old bucket. That's it.

If you have lots of files in your bucket and you're worried about the costs, then read on. Behind the scenes what happens is that all the files within the bucket are first copied and then deleted. It should cost an insignificant amount if you have a few thousand files. Otherwise check this answer to see how this would impact you.

Example

In the following example we create and populate the old bucket and then sync the files to the new one. Check the output of the commands to see what AWS does.

> # bucket suffix so we keep it unique
> suffix="ieXiy2"  # used `pwgen -1 -6` to get this
>
> # populate old bucket
> echo "asdf" > asdf.txt
> echo "yxcv" > yxcv.txt
> aws s3 mb s3://old-bucket-$suffix
make_bucket: old-bucket-ieXiy2
> aws s3 cp asdf.txt s3://old-bucket-$suffix/asdf.txt
upload: ./asdf.txt to s3://old-bucket-ieXiy2/asdf.txt
> aws s3 cp yxcv.txt s3://old-bucket-$suffix/yxcv.txt
upload: ./yxcv.txt to s3://old-bucket-ieXiy2/yxcv.txt
>
> # "rename" to new bucket
> aws s3 mb s3://new-bucket-$suffix
make_bucket: new-bucket-ieXiy2
> aws s3 sync s3://old-bucket-$suffix s3://new-bucket-$suffix
copy: s3://old-bucket-ieXiy2/yxcv.txt to s3://new-bucket-ieXiy2/yxcv.txt
copy: s3://old-bucket-ieXiy2/asdf.txt to s3://new-bucket-ieXiy2/asdf.txt
> aws s3 rb --force s3://old-bucket-$suffix
delete: s3://old-bucket-ieXiy2/asdf.txt
delete: s3://old-bucket-ieXiy2/yxcv.txt
remove_bucket: old-bucket-ieXiy2

I think only way is to create a new bucket with correct name and then copy all your objects from old bucket to new bucket. You can do it using Aws CLI.


Probably a later version of the AWS CLI toolkit provided the mv option.

$ aws --version
aws-cli/1.15.30 Python/3.6.5 Darwin/17.6.0 botocore/1.10.30

I'm renaming buckets using the following command:

aws s3 mv s3://old-bucket s3://new-bucket --recursive