AWS S3 cli - 标记目录中的所有对象

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)

这有解决方法吗?

hel*_*loV 7

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)

  • 一般警告/提示... `aws s3api put-object-tagging` 在写入新标签时删除对象上的任何现有标签。 (4认同)
  • 这确实有效(我测试过)。但是您不需要那个 grep,因为 s3api list-objects 命令需要一个 --prefix 参数。像...`aws s3api list-objects --prefix foo/bar/ ...` (3认同)

Cal*_*ran 6

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)

该命令分为两部分:

  1. 列出存储桶前缀中的所有对象并输出到文本
  2. 使用xargs -n 1循环的每个结果和标记它的。


onc*_*nce 6

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)