OSS lets you attach key-value tags to objects for classification and lifecycle management. This document describes how to set object tags using OSS SDK for Python V2.
Prerequisites
Before you begin, ensure that you have:
The
oss:PutObjectTaggingpermission. For details, see Attach a custom policy to a RAM user
Usage notes
The sample code uses the China (Hangzhou) region (cn-hangzhou) and a public endpoint by default. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For a list of supported regions and endpoints, see Regions and endpoints.
Method definition
put_object_tagging(request: PutObjectTaggingRequest, **kwargs) -> PutObjectTaggingResultRequest parameters
| Parameter | Type | Description |
|---|---|---|
request | PutObjectTaggingRequest | The request object. For all fields, see PutObjectTaggingRequest. |
Response
| Type | Description |
|---|---|
PutObjectTaggingResult | The result object. For all fields, see PutObjectTaggingResult. |
For the complete method definition, see put_object_tagging.
Example
The following example sets one tag on an object. To set multiple tags, add more oss.Tag objects to the tags list.
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 credentials 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)
# Define the tags to set.
# To set multiple tags, add more oss.Tag objects:
# 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,
)]
# Set the tags on 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()References
Complete sample code: put_object_tagging.py