Object Storage Service (OSS) allows you to configure bucket tagging to classify and manage buckets. For example, you can use this feature to list only buckets that have specific tags.

Note
  • Only the bucket owner or authorized Resource Access Management (RAM) users can configure tagging for the bucket. Otherwise, 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 of a tag can be up to 64 characters in length and cannot start with http://, https://, or Aliyun. The key of a tag cannot be empty.
  • The value of a tag can be up to 128 characters in length and can be empty.
  • The key and 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 exiting tag.

Configure tags for a bucket

The following code provides an example on how to configure tags for a bucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.models import Tagging, TaggingRule

# 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 Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this region. Specify the actual endpoint based on your requirements. 
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# Create a tagging rule. 
rule = TaggingRule()
rule.add('key1', 'value1')
rule.add('key2', 'value2')

# Create a tag. 
tagging = Tagging(rule)
# Configure tags for the bucket. 
result = bucket.put_bucket_tagging(tagging)
# Query the returned HTTP status code. 
print('http status:', result.status)

Query the tags of a bucket

The following code provides an example on how to query the tags configured for a bucket:

# -*- coding: utf-8 -*-
import oss2

# 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. 
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this region. Specify the actual endpoint based on your requirements. 
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# Query the tags configured for the bucket. 
result = bucket.get_bucket_tagging()
# Query the obtained tagging rule. 
tag_rule = result.tag_set.tagging_rule
print('tag rule:', tag_rule)

List buckets that have specified tags

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

# -*- coding: utf-8 -*-
import oss2

# Create a server object. 
# 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. 
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this region. Specify the actual endpoint based on your requirements. 
service = oss2.Service(auth,  'http://oss-cn-hangzhou.aliyuncs.com')

# Add the tag-key and tag-value fields to the params parameter of the ListBuckets API operation. 
params = {}
params['tag-key'] = '<yourTagging_key>'
params['tag-value'] = '<yourTagging_value>'

# List buckets that have specified tags 
result = service.list_buckets(params=params)
# View the results of the list operation. 
for bucket in result.buckets:
    print('result bucket_name:', bucket.name)

Delete the tags of a bucket

The following code provides an example on how to delete the tags of a bucket:

# -*- coding: utf-8 -*-
import oss2

# 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. 
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this region. Specify the actual endpoint based on your requirements. 
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# Delete the tags of the bucket. 
result = bucket.delete_bucket_tagging()
# Query the returned HTTP status code. 
print('http status:', result.status)