Skip to content

Commit 3a16f3b

Browse files
author
libertyzhu
committed
上传对象时设置标签
1 parent d63cf5e commit 3a16f3b

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

qcloud_cos/cos_comm.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
'ResponseContentEncoding': 'response-content-encoding',
3838
'Metadata': 'Metadata',
3939
'ACL': 'x-cos-acl',
40+
'Tagging': 'x-cos-tagging',
4041
'GrantFullControl': 'x-cos-grant-full-control',
4142
'GrantWrite': 'x-cos-grant-write',
4243
'GrantRead': 'x-cos-grant-read',

ut/test.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6624,6 +6624,46 @@ def test_head_exception():
66246624
assert 403 == e.get_status_code()
66256625

66266626

6627+
def test_put_object_with_tagging():
6628+
"""上传对象时设置标签"""
6629+
key = 'tagging.obj'
6630+
# 设置单个tag: key=value
6631+
client.put_object(Bucket=test_bucket, Key=key, Body=b'hello', Tagging='A=B')
6632+
resp = client.get_object_tagging(Bucket=test_bucket, Key=key)
6633+
print(resp)
6634+
tag = resp['TagSet']['Tag']
6635+
assert tag[0]['Key'] == 'A'
6636+
assert tag[0]['Value'] == 'B'
6637+
resp = client.delete_object(Bucket=test_bucket, Key=key)
6638+
# 设置多个tag: key1=value1&key2=value2
6639+
client.put_object(Bucket=test_bucket, Key=key, Body=b'hello', Tagging='tagKey1=tagValue1&tagKey2=tagValue2')
6640+
resp = client.get_object_tagging(Bucket=test_bucket, Key=key)
6641+
print(resp)
6642+
tag = resp['TagSet']['Tag']
6643+
assert tag[0]['Key'] == 'tagKey1'
6644+
assert tag[0]['Value'] == 'tagValue1'
6645+
assert tag[1]['Key'] == 'tagKey2'
6646+
assert tag[1]['Value'] == 'tagValue2'
6647+
resp = client.delete_object(Bucket=test_bucket, Key=key)
6648+
6649+
# 高级接口upload_file
6650+
filename = 'BIG_20M'
6651+
gen_file(filename, 20)
6652+
6653+
resp = client.upload_file(Bucket=test_bucket, Key=key, LocalFilePath=filename, PartSize=1, Tagging='A=B')
6654+
print(resp)
6655+
resp = client.get_object_tagging(Bucket=test_bucket, Key=key)
6656+
print(resp)
6657+
tag = resp['TagSet']['Tag']
6658+
assert tag[0]['Key'] == 'A'
6659+
assert tag[0]['Value'] == 'B'
6660+
resp = client.delete_object(Bucket=test_bucket, Key=key)
6661+
6662+
if os.path.exists(filename):
6663+
os.remove(filename)
6664+
6665+
6666+
66276667
if __name__ == "__main__":
66286668
setUp()
66296669
"""

0 commit comments

Comments
 (0)