Last updated: Feb 26, 2024
Reading time·3 min
To tag an S3 bucket with the AWS CLI, run the put-bucket-tagging
command,
passing in the tag key-value pairs.
aws s3api put-bucket-tagging --bucket your_bucket --tagging "TagSet=[{Key=department, Value=accounting}]"
The example command creates a tag with the key of department
and a value of
accounting
.
put-bucket-tagging
command deletes any tags the S3 bucket previously had, before it creates the new tags.To verify the S3 Bucket has been tagged, run the get-bucket-tagging
command.
aws s3api get-bucket-tagging --bucket your_bucket
To create multiple tags on the bucket, pass multiple key-value pairs to the
--tagging
parameter:
aws s3api put-bucket-tagging --bucket your_bucket --tagging "TagSet=[{Key=department, Value=accounting}, {Key=food, Value=cake}]"
To verify the tags got created successfully, run the get-bucket-tagging
command again.
aws s3api get-bucket-tagging --bucket your_bucket
An alternative to passing the tag key-value pairs directly in the --tagging
parameter is to store the tag values in a file
You can then pass the file to the put-bucket-tagging
command.
Create a file called bucket-tags.json
with the following contents:
{ "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 it to the file.
aws s3api put-bucket-tagging --bucket your_bucket --tagging file://bucket-tags.json
When we list the tags for the bucket, we can see that the tags from the file have been created:
To delete all of the tags of an S3 Bucket, run the delete-bucket-tagging
command:
aws s3api delete-bucket-tagging --bucket your_bucket
Note that running the get-bucket-tagging
command on a bucket that has no tags,
throws an error - "The TagSet does not exist".
To tag an S3 object with the AWS CLI, use the put-object-tagging
command.
aws s3api put-object-tagging --bucket your_bucket --key dog.png --tagging "{\"TagSet\": [{\"Key\": \"department\", \"Value\": \"accounting\"}]}"
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
.
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.
aws s3api get-object-tagging --bucket your_bucket --key dog.png
To create multiple tags on an S3 object with the AWS CLI, pass multiple
key:value
pair objects in the --tagging
parameter.
aws s3api put-object-tagging --bucket your_bucket --key dog.png --tagging "{\"TagSet\": [{\"Key\": \"department\", \"Value\": \"accounting\"}, {\"Key\": \"color\", \"Value\": \"green\"}]}"
To verify that both of the tags were created on the S3 object, run the
get-object-tagging
command.
aws s3api get-object-tagging --bucket your_bucket --key dog.png
You can learn more about the related topics by checking out the following tutorials: