Tag a Bucket
Using this feature you will be able to label boxes. Tags allow you to perform batch operations on boxes with a common tag.
Components
- Credentials
- Bucket Name
- Tag
Tag
Each tag consists of a key-value component, which is used as follows:
- .NET
- PHP
- Python
- Javascript
- GO
{
tagList.Add(new Tag() { Key = "Key1", Value = "Value1" }); //REQUIRED
tagList.Add(new Tag() { Key = "Key2", Value = "Value2" }); //REQUIRED
}
Note that by using this function, the new tags will replace the previous tags. If you want to add new tags to the old tags, you must first get the old tags using the GetBucketTaggingAsync request and then pass the set of new and old tags as input to that.
Limits
- The maximum number of tags for a bucket is 10.
- The value of the Key for the tags of a bucket must be unique.
- The value of the Key and the Value for each bucket must be in the form of a string.
- The length of the Key can be up to 128 Unicode characters and also the length of the Value can be up to 256 Unicode characters.
- Key and Value are case-sensitive.
Code Sample
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Reflection;
using System.Collections;
namespace PutBucketTags
{
class PutBucketTags
{
private const string bucketName = "<BUCKET_NAME>";
private static IAmazonS3 _s3Client;
public static void Main()
{
var awsCredentials = new Amazon.Runtime.BasicAWSCredentials("<ACCESS-KEY>", "<SECRET-KEY>");
var config = new AmazonS3Config { ServiceURL = "<ENDPOINT>" };
_s3Client = new AmazonS3Client(awsCredentials, config);
PutBucketTagsAsync().Wait();
}
private static async Task PutBucketTagsAsync()
{
try
{
List<Tag> tagList = new List<Tag>();
// Add parts to the list.
tagList.Add(new Tag() { Key = "Key1", Value = "Value1" });
tagList.Add(new Tag() { Key = "Key2", Value = "Value2" });
PutBucketTaggingResponse response = await _s3Client.PutBucketTaggingAsync(bucketName, tagList);
foreach (PropertyInfo prop in response.GetType().GetProperties())
{
Console.WriteLine($"{prop.Name}: {prop.GetValue(response, null)}");
}
Console.WriteLine($"Tags added to {bucketName} bucket");
}
catch (AmazonS3Exception amazonS3Exception)
{
Console.WriteLine("An AmazonS3Exception was thrown. Exception: " + amazonS3Exception.ToString());
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.ToString());
}
}
}
}
{
'Key' => '<string>', // REQUIRED
'Value' => '<string>', // REQUIRED
}
Note that by using this function, the new tags will replace the previous tags. If you want to add new tags to the old tags, you must first get the old tags using the get_bucket_tagging request and then pass the set of new and old tags as input to that.
Limits
- The maximum number of tags for a bucket is 10.
- The value of the Key for the tags of a bucket must be unique.
- The value of the Key and the Value for each bucket must be in the form of a string.
- The length of the Key can be up to 128 Unicode characters and also the length of the Value can be up to 256 Unicode characters.
- Key and Value are case-sensitive.
Code Sample
<?php
require('client.php');
$bucket = $config['sample_bucket'];
try {
$result = $client->putBucketTagging([
'Bucket' => $bucket, // REQUIRED
'Tagging' => [ // REQUIRED
'TagSet' => [ // REQUIRED
[
'Key' => '<string>', // REQUIRED
'Value' => '<string>', // REQUIRED
],
// ...
],
],
]);
var_dump($result);
} catch (AwsException $e) {
// Display error message
echo $e->getMessage();
echo "\n";
}
{
"Key": "str", # REQUIRED
"Value": "str" # REQUIRED
}
Note that by using this function, the new tags will replace the previous tags. If you want to add new tags to the old tags, you must first get the old tags using the get_bucket_tagging request and then pass the set of new and old tags as input to that.
Limits
- The maximum number of tags for a bucket is 10.
- The value of the Key for the tags of a bucket must be unique.
- The value of the Key and the Value for each bucket must be in the form of a string.
- The length of the Key can be up to 128 Unicode characters and also the length of the Value can be up to 256 Unicode characters.
- Key and Value are case-sensitive.
Code Sample
import boto3
import logging
from botocore.exceptions import ClientError
# Configure logging
logging.basicConfig(level=logging.INFO)
try:
# S3 client
s3_client = boto3.client(
's3',
endpoint_url='endpoint_url',
aws_access_key_id='access_key',
aws_secret_access_key='secret_key'
)
except Exception as exc:
logging.error(exc)
else:
try:
bucket_name = 'bucket_name'
response = s3_client.put_bucket_tagging(
Bucket = bucket_name,
Tagging = {
'TagSet': [
{
'Key': 'string',
'Value': 'string'
},
]
},
)
logging.info(response)
except ClientError as e:
logging.error(e)
{
Key: "string", # REQUIRED
Value: "string" # REQUIRED
}
Note that by using this function, the new tags will replace the previous tags. If you want to add new tags to the old tags, you must first get the old tags using the get_bucket_tagging request and then pass the set of new and old tags as input to that.
Limits
- The maximum number of tags for a bucket is 10.
- The value of the Key for the tags of a bucket must be unique.
- The value of the Key and the Value for each bucket must be in the form of a string.
- The length of the Key can be up to 128 Unicode characters and also the length of the Value can be up to 256 Unicode characters.
- Key and Value are case-sensitive.
Code Sample
// Import required AWS SDK clients and commands for Node.js
const { S3Client, PutBucketTaggingCommand } = require('@aws-sdk/client-s3');
// Create an S3 client service object
const s3 = new S3Client({
region: 'default',
endpoint: 'endpoint_url',
credentials: {
accessKeyId: 'access_key',
secretAccessKey: 'secret_key',
},
});
const run = async () => {
try {
const response = await s3.send(
new PutBucketTaggingCommand({
Bucket: 'sample_bucket',
Tagging: {
TagSet: [
{
Key: 'Key1',
Value: 'Value1',
},
{
Key: 'Key2',
Value: 'Value2',
},
],
},
})
);
console.log('Success', response.CORSRules);
} catch (err) {
console.log('Error', err);
}
};
run();
{
"Key": "str", # REQUIRED
"Value": "str" # REQUIRED
}
Note that by using this function, the new tags will replace the previous tags. If you want to add new tags to the old tags, you must first get the old tags using the get_bucket_tagging request and then pass the set of new and old tags as input to that.
Limits
- The maximum number of tags for a bucket is 10.
- The value of the Key for the tags of a bucket must be unique.
- The value of the Key and the Value for each bucket must be in the form of a string.
- The length of the Key can be up to 128 Unicode characters and also the length of the Value can be up to 256 Unicode characters.
- Key and Value are case-sensitive.
Code Sample
package main
// snippet-start:[s3.go.set_cors.imports]
import (
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
// Lists Bucket Tags
//
// Usage:
// go run s3_put_bucket_tags.go BUCKET_NAME
func main() {
if len(os.Args) != 2 {
exitErrorf("Bucket name required\nUsage: go run", os.Args[0], "BUCKET")
}
bucket := os.Args[1]
sess, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials("<ACCESS_KEY>", "<SECRET_KEY>", ""),
})
svc := s3.New(sess, &aws.Config{
Region: aws.String("default"),
Endpoint: aws.String("<ENDPOINT_URL>"),
})
input := &s3.PutBucketTaggingInput{
Bucket: aws.String(bucket),
Tagging: &s3.Tagging{
TagSet: []*s3.Tag{
{
Key: aws.String("Key1"),
Value: aws.String("Value1"),
},
{
Key: aws.String("Key2"),
Value: aws.String("Value2"),
},
},
},
}
result, err := svc.PutBucketTagging(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
fmt.Println("Tags successfully added to", bucket, "bucket")
}
// snippet-start:[s3.go.set_cors.exit]
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}