By using the bucket tagging function, you can classify your buckets to manage them. For example, you can specify that only buckets with specified tags are returned in ListBucket operations.

Usage notes

Note the following items when you use bucket tagging:

  • Only the bucket owner and authorized RAM users can configure tagging for a bucket. If other users attempt to configure tagging for the bucket, 403 Forbidden is returned with the AccessDenied error code.
  • A tag is a key-value pair. You can configure up to 20 tags for a bucket. The key and value of a tag must be encoded in UTF-8.
  • The key of a tag can be a maximum of 64 bytes in length. It cannot be null and cannot start with http://, https://, or Aliyun.
  • The value of a tag can be a maximum of 128 bytes in length and can be null.
  • When you use PutBucketTags to configure a tag for a bucket, the new tag overwrites the exiting tag.

Configure tags for a bucket

You can run the following code to configure tags to a bucket:

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
// Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // Configure tags for the bucket.
    var setRequest = new SetBucketTaggingRequest(bucketName);

    var tag1 = new Tag
    {
        Key = "project",
        Value = "projectone"
    };

    var tag2 = new Tag
    {
        Key = "user",
        Value = "jsmith"
    };

    setRequest.AddTag(tag1);
    setRequest.AddTag(tag2);    
    client.SetBucketTagging(setRequest);
    Console.WriteLine("Set bucket:{0} Tagging succeeded ", bucketName);
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
    Console.WriteLine("Failed with error info: {0}", ex.Message);
}

For more information about how to configure tags for a bucket, see PutBucketTags.

Query the tags of bucket

You can run the following code to query the tags to a bucket:

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    //Query the tags of a bucket.
    var result = client.GetBucketTagging(bucketName);
    Console.WriteLine("Get bucket:{0} tagging succeeded ", bucketName);

    for (var i = 0; i < result.Tags.Count; i++) {
        Console.WriteLine("bucket tagging key: {0}; bucket tagging value: {1}", result.Tags[i].Key, result.Tags[i].Value);
    }
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
    Console.WriteLine("Failed with error info: {0}", ex.Message);
}

For more information about how to query the tags of a bucket, see GetBucketTags.

Lists buckets that have specified tags

You can run the following code to list buckets with a specified tag:

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // List buckets that have a specified tag.
    var tag = new Tag
    {
        Key = "tag",
        Value = "value"
    };
    var listRequest = new ListBucketsRequest();
    listRequest.Tag = tag;
    var result = client.ListBuckets(listRequest);
    Console.WriteLine("list bucket:{0} succeeded ", bucketName);  
    foreach (var bucket in result.Buckets) {
        Console.WriteLine("bucket name: {0}", bucket.Name);
    }
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
    Console.WriteLine("Failed with error info: {0}", ex.Message);
}

Delete all tags from a bucket

The following code provides an example on how to delete all tags from a bucket:

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // Delete all tags from the bucket.
    client.DeleteBucketTagging(bucketName);
    Console.WriteLine("Delete bucket:{0} Tagging succeeded ", bucketName);
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
    Console.WriteLine("Failed with error info: {0}", ex.Message);
}

For more information about how to delete tags from a bucket, see DeleteBucketTags.

Delete specified tags from a bucket

The following code provides an example on how to delete specified tags from a bucket:

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";

// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    var tag = new Tag
    {
        Key = "tag1",
        Value = "value1"
    };
    var tags = new List<Tag>();
       tags.Add(tag);
    // Delete specified tags from a bucket by specifying tag keys.
    var request = new DeleteBucketTaggingRequest(bucketName, tags);
    var result = client.DeleteBucketTagging(request);
    Console.WriteLine("delete bucket:{0} Tagging by key succeeded ", bucketName);
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}