简体   繁体   English

如何通过 aws cli 在 Amazon s3 中重命名“PREFIX”?

[英]How to rename “PREFIX” in Amazon s3 via aws cli?

I understand that in Amazon s3, there are only buckets and objects .我知道在 Amazon s3 中, 只有 buckets 和 objects There are no such things as folders and all files sit in the same bucket, and that objects that appear to be in a folder only have a prefix in front of their file name.没有文件夹和所有文件都在同一个存储桶中这样的东西,并且看起来在文件夹中的对象只有在它们的文件名前面有一个前缀。

Now my question is... is there a way to change the PREFIX using AWS CLI?现在我的问题是……有没有办法使用 AWS CLI 更改 PREFIX?

In awscli2 I was able to accomplish this with the following command:在 awscli2 中,我能够使用以下命令完成此操作:

aws s3 mv --recursive s3://bucket/prefix1/ s3://bucket/prefix2/

I ran a quick test (dryrun) prior to doing this by checking the output of:在执行此操作之前,我通过检查以下输出进行了快速测试(dryrun):

aws s3 --dryrun mv --recursive s3://bucket/prefix1/ s3://bucket/prefix2/

Yes, but not easily.是的,但并不容易。 Keeping in mind that a "prefix" is just part of a filename, changing a prefix will require that you rename every file that has that prefix in it — for instance, if your bucket contained the following files, and you wanted to rename /oldprefix/ to /newprefix/ :请记住,“前缀”只是文件名的一部分,更改前缀将要求您重命名包含该前缀的每个文件 - 例如,如果您的存储桶包含以下文件,并且您想重命名/oldprefix//newprefix/ :

/oldprefix/file1.txt
/oldprefix/file2.txt
/oldprefix/folder/anotherfile.txt

You would need to perform three* operations to accomplish this change of prefix: one for each object.您需要执行三个* 操作来完成前缀的这种更改:每个对象一个。 There is no way to rename them all in a single operation.无法在一次操作中将它们全部重命名。 In general, you should try to avoid situations where you need to do this kind of renaming on a large scale, as it can be a rather lengthy process.一般来说,您应该尽量避免需要大规模进行这种重命名的情况,因为这可能是一个相当漫长的过程。

*: If you want to get picky about it, there are actually not three but six operations required, since renaming an object is accomplished in two steps by first copying it to the new name, then deleting the original. *:如果你想挑剔它,实际上不需要三个而是六个操作,因为重命名对象分两步完成,首先将其复制到新名称,然后删除原始名称。 Many S3 clients and libraries will handle this detail for you, though.不过,许多 S3 客户端和库会为您处理这个细节。

[edit] I realized that my answer didn't fully answer the OP, and as several people pointed out, could use some more information. [编辑] 我意识到我的回答并没有完全回答 OP,正如一些人指出的那样,可以使用更多信息。

In general, the AWS CLI for S3 will look for objects under a specified bucket AND prefix.通常, 适用于 S3AWS CLI将在指定的存储桶 AND 前缀下查找对象。 If the prefix is included, it ignores that portion of an object's name when matching objects.如果包含前缀,则在匹配对象时会忽略对象名称的该部分。

For example, say you have a number of images two folders deep in a bucket backup .例如,假设您在存储桶备份中拥有多个位于两个文件夹深处的图像。 Your object name may look like this photos/graduation/image1.jpg .您的对象名称可能看起来像这样photos/graduation/image1.jpg

You can copy the inner folder ( graduation in this case), as well as all files in that folder, to the top level with the following command:您可以使用以下命令将内部文件夹(在这种情况下为毕业)以及该文件夹中的所有文件复制到顶层:

aws s3 cp s3://backup/photos s3://backup --recursive

This matches all files in the bucket backup with the prefix photos .这将匹配存储桶备份中带有前缀photos 的所有文件。 It will copy those files to the backup bucket, and name them whatever is left after stripping of the prefix - so in this case, they will be named graduation/image1.jpg .它将这些文件复制到备份存储桶,并在去除前缀后将它们命名为任何剩余的名称 - 因此在这种情况下,它们将被命名为degree/image1.jpg

Note that the original files will still be in the original folder.请注意,原始文件仍将位于原始文件夹中。 To truly simulate a rename of the prefix, you need to remove the old files.要真正模拟前缀的重命名,您需要删除旧文件。

aws rm s3://backup/photos/graduation --recursive 

Once you are done, you will have a graduation folder of images under the root level of your bucket, and none under the photos folder.完成后,您将在存储桶的根级别下有一个图像毕业文件夹,而在照片文件夹下没有。 In other words, you have renamed the prefix from photos/graduation to graduation .换句话说,您已将前缀从照片/毕业重命名为毕业

Unfortunately it does not appear that you can use the mv command to do this within the same bucket - it fails with an error saying "Cannot mv a file onto itself".不幸的是,您似乎无法使用mv命令在同一个存储桶中执行此操作 - 它失败并显示“无法将文件 mv 到自身”的错误。

Ran into the same problem.遇到了同样的问题。 Below is how I solved it with the aws cli from Linux bash.以下是我使用 Linux bash 中的 aws cli 解决它的方法。

# check current contents
aws s3 ls --human-readable s3://mybucket/

Output:输出:

PRE AK/
PRE AR/
PRE DC/
PRE DE/
PRE HI/
PRE OH/
PRE TN/
PRE VT/

Construct array to iterate over then iterate and execute aws s3 mv on each existing prefix and move into a new prefix, reusing the existing prefix as needed.构造数组进行迭代,然后在每个现有前缀上迭代和执行aws s3 mv并移动到新前缀,根据需要重用现有前缀。

prefixArr=(AK AR DC DE HI OH TN VT)

for s3p in ${prefixArr2[@]}; do
     echo moving s3://mybucket/$s3p
     aws s3 mv s3://mybucket/$s3p s3://mybucket/state=$s3p.parquet --recursive
done

Output:输出:

moving s3://mybucket/AK
move: s3://mybucket/AK/_SUCCESS to s3://mybucket/state=AK.parquet/_SUCCESS
move: s3://mybucket/AK/part-00002.snappy.parquet to s3://mybucket/state=AK.parquet/part-00002.snappy.parquet
move: s3://mybucket/AK/part-00003.snappy.parquet to s3://mybucket/state=AK.parquet/part-00003.snappy.parquet
...

Check results:检查结果:

aws s3 ls --human-readable s3://mybucket/

Output:输出:

PRE state=AK.parquet/
PRE state=AR.parquet/
PRE state=DC.parquet/
PRE state=DE.parquet/
PRE state=HI.parquet/
PRE state=OH.parquet/
PRE state=TN.parquet/
PRE state=VT.parquet/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM