fez*_*fez 7 amazon-s3 amazon-web-services aws-cli
有没有办法使用一个put-object-tagging cli命令将标记(或一组标记)应用于S3目录中的所有对象?
即如果我有两个文件(test0.txt,test.txt)我可以运行以下两个命令:
>aws s3api put-object-tagging --bucket mybucket --key foo/bar/test0.txt --tagging 'TagSet=[{Key=colour,Value=blue}]'
>aws s3api put-object-tagging --bucket mybucket --key foo/bar/test1.txt --tagging 'TagSet=[{Key=colour,Value=blue}]'
Run Code Online (Sandbox Code Playgroud)
当尝试将文件夹本身作为--key选项传递时,我收到以下错误(因为它必须引用单个对象):
>aws s3api put-object-tagging --bucket mybucket --key foo/bar/ --tagging 'TagSet=[{Key=colour,Value=blue}]
An error occurred (NoSuchKey) when calling the PutObjectTagging operation: The specified key does not exist.
Run Code Online (Sandbox Code Playgroud)
这有解决方法吗?
S3中没有目录的概念.这是达到你想要的原始方式.其他海报可能有更好的解决方案.以下解决方案首先获取文件夹中的所有对象,然后调用put-object-tagging它们中的每一个.注意:我没有测试这个解决方案.
aws s3api list-objects --bucket mybucket --query 'Contents[].{Key:Key}'
--output text | grep foo/bar/ | xargs aws s3api put-object-tagging
--bucket mybucket --tagging 'TagSet=[{Key=colour,Value=blue}]' --key
Run Code Online (Sandbox Code Playgroud)
helloV的答案已得到纠正(已经测试过),但是如果存储桶很大,这不是一个好的解决方案,aws将花费时间来扫描整个存储桶。这是我的解决方案
aws s3api list-objects --bucket mybucket --query 'Contents[].{Key:Key}' --prefix foo/bar --output text | xargs -n 1 aws s3api put-object-tagging --bucket mybucket --tagging 'TagSet=[{Key=colour,Value=blue}]' --key
Run Code Online (Sandbox Code Playgroud)
该命令分为两部分:
xargs -n 1循环的每个结果和标记它的。Calvin Duy Canh Tran的回答可能会返回带有空格的文件名错误,在下面的命令中,我添加了一个 -I 标志,xargs用于将参数“targetobject”替换为标准输入。
aws s3api list-objects --bucket mybucket --query 'Contents[].{Key:Key}' --prefix testfolder/ --output text | xargs -n 1 -I targetobject aws s3api put-object-tagging --bucket mybucket --tagging 'TagSet=[{Key=colour,Value=blue}]' --key targetobject
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3759 次 |
| 最近记录: |