You can configure bucket tagging to classify and manage buckets in Object Storage Service (OSS). For example, you can configure that only buckets with specific tags are listed in ListBucket operations.

Usage notes

When you configure tags for a bucket, take note of the following items:

  • Only the owner of a bucket and authorized Resource Access Management (RAM) users can configure tags for the bucket. If other users attempt to configure tags for the bucket, 403 Forbidden is returned with the error code AccessDenied.
  • You can configure up to 20 tags for a bucket. A tag is a key-value pair. The key and value of the tag must be UTF-8-encoded.
  • The key of a tag can be up to 64 characters in length and cannot be empty. The key of a tag cannot start with http://, https://, or Aliyun.
  • The value of a tag can be up to 128 characters in length and can be empty.
  • When you call PutBucketTags to configure a tag for a bucket, the new tag overwrites the existing tag.

Configure tags for a bucket

The following code provides an example on how to configure tags for 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 a RAM user to call API operations or perform routine O&M. 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 a tag 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 bucket tagging, see PutBucketTags.

Query the tags of a bucket

The following code provides an example on how to query the tags configured for 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.

List buckets that have specified tags

The following code provides an example on how to list buckets that have specified tags:

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 of a bucket

The following code provides an example on how to delete all tags of 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 of 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 of a bucket, see DeleteBucketTags.

Delete specified tags of a bucket

The following code provides an example on how to delete specified tags of 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 of a bucket by specifying the 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);
}