Skip to main content

Command Palette

Search for a command to run...

AWS Tagging

s3 bucket tagging

Published
2 min read
AWS Tagging

How to tag an s3 bucket and its object using AWS CLI

Step1: Go to Cloud Shell or AWS CLI in your local PC.

Step2 : Create an s3 bucket using CLI command:

aws s3api create-bucket --bucket <mybucketname>

Check in the management console, if the s3 bucket has been created.

Step3: Let us learn the CLI commands to add,view and delete tags to an s3 bucket.

To add a single tag to your s3 bucket:

aws s3api put-bucket-tagging --bucket <bucketname> --tagging "TagSet"=[{Key=Type,Value=Image}]

To add multiple tags to your s3 bucket:

aws s3api put-bucket-tagging --bucket <bucketname> --tagging "TagSet"=[{Key=Type,Value=Image},{Key=Size,Value=Upto 10MB}]

To add multiple tags using a json file, first create a json file let's say named tags.json.Use the following commands:

-> touch tags.json

-> vim tags.json

Then type the following in the file:

{

"TagSet": [

{

"Key":"Type",

"Value": "Photo"

},

{

"Key":"Size",

"Value":"Upto 10MB"

}

]

}

Once you are done with creating a json file with tags, save it and exit by pressing "ESC+:+wq" and then use the following command:

aws s3api put-bucket-tagging --bucket <bucketname> --tagging file://tags.json

To view the tags of the bucket in CLI, use the following command:

aws s3api get-bucket-tagging --bucket <bucketname>

To delete tags of your bucket , use the below command:

aws s3api delete-bucket-tagging --bucket <bucketname>

Step4: Upload an object to the bucket using CLI commands

aws s3 cp <filepath> s3://<bucketname>

Check in the console if the object is uploaded in the s3 bucket.

Step5: Let us add,view and delete tags to the objects of the s3 bucket

To add tag to the object of your s3 bucket, add the following command:

aws s3api put-object-tagging --bucket <bucketname> --key <objectname> --tagging {\"TagSet\":[{\"Key\":\"Name\",\"Value\":\"Dog\"}]

Use same method to add multiple tags in same command as well as multiple tags using json file , as we did for bucket tagging.

To view the tags of your object in CLI, use the command:

aws s3api get-object-tagging --bucket <bucketname> --key <keyname>

To delete tagging of your object, use the following command:

aws s3api delete-object-tagging --bucket <bucketname> --key <objectname>