16

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:

>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):

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

3 Answers 3

15

helloV's answer is corrected (have tested it) but it's not a good solution if the bucket is big, aws will take time to scan the whole bucket. Here is my solution

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

The command has 2 parts:

  1. List all the object in prefix of bucket and output to text
  2. Use xargs -n 1 to loop each of the result and tag it.
4
  • This worked nicely for me in a bucket with ~9000 objects
    – NFlourish
    Feb 15, 2021 at 14:56
  • s3api list-objects with --query returns "None" for empty buckets, causing an aws: error: argument --key: expected one argument error. Was able to work around this with aws s3api list-objects --bucket mybucket --prefix foo/bar --output json | jq '.Contents[] | .Key' | xargs -rn 1 aws s3api put-object-tagging --bucket mybucket --tagging 'TagSet=[{Key=colour,Value=blue}]' --key Used jq to parse and added the -r flag to xargs. Dec 25, 2021 at 16:42
  • 1
    use xargs -P 15 to go faster, that will make 15 processes to request them. Makes large buckets go much faster
    – exussum
    Apr 2, 2022 at 13:28
  • See sykes.technology/blog/2022/04/27/Speed-Up-AWS-CLI-commands to speed up the command some more
    – exussum
    Apr 27, 2022 at 9:15
13

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.

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
2
  • 3
    This does work (I tested it). But you don't need that grep as the s3api list-objects command takes a --prefix argument. Like... aws s3api list-objects --prefix foo/bar/ ... Jun 8, 2018 at 21:22
  • 4
    A general warning/heads up... aws s3api put-object-tagging deletes any existing tags on the objects when it writes its new ones. Jul 31, 2018 at 20:08
6

Calvin Duy Canh Tran 's answer might return error for filename with spaces, in the command below, I added an -I flag for xargs to replace the argument "targetobject" with standard input.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.