Skip to content

Commit

Permalink
Merge pull request #48 from aliyun/fix_upload_part_headers
Browse files Browse the repository at this point in the history
add headers to upload_part()
  • Loading branch information
yami authored Nov 14, 2016
2 parents 1688e1c + 6bb05a0 commit e2aaf4a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ OSS SDK for Python 版本记录

Python SDK的版本号遵循 `Semantic Versioning <http://semver.org/>`_ 规则。

Version 2.2.2
-------------

- 修复:upload_part接口加上headers参数


Version 2.2.1
-------------

Expand Down
2 changes: 1 addition & 1 deletion oss2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '2.2.1'
__version__ = '2.2.2'

from . import models, exceptions

Expand Down
6 changes: 5 additions & 1 deletion oss2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ def init_multipart_upload(self, key, headers=None):
resp = self.__do_object('POST', key, params={'uploads': ''}, headers=headers)
return self._parse_result(resp, xml_utils.parse_init_multipart_upload, InitMultipartUploadResult)

def upload_part(self, key, upload_id, part_number, data, progress_callback=None):
def upload_part(self, key, upload_id, part_number, data, progress_callback=None, headers=None):
"""上传一个分片。
:param str key: 待上传文件名,这个文件名要和 :func:`init_multipart_upload` 的文件名一致。
Expand All @@ -644,6 +644,9 @@ def upload_part(self, key, upload_id, part_number, data, progress_callback=None)
:param data: 待上传数据。
:param progress_callback: 用户指定进度回调函数。可以用来实现进度条等功能。参考 :ref:`progress_callback` 。
:param headers: 用户指定的HTTP头部。可以指定Content-MD5头部等
:type headers: 可以是dict,建议是oss2.CaseInsensitiveDict
:return: :class:`PutObjectResult <oss2.models.PutObjectResult>`
"""
if progress_callback:
Expand All @@ -654,6 +657,7 @@ def upload_part(self, key, upload_id, part_number, data, progress_callback=None)

resp = self.__do_object('PUT', key,
params={'uploadId': upload_id, 'partNumber': str(part_number)},
headers=headers,
data=data)
result = PutObjectResult(resp)

Expand Down
6 changes: 6 additions & 0 deletions oss2/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
OSS_REQUEST_ERROR_STATUS = -2
OSS_INCONSISTENT_ERROR_STATUS = -3


class OssError(Exception):
def __init__(self, status, headers, body, details):
#: HTTP 状态码
Expand Down Expand Up @@ -104,6 +105,11 @@ def __init__(self, status, headers, body, details):
self.value = details.get('ArgumentValue')


class InvalidDigest(ServerError):
status = 400
code = 'InvalidDigest'


class InvalidObjectName(ServerError):
status = 400
code = 'InvalidObjectName'
Expand Down
27 changes: 25 additions & 2 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@


class TestMultipart(OssTestCase):
def test_multipart(self):
def do_multipart_internal(self, do_md5):
key = self.random_key()
content = random_bytes(128 * 1024)

parts = []
upload_id = self.bucket.init_multipart_upload(key).upload_id

result = self.bucket.upload_part(key, upload_id, 1, content)
if do_md5:
headers = {'Content-Md5': oss2.utils.content_md5(content)}
else:
headers = None

result = self.bucket.upload_part(key, upload_id, 1, content, headers=headers)
parts.append(oss2.models.PartInfo(1, result.etag))
self.assertTrue(result.crc is not None)

Expand All @@ -23,6 +28,24 @@ def test_multipart(self):
result = self.bucket.get_object(key)
self.assertEqual(content, result.read())

def test_multipart(self):
self.do_multipart_internal(False)

def test_upload_part_content_md5_good(self):
self.do_multipart_internal(True)

def test_upload_part_content_md5_bad(self):
key = self.random_key()
content = random_bytes(128 * 1024)

parts = []
upload_id = self.bucket.init_multipart_upload(key).upload_id

# construct a bad Content-Md5 by using 'content + content's Content-Md5
headers = {'Content-Md5': oss2.utils.content_md5(content + content)}

self.assertRaises(oss2.exceptions.InvalidDigest, self.bucket.upload_part, key, upload_id, 1, content, headers=headers)

def test_progress(self):
stats = {'previous': -1}

Expand Down

0 comments on commit e2aaf4a

Please sign in to comment.