Object Storage Service (OSS) lets you configure tags for classification. This topic describes how to configure tags for objects using OSS SDK for Python 2.0.
Usage notes
The sample code in this topic uses the China (Hangzhou) region (Region ID:
cn-hangzhou) as an example. A public endpoint is used by default. If you want to access OSS from other Alibaba Cloud services in the same region, you must use an internal endpoint. For more information about the regions and endpoints that are supported by OSS, see Regions and endpoints.To set object tags, you must have the
oss:PutObjectTaggingpermission. For more information, see Attach a custom policy to a RAM user.
Method definition
put_object_tagging(request: PutObjectTaggingRequest, **kwargs) → PutObjectTaggingResultRequest parameters
Parameter | Type | Description |
request | PutObjectTaggingRequest | The request for the PutObjectTagging operation. For parameters in the request, see PutObjectTaggingRequest. |
Response parameters
Type | Description |
PutObjectTaggingResult | The return value. For details, see PutObjectTaggingResult. |
See put_object_tagging for the complete definition of how to configure tags.
Example
Below is the sample code for configuring tags of a specific object in a bucket:
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line parameter parser and define the parameters.
parser = argparse.ArgumentParser(description="put object tagging sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--key', help='The name of the object.', required=True)
parser.add_argument('--tag_key', help='The name of the tag key.', required=True)
parser.add_argument('--tag_value', help='The name of the tag value.', required=True)
def main():
# Parse the command-line parameters.
args = parser.parse_args()
# Load access credentials from environment variables.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Use the default configuration of the SDK.
cfg = oss.config.load_default()
# Specify the credential provider.
cfg.credentials_provider = credentials_provider
# Specify the region.
cfg.region = args.region
# Specify the endpoint if one is provided.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Create an OSS client.
client = oss.Client(cfg)
# Specify a tag.
# Add more oss.Tag objects in the following format to configure multiple tags.
# tags = [oss.Tag(key=args.tag_key, value=args.tag_value), oss.Tag(key=args.tag_key2, value=args.tag_value2)]
tags = [oss.Tag(
key=args.tag_key,
value=args.tag_value,
)]
# Configure the tags for the object.
result = client.put_object_tagging(oss.PutObjectTaggingRequest(
bucket=args.bucket,
key=args.key,
tagging=oss.Tagging(
tag_set=oss.TagSet(
tags=tags,
),
),
))
# Print the response.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' version id: {result.version_id},'
)
if __name__ == "__main__":
main()
Reference
For the complete sample code that is used to configure the tags of an object, visit put_object_tagging.py.