Question:
Is there a way to apply a tag (or set of tags) to all objects in an S3 directory using one single put-object-tagging cli command?
I.e if I have two files (test0.txt
, test.txt
) I can do the run the following two commands:
1 2 3 |
>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}]' |
When trying to pass the folder itself as the –key option I get the following error (as it must reference a single object):
1 2 3 |
>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. |
Is there a workaround for this?
Answer:
There is no concept of a directory in S3. Here is a crude way of achieving what you want. Other posters may have a better solution. The following solution first gets all the objects in the folder and then calls put-object-tagging
for each one of them. Note: I didn’t test this solution.
1 2 3 4 |
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 |