All Products
Search
Document Center

Object Storage Service:Bucket tagging

Last Updated:Nov 08, 2024

Bucket tags allow you to classify and efficiently manage Object Storage Service (OSS) buckets. For example, you can list only buckets that have specific tags when you call the ListBuckets operation.

Usage 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, endpoints and open ports.

  • 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 a bucket owner and users who are granted the oss:PutBucketTagging permission can configure tags for buckets. If other users attempt to configure tags for buckets, the 403 Forbidden message that contains the AccessDenied error code is returned.

  • You can configure up to 20 tags for a bucket.

  • The key and value of a tag must be encoded in UTF-8.

  • The key can be up to 64 characters in length and is case-sensitive. The key cannot be left empty. The key cannot start with http://, https://, or Aliyun. These prefixes are not case-sensitive.

  • The value of a tag can be up to 128 characters in length and can be left empty.

Configure tags for a bucket

The following sample code provides an example on how to configure 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 the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

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

# 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)
# Display the returned HTTP status code. 
print('http status:', result.status)

Query the tags of a bucket

The following sample code provides an example on how to query the tags of a bucket named examplebucket:

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

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

# 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

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

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

List buckets that have specific tags

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

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

# Create a server object. 
# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

service = oss2.Service(auth, endpoint, region=region)

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

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

Delete the tags of a bucket

Delete all tags of a bucket

The following sample code provides an example on how to delete all tags of a bucket named examplebucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

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

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

Delete a specific tag of a bucket

The following sample code provides an example on how to delete a specific tag of a bucket named examplebucket:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# 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 name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')

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

References

  • For the complete sample code for managing the tags of 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 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.