如何重命名AWS S3 Bucket名称

时间:2021-08-04 02:41:52

After all the tough works of migration etc. Just realise that If need to serve the content using CNAME (e.g media.abc.com). The bucket name need to start with media.abc.com/S3/amazon.com to ensure it work perfectly.

经过所有艰难的迁移工作等等,只需要意识到如果需要使用CNAME(例如media.abc.com)来提供内容。存储桶名称需要从media.abc.com/S3/amazon.com开始,以确保其完美运行。

Just realise that S3 don't allow direct rename from the console.

只是意识到S3不允许从控制台直接重命名。

Is there any ways to work around for this?

有没有办法解决这个问题?

3 个解决方案

#1


49  

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.

我认为唯一的方法是创建一个具有正确名称的新存储桶,然后将所有对象从旧存储桶复制到新存储桶。您可以使用Aws CLI执行此操作。

#2


136  

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.

S3没有重命名存储桶功能,因为S3中技术上没有文件夹,所以我们必须处理存储桶中的每个文件。

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

上面的代码将1.创建一个新的存储桶,2。复制文件和3.删除旧存储桶。而已。

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.

在下面的示例中,我们创建并填充旧存储桶,然后将文件同步到新存储桶。检查命令的输出以查看AWS的功能。

> # 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

#3


3  

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

可能更新版本的AWS CLI工具包提供了mv选项。

$ 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

#1


49  

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.

我认为唯一的方法是创建一个具有正确名称的新存储桶,然后将所有对象从旧存储桶复制到新存储桶。您可以使用Aws CLI执行此操作。

#2


136  

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.

S3没有重命名存储桶功能,因为S3中技术上没有文件夹,所以我们必须处理存储桶中的每个文件。

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

上面的代码将1.创建一个新的存储桶,2。复制文件和3.删除旧存储桶。而已。

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.

在下面的示例中,我们创建并填充旧存储桶,然后将文件同步到新存储桶。检查命令的输出以查看AWS的功能。

> # 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

#3


3  

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

可能更新版本的AWS CLI工具包提供了mv选项。

$ 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