Tag S3 Objects with AWS CLI

avatar

Borislav Hadzhiev

Thu Sep 16 20212 min read

banner

Photo by Rashel Ochoa

Tagging S3 Objects with AWS CLI #

To tag an S3 object with the AWS CLI, use the put-object-tagging command.

shell
aws s3api put-object-tagging --bucket your_bucket --key dog.png --tagging "{\"TagSet\": [{\"Key\": \"department\", \"Value\": \"accounting\"}]}"

tag s3 object

The command creates a tag with a key of department and a value of accounting.

If the S3 object is nested in a "directory", pass the --key parameter as the whole path that leads to the object, i.e. /static/images/dog.png.

Note that the put-object-tagging command deletes any tags the S3 object previously had, before creating the new tags.

To verify the S3 object got tagged successfully, run the get-object-tagging command:

shell
aws s3api get-object-tagging --bucket your_bucket --key dog.png

list object tags

To create multiple tags on an S3 object with the AWS CLI, pass multiple key:value pair objects in the --tagging parameter:

shell
aws s3api put-object-tagging --bucket your_bucket --key dog.png --tagging "{\"TagSet\": [{\"Key\": \"department\", \"Value\": \"accounting\"}, {\"Key\": \"color\", \"Value\": \"green\"}]}"

create multiple tags

AWS S3 limits the number of tags to a maximum of 10 tags per object.

To verify that both of the tags were created on the S3 object, run the get-object-tagging command:

shell
aws s3api get-object-tagging --bucket your_bucket --key dog.png

verify multiple tags

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee