All Products
Search
Document Center

Object Storage Service:Bucket tagging (Python SDK V1)

Last Updated:Feb 28, 2026

Bucket tagging categorizes and manages buckets. For example, list only buckets with specific tags.

Notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials using OSS SDK for Python 1.0.

  • This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Initialization.

  • Only the bucket owner and users granted the oss:PutBucketTagging permission can set tags. Otherwise, a 403 Forbidden error with error code AccessDenied is returned.

  • Tag constraints:

    ConstraintLimit
    Maximum tags per bucket20 key-value pairs
    EncodingUTF-8
    Key lengthUp to 64 characters, case-sensitive, cannot be empty
    Key prefix restrictionCannot start with http://, https://, or Aliyun (case-insensitive)
    Value lengthUp to 128 characters, can be empty

Set bucket tags

The following example sets tags for a bucket named examplebucket.

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

# Obtain access credentials from environment variables. Before running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the region that corresponds to the endpoint, such as cn-hangzhou. This parameter is required for V4 signatures.
region = "cn-hangzhou"

# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

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

# Create tags.
tagging = Tagging(rule)
# Set the bucket tags.
result = bucket.put_bucket_tagging(tagging)
# View the HTTP return code.
print('http status:', result.status)

Get bucket tags

The following example retrieves the tags of a bucket named examplebucket.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the region that corresponds to the endpoint, such as cn-hangzhou. This parameter is required for V4 signatures.
region = "cn-hangzhou"

# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Get the bucket tags.
result = bucket.get_bucket_tagging()
# View the retrieved tagging rule.
tag_rule = result.tag_set.tagging_rule
print('tag rule:', tag_rule)

List buckets that have specified tags

The following example lists buckets with specified tags. This operation requires a Service object instead of a Bucket instance.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the region that corresponds to the endpoint, such as cn-hangzhou. This parameter is required for V4 signatures.
region = "cn-hangzhou"

# Create a Service object.
service = oss2.Service(auth, endpoint, region=region)

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

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

Delete bucket tags

Delete all tags from a bucket

The following example deletes all tags from a bucket named examplebucket.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the region that corresponds to the endpoint, such as cn-hangzhou. This parameter is required for V4 signatures.
region = "cn-hangzhou"

# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Delete the bucket tags.
result = bucket.delete_bucket_tagging()
# View the HTTP return code.
print('http status:', result.status)

Delete specified tags from a bucket

The following example deletes specified tags from a bucket named examplebucket.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Obtain access credentials from environment variables. Before running this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the region that corresponds to the endpoint, such as cn-hangzhou. This parameter is required for V4 signatures.
region = "cn-hangzhou"

# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

params = dict()
# Delete the tag whose key is key1.
params['tagging'] = "key1"
# Delete the specified bucket tags.
result = bucket.delete_bucket_tagging(params=params)
# View the HTTP return code.
print('http status:', result.status)

References

  • For the complete sample code for bucket tagging, see GitHub example.

  • For more information about the API operation to set bucket tags, see PutBucketTags.

  • For more information about the API operation to get bucket tags, see GetBucketTags.

  • For more information about the API operation to delete bucket tags, see DeleteBucketTags.