1+ import argparse
2+ import base64
3+ import hashlib
4+ from requests .structures import CaseInsensitiveDict
5+ from alibabacloud_oss_v2 import OperationInput
6+ import alibabacloud_oss_v2 as oss
7+
8+ parser = argparse .ArgumentParser (description = "invoke operation bucket tags sample" )
9+ parser .add_argument ('--region' , help = 'The region in which the bucket is located.' , required = True )
10+ parser .add_argument ('--bucket' , help = 'The name of the bucket.' , required = True )
11+ parser .add_argument ('--endpoint' , help = 'The domain names that other services can use to access OSS' )
12+
13+
14+ def main ():
15+
16+ args = parser .parse_args ()
17+
18+ # Loading credentials values from the environment variables
19+ credentials_provider = oss .credentials .EnvironmentVariableCredentialsProvider ()
20+
21+ # Using the SDK's default configuration
22+ cfg = oss .config .load_default ()
23+ cfg .credentials_provider = credentials_provider
24+ cfg .region = args .region
25+ if args .endpoint is not None :
26+ cfg .endpoint = args .endpoint
27+
28+ client = oss .Client (cfg )
29+
30+
31+ xml_data = r'''
32+ <?xml version="1.0" encoding="UTF-8"?>
33+ <Tagging>
34+ <TagSet>
35+ <Tag>
36+ <Key>testa</Key>
37+ <Value>value1-test</Value>
38+ </Tag>
39+ <Tag>
40+ <Key>testb</Key>
41+ <Value>value2-test</Value>
42+ </Tag>
43+ </TagSet>
44+ </Tagging>
45+ '''
46+ h = hashlib .md5 ()
47+ h .update (xml_data .encode ())
48+ md5 = base64 .b64encode (h .digest ()).decode ()
49+
50+ op_input = OperationInput (
51+ op_name = 'PutBucketTags' ,
52+ method = 'PUT' ,
53+ headers = CaseInsensitiveDict ({
54+ 'Content-Type' : 'application/xml' ,
55+ 'Content-MD5' : md5 ,
56+ }),
57+ parameters = {
58+ 'tagging' : '' ,
59+ },
60+ bucket = args .bucket ,
61+ op_metadata = {'sub-resource' : ['tagging' ]},
62+ body = xml_data .encode (),
63+ )
64+
65+ op_output = client .invoke_operation (op_input )
66+
67+ print (f'status code: { op_output .status_code } ,'
68+ f' request id: { op_output .headers .get ("x-oss-request-id" , "" )} ,'
69+ )
70+
71+
72+ # get bucket tags
73+ op_input = OperationInput (
74+ op_name = 'GetBucketTags' ,
75+ method = 'GET' ,
76+ parameters = {
77+ 'tagging' : '' ,
78+ },
79+ bucket = args .bucket ,
80+ op_metadata = {'sub-resource' : ['tagging' ]},
81+ )
82+
83+ op_output = client .invoke_operation (op_input )
84+
85+ print (f'status code: { op_output .status_code } ,'
86+ f' request id: { op_output .headers .get ("x-oss-request-id" , "" )} ,'
87+ f' content: { op_output .http_response .content } ,'
88+ )
89+
90+
91+ # delete bucket tags
92+ op_input = OperationInput (
93+ op_name = 'DeleteBucketTags' ,
94+ method = 'DELETE' ,
95+ parameters = {
96+ 'tagging' : '' ,
97+ },
98+ bucket = args .bucket ,
99+ op_metadata = {'sub-resource' : ['tagging' ]},
100+ )
101+
102+ op_output = client .invoke_operation (op_input )
103+
104+ print (f'status code: { op_output .status_code } ,'
105+ f' request id: { op_output .headers .get ("x-oss-request-id" , "" )} ,'
106+ )
107+
108+
109+
110+ if __name__ == "__main__" :
111+ main ()
0 commit comments