Skip to content

Commit

Permalink
Add EnableMD5 for put object
Browse files Browse the repository at this point in the history
  • Loading branch information
dt3310321 committed Mar 29, 2018
1 parent 6ee2c07 commit 17e406a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
7 changes: 6 additions & 1 deletion qcloud_cos/cos_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,13 @@ def send_request(self, method, url, timeout=30, **kwargs):
return None

# s3 object interface begin
def put_object(self, Bucket, Body, Key, **kwargs):
def put_object(self, Bucket, Body, Key, EnableMD5=False, **kwargs):
"""单文件上传接口,适用于小文件,最大不得超过5GB
:param Bucket(string): 存储桶名称.
:param Body(file|string): 上传的文件内容,类型为文件流或字节流.
:param Key(string): COS路径.
:param EnableMD5(bool): 是否需要SDK计算Content-MD5,打开此开关会增加上传耗时.
:kwargs(dict): 设置上传的headers.
:return(dict): 上传成功返回的结果,包含ETag等信息.
Expand Down Expand Up @@ -223,6 +224,10 @@ def put_object(self, Bucket, Body, Key, **kwargs):
url=url,
headers=headers))
Body = deal_with_empty_file_stream(Body)
if EnableMD5:
md5_str = get_content_md5(Body)
if md5_str:
headers['Content-MD5'] = md5_str
rt = self.send_request(
method='PUT',
url=url,
Expand Down
16 changes: 16 additions & 0 deletions qcloud_cos/cos_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ def get_md5(data):
return MD5


def get_content_md5(body):
body_type = type(body)
if body_type == str:
return get_md5(body)
elif body_type == file:
if hasattr(body, 'tell') and hasattr(body, 'seek') and hasattr(body, 'read'):
file_position = body.tell() # 记录文件当前位置
md5_str = get_md5(body.read())
body.seek(file_position) # 恢复初始的文件位置
return md5_str
else:
raise CosClientError('can not get md5 digest for file without necessary attrs, including tell, seek and read')
return None


def dict_to_xml(data):
"""V5使用xml格式,将输入的dict转换为xml"""
doc = xml.dom.minidom.Document()
Expand Down Expand Up @@ -105,6 +120,7 @@ def xml_to_dict(data, origin_str="", replace_str=""):
xmldict = Xml2Dict(root)
xmlstr = str(xmldict)
xmlstr = xmlstr.replace("{http://www.qcloud.com/document/product/436/7751}", "")
xmlstr = xmlstr.replace("{https://cloud.tencent.com/document/product/436}", "")
xmlstr = xmlstr.replace("{http://doc.s3.amazonaws.com/2006-03-01}", "")
xmlstr = xmlstr.replace("{http://www.w3.org/2001/XMLSchema-instance}", "")
if origin_str:
Expand Down
23 changes: 23 additions & 0 deletions ut/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,12 +645,35 @@ def test_put_get_bucket_logging():
response = logging_client.get_bucket_logging(
Bucket=logging_bucket
)
print response
assert response['LoggingEnabled']['TargetBucket'] == logging_bucket
assert response['LoggingEnabled']['TargetPrefix'] == 'test'


def test_put_object_enable_md5():
"""上传文件,SDK计算content-md5头部"""
file_size = 10
file_name = 'test_object_sdk_caculate_md5.file'
gen_file(file_name, 10)
with open(file_name, 'rb') as f:
etag = get_raw_md5(f.read())
with open(file_name, 'rb') as fp:
put_response = client.put_object(
Bucket=test_bucket,
Body=fp,
Key=file_name,
EnableMD5=True,
CacheControl='no-cache',
ContentDisposition='download.txt'
)
assert etag == put_response['Etag']
if os.path.exists(file_name):
os.remove(file_name)


if __name__ == "__main__":
setUp()
test_put_object_enable_md5()
test_upload_with_server_side_encryption()
test_upload_empty_file()
test_put_get_delete_object_10MB()
Expand Down

0 comments on commit 17e406a

Please sign in to comment.