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.

  • Only the bucket owner and authorized RAM users can add tags to a bucket. Otherwise, the 403 Forbidden error is returned with the error code: AccessDenied.
  • You can add a maxito a bucketmum of 20 tags (key-value pairs) to a bucket.
  • The maximum size of a key is 64 bytes. The key of a tag cannot be null and cannot be prefixed with http://, https://, or Aliyun.
  • The maximum size of a tag value is 128 bytes. The value of a tag can be null.
  • The key and value of a tag must be UTF-8 encoded.
  • If you use PutBucketTagging to add tags to a bucket, the original tags added to the bucket are completely overwritten.

Add a tag to a bucket

You can run the following code to add a tag to a bucket:
# -*- coding: utf-8 -*-
import oss2
from oss2.models import Tagging, TaggingRule

# It is highly risky to log on with the AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM user account, log on to https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the China East 1 (Hangzhou) endpoint. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

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

# Creates a tag.
tagging = Tagging(rule)
# Adds the tag to the bucket.
result = bucket.put_bucket_tagging(tagging)
# Checks the returned HTTP status code.
print('http status:', result.status)

Obtain the tags added to a bucket

You can run the following code to obtain the tags to a bucket:
# -*- coding: utf-8 -*-
import oss2

# It is highly risky to log on with the AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM user account, log on to https://ram.console.aliyun.com.

auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the China East 1 (Hangzhou) endpoint. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# Obtains the tagging information about to a bucketsult = bucket.get_bucket_tagging()
# Views the obtained tagging rule.
tag_rule = result.tag_set.tagging_rule
print('tag rule:', tag_rule)

List buckets with a specified tag

Run the following code to list buckets with a specified tag:
# -*- coding: utf-8 -*-
import oss2

# Creates a server object.
# It is highly risky to log on with the AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM user account, log on to https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the China East 1 (Hangzhou) endpoint. Specify the actual endpoint based on your requirements.
service = oss2.Service(auth,  'http://oss-cn-hangzhou.aliyuncs.com')

# Inputs the tag-key and tag-value fields to the params parameter of the list_buckets interface.
params = {}
params['tag-key'] = '<yourTagging_key>'
params['tag-value'] = '<yourTagging_value>'

# Lists buckets with the specified tag.
result = service.list_buckets(params=params)
# Views the list results.
for bucket in result.buckets:
    print('result bucket_name:', bucket.name)

Delete the tags added to a bucket

You can run the following code to delete the tags added to a bucket:
# -*- coding: utf-8 -*-
import oss2

# It is highly risky to log on with the AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM user account, log on to https://ram.console.aliyun.com.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# This example uses the China East 1 (Hangzhou) endpoint. Specify the actual endpoint based on your requirements.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# Deletes the tags added to the bucket.
result = bucket.delete_bucket_tagging()
# Checks the returned HTTP status code.
print('http status:', result.status)