You can use tags to identify buckets used for different purposes and manage these buckets.

Usage notes

The bucket tagging feature uses a key-value pair to identify a bucket. You can add tags to buckets that are used for different purposes and manage the buckets by tags.
  • Only the bucket owner and users who are granted the oss:PutBucketTagging permission can add tags to the bucket or modify the tags of the bucket. If other users try to add tags to the bucket or modify the tags of the bucket, 403 Forbidden is returned with the AccessDenied error code.
  • You can configure up to 20 tags for a bucket.
  • The key and value of a tag must be UTF-8-encoded.
  • The key of a tag can be up to 64 characters in length and cannot start with http://, https://, or Aliyun. In addition, 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.

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 access Object Storage Service (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')
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the bucket name. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# Create tagging rules for the bucket. 
rule = TaggingRule()
rule.add('key1', 'value1')
rule.add('key2', 'value2')

# Create tags for the bucket. 
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 tags of a bucket

The following code provides an example on how to query 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 access 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')
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the bucket name. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

# Query tags of 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 specific tags

The following code provides an example on how to list buckets that have specific 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 access 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')
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
service = oss2.Service(auth,  'https://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 specific tags 
result = service.list_buckets(params=params)
# Query the results of the list operation. 
for bucket in result.buckets:
    print('result bucket_name:', bucket.name)

Delete tags of a bucket

The following code provides an example on how to delete 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 access 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')
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the bucket name. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

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

References

  • For the complete sample code that is used to configure tags for a bucket, visit GitHub.
  • For more information about the API operation that you can call to configure tags for a bucket, see PutBucketTags.
  • For more information about the API operation that you can call to query tags of a bucket, see GetBucketTags.
  • For more information about the API operation that you can call to delete tags of a bucket, see DeleteBucketTags.