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

Note
  • Only the owner of a bucket and authorized 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.
  • Each tag must have a key. The key of a tag can be up to 64 characters in length and cannot start with http://, https://, or Aliyun.
  • The value of a tag can be up to 128 characters in length and can be empty.
  • The key and the value of a tag must be UTF-8-encoded.
  • When you use PutBucketTags to configure a tag for a bucket, the new tag overwrites the existing tag.

Configure bucket tags

The following code provides an example on how to configure a tag for a bucket named examplebucket:

// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
String 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 operations and maintenance. To create a RAM user, log on to the RAM console. 
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket. Example: examplebucket. 
String bucketName = "examplebucket";

// Create an OSSClient instance. 
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Configure tags for the bucket. 
SetBucketTaggingRequest request = new SetBucketTaggingRequest(bucketName);
// Specify the key and the value of the bucket tag. For example, set the key to owner and the value to John. 
request.setTag("owner", "John");
request.setTag("location", "hangzhou");
ossClient.setBucketTagging(request);

// Shut down the OSSClient instance. 
ossClient.shutdown();

Query bucket tags

The following code provides an example on how to query the tags of a bucket named examplebucket:

// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
String 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 operations and maintenance. To create a RAM user, log on to the RAM console. 
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket. Example: examplebucket. 
String bucketName = "examplebucket";

// Create an OSSClient instance. 
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Query the tags configured for the bucket. 
TagSet tagSet = ossClient.getBucketTagging(new GenericRequest(bucketName));
Map<String, String> tags = tagSet.getAllTags();
for(Map.Entry tag:tags.entrySet()){
    System.out.println("key:"+tag.getKey()+" value:"+tag.getValue());
}

// Shut down the OSSClient instance. 
ossClient.shutdown();

List buckets that have specified tags

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

// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
String 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 operations and maintenance. To create a RAM user, log on to the RAM console. 
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";

// Create an OSSClient instance. 
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// List buckets that have a specified tag. 
ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
// Specify the key and the value of the bucket tag. For example, set the key to owner and the value to John. 
listBucketsRequest.setTag("owner", "John");
BucketList bucketList = ossClient.listBuckets(listBucketsRequest);
for (Bucket o : bucketList.getBucketList()) {
  System.out.println("list result bucket: " + o.getName());
}

// Shut down the OSSClient instance. 
ossClient.shutdown();
            

Delete the tags of a bucket.

The following code provides an example on how to delete the tags of a bucket named examplebucket.

Note Before you delete the specified tags of a bucket, you must obtain the tags. Then, manually remove the tags and configure new tags for the bucket.
// Set yourEndpoint to the endpoint of the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set yourEndpoint to https://oss-cn-hangzhou.aliyuncs.com. 
String 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 operations and maintenance. To create a RAM user, log on to the RAM console. 
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// Specify the name of the bucket. Example: examplebucket. 
String bucketName = "examplebucket";

// Create an OSSClient instance. 
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

// Delete bucket tagging configurations. 
ossClient.deleteBucketTagging(new GenericRequest(bucketName));

// Shut down the OSSClient instance. 
ossClient.shutdown();