Tag an S3 Bucket with AWS CLI

avatar

Borislav Hadzhiev

2 min

banner

Photo from Unsplash

Table of Contents #

  1. Tagging S3 Buckets with AWS CLI
  2. Tagging S3 Objects with AWS CLI

Tagging S3 Buckets with AWS CLI #

To tag an S3 bucket with the AWS CLI, run the put-bucket-tagging command, passing in the tag key-value pairs.

shell
aws s3api put-bucket-tagging --bucket your_bucket --tagging "TagSet=[{Key=department, Value=accounting}]"

create bucket tag

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

The put-bucket-tagging command deletes any tags the S3 bucket previously had, before it creates the new tags.

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

shell
aws s3api get-bucket-tagging --bucket your_bucket

verify bucket tagged

To create multiple tags on the bucket, pass multiple key-value pairs to the --tagging parameter:

shell
aws s3api put-bucket-tagging --bucket your_bucket --tagging "TagSet=[{Key=department, Value=accounting}, {Key=food, Value=cake}]"

create multiple bucket tags

To verify the tags got created successfully, run the get-bucket-tagging command again:

shell
aws s3api get-bucket-tagging --bucket your_bucket

verify multiple tags created

An alternative to passing the tag key-value pairs directly in the --tagging parameter is to store the tag values in a file and pass the file to the put-bucket-tagging command.

Create a file called bucket-tags.json with the following contents:

bucket-tags.json
{ "TagSet": [ { "Key": "color", "Value": "green" }, { "Key": "fruit", "Value": "avocado" } ] }

Open your terminal in the directory where you stored the bucket-tags.json file and run the put-bucket-tagging command pointing to the file:

shell
aws s3api put-bucket-tagging --bucket your_bucket --tagging file://bucket-tags.json

create tag from file

When we list the tags for the bucket, we can see that the tags from the file have been created:

list bucket tags

To delete all of the tags of an S3 bucket, run the delete-bucket-tagging command:

shell
aws s3api delete-bucket-tagging --bucket your_bucket

delete bucket tags

Note that running the get-bucket-tagging command on a bucket that has no tags, throws an error - "The TagSet does not exist":

tagset does not exist

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

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.