This topic describes how to set tags for a bucket.
Notes
The sample code in this topic uses the region ID
cn-hangzhoufor the China (Hangzhou) region. By default, a public endpoint is used. If you want to access OSS from other Alibaba Cloud products in the same region, use an internal endpoint. For more information about the mappings between OSS regions and endpoints, see Regions and endpoints.The sample code in this topic reads access credentials from environment variables. For more information about how to configure access credentials, see Configure access credentials.
Sample code
Set bucket tags
You can use the following code to set tags for a bucket.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line parameter parser for parsing parameters from the command line.
parser = argparse.ArgumentParser(description="put bucket tags sample")
# Add the --region command-line argument, which specifies the region where the bucket is located. This is a required argument.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the name of the bucket. This is a required argument.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Add the --endpoint command-line argument, which specifies the domain name that other services use to access OSS. This is an optional argument.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
def main():
# Parse the command-line arguments.
args = parser.parse_args()
# Load credentials (AccessKey ID and AccessKey secret) from environment variables.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Load the default configurations of the SDK.
cfg = oss.config.load_default()
# Set the credentials provider.
cfg.credentials_provider = credentials_provider
# Set the region where the bucket is located.
cfg.region = args.region
# If a custom endpoint is provided by the user, set it in the configuration.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Initialize the OSS client using the configuration object.
client = oss.Client(cfg)
# Call the put_bucket_tags method to set tags for the bucket.
result = client.put_bucket_tags(
oss.PutBucketTagsRequest(
bucket=args.bucket, # Specify the name of the destination bucket.
tagging=oss.Tagging( # Construct a tag set.
tag_set=oss.TagSet( # The tag set contains multiple tags.
tags=[ # Define a list of tags.
oss.Tag( # The first tag.
key='test_key', # The tag key.
value='test_value', # The tag value.
),
oss.Tag( # The second tag.
key='test_key2', # The tag key.
value='test_value2', # The tag value.
),
],
),
),
)
)
# Print the status code and request ID of the operation result.
print(f'status code: {result.status_code}, ' # The HTTP status code, which indicates whether the request is successful.
f'request id: {result.request_id}') # The request ID, which is used to track logs and debug.
if __name__ == "__main__":
# The entry point of the program. Call the main function to execute the logic.
main()
Query bucket tags
You can use the following code to query the tags for a bucket.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line parameter parser for parsing parameters from the command line.
parser = argparse.ArgumentParser(description="get bucket tags sample")
# Add the --region command-line argument, which specifies the region where the bucket is located. This is a required argument.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the name of the bucket. This is a required argument.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Add the --endpoint command-line argument, which specifies the domain name that other services use to access OSS. This is an optional argument.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
def main():
# Parse the command-line arguments.
args = parser.parse_args()
# Load credentials (AccessKey ID and AccessKey secret) from environment variables.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Load the default configurations of the SDK.
cfg = oss.config.load_default()
# Set the credentials provider.
cfg.credentials_provider = credentials_provider
# Set the region where the bucket is located.
cfg.region = args.region
# If a custom endpoint is provided by the user, set it in the configuration.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Initialize the OSS client using the configuration object.
client = oss.Client(cfg)
# Call the get_bucket_tags method to obtain the tag information of the bucket.
result = client.get_bucket_tags(
oss.GetBucketTagsRequest(
bucket=args.bucket, # Specify the name of the destination bucket.
)
)
# Print the status code, request ID, and tag information of the operation result.
print(f'status code: {result.status_code}, ' # The HTTP status code, which indicates whether the request is successful.
f'request id: {result.request_id}, ' # The request ID, which is used to track logs and debug.
f'tagging: {result.tagging}') # Tag set information.
# If the returned tag set contains tags, print the key-value pair of each tag.
if result.tagging.tag_set.tags: # Check whether tags exist.
for r in result.tagging.tag_set.tags: # Traverse the tag set.
print(f'result: key: {r.key}, value: {r.value}') # Print the key and value of each tag.
if __name__ == "__main__":
# The entry point of the program. Call the main function to execute the logic.
main()
Delete bucket tags
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line parameter parser for parsing parameters from the command line.
parser = argparse.ArgumentParser(description="delete bucket tags sample")
# Add the --region command-line argument, which specifies the region where the bucket is located. This is a required argument.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the name of the bucket. This is a required argument.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Add the --endpoint command-line argument, which specifies the domain name that other services use to access OSS. This is an optional argument.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
def main():
# Parse the command-line arguments.
args = parser.parse_args()
# Load credentials (AccessKey ID and AccessKey secret) from environment variables.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Load the default configurations of the SDK.
cfg = oss.config.load_default()
# Set the credentials provider.
cfg.credentials_provider = credentials_provider
# Set the region where the bucket is located.
cfg.region = args.region
# If a custom endpoint is provided by the user, set it in the configuration.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Initialize the OSS client using the configuration object.
client = oss.Client(cfg)
# Call the delete_bucket_tags method to delete all tags of the bucket.
result = client.delete_bucket_tags(
oss.DeleteBucketTagsRequest(
bucket=args.bucket, # Specify the name of the destination bucket.
)
)
# Print the status code and request ID of the operation result.
print(f'status code: {result.status_code}, ' # The HTTP status code, which indicates whether the request is successful.
f'request id: {result.request_id}') # The request ID, which is used to track logs and debug.
if __name__ == "__main__":
# The entry point of the program. Call the main function to execute the logic.
main()