There are more AWS SDK examples available in the AWS Doc SDK Examples
Use PutBucketTagging
with an AWS SDK or CLI
The following code examples show how to use PutBucketTagging
.
- CLI
-
- AWS CLI
-
The following command applies a tagging configuration to a bucket named
my-bucket
:aws s3api put-bucket-tagging --bucket
my-bucket
--taggingfile://tagging.json
The file
tagging.json
is a JSON document in the current folder that specifies tags:{ "TagSet": [ { "Key": "organization", "Value": "marketing" } ] }
Or apply a tagging configuration to
my-bucket
directly from the command line:aws s3api put-bucket-tagging --bucket
my-bucket
--tagging 'TagSet=[{Key=organization,Value=marketing}]
'-
For API details, see PutBucketTagging
in AWS CLI Command Reference.
-
- PowerShell
-
- Tools for PowerShell
-
Example 1: This command applies two tags to a bucket named
cloudtrail-test-2018
: a tag with a key of Stage and a value of Test, and a tag with a key of Environment and a value of Alpha. To verify that the tags were added to the bucket, runGet-S3BucketTagging -BucketName bucket_name
. The results should show the tags that you applied to the bucket in the first command. Note thatWrite-S3BucketTagging
overwrites the entire existing tag set on a bucket. To add or delete individual tags, run the Resource Groups and Tagging API cmdlets,Add-RGTResourceTag
andRemove-RGTResourceTag
. Alternatively, use Tag Editor in the AWS Management Console to manage S3 bucket tags.Write-S3BucketTagging -BucketName cloudtrail-test-2018 -TagSet @( @{ Key="Stage"; Value="Test" }, @{ Key="Environment"; Value="Alpha" } )
Example 2: This command pipes a bucket named
cloudtrail-test-2018
into theWrite-S3BucketTagging
cmdlet. It applies tags Stage:Production and Department:Finance to the bucket. Note thatWrite-S3BucketTagging
overwrites the entire existing tag set on a bucket.Get-S3Bucket -BucketName cloudtrail-test-2018 | Write-S3BucketTagging -TagSet @( @{ Key="Stage"; Value="Production" }, @{ Key="Department"; Value="Finance" } )
-
For API details, see PutBucketTagging in AWS Tools for PowerShell Cmdlet Reference.
-