Skip to content

Commit 9fb840c

Browse files
authored
Merge pull request #138 from jayzhenghan/master
修复request超时重发导致文件流偏移异常的bug
2 parents 24bc583 + 2a1cb76 commit 9fb840c

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

qcloud_cos/cos_client.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from requests import Request, Session
1515
from datetime import datetime
1616
from six.moves.urllib.parse import quote, unquote, urlencode
17+
from six import text_type, binary_type
1718
from hashlib import md5
1819
from dicttoxml import dicttoxml
1920
from .streambody import StreamBody
@@ -237,7 +238,12 @@ def send_request(self, method, url, bucket, timeout=30, cos_request=True, **kwar
237238
elif bucket is not None:
238239
kwargs['headers']['Host'] = self._conf.get_host(bucket)
239240
kwargs['headers'] = format_values(kwargs['headers'])
241+
242+
file_position = None
240243
if 'data' in kwargs:
244+
body = kwargs['data']
245+
if hasattr(body, 'tell') and hasattr(body, 'seek') and hasattr(body, 'read'):
246+
file_position = body.tell() # 记录文件当前位置
241247
kwargs['data'] = to_bytes(kwargs['data'])
242248
if self._conf._ip is not None and self._conf._scheme == 'https':
243249
kwargs['verify'] = False
@@ -261,7 +267,20 @@ def send_request(self, method, url, bucket, timeout=30, cos_request=True, **kwar
261267
break
262268
except Exception as e: # 捕获requests抛出的如timeout等客户端错误,转化为客户端错误
263269
logger.exception('url:%s, retry_time:%d exception:%s' % (url, j, str(e)))
264-
if j < self._retry:
270+
can_retry = False
271+
if 'data' in kwargs:
272+
body = kwargs[data]
273+
if hasattr(body, 'tell') and hasattr(body, 'seek') and hasattr(body, 'read'):
274+
can_retry = True
275+
elif isinstance(body, text_type) or isinstance(body, binary_type):
276+
can_retry = True
277+
278+
if j < self._retry and can_retry:
279+
if file_position is not None:
280+
try:
281+
kwargs['data'].seek(file_position)
282+
except IOError as ioe:
283+
raise CosClientError(str(ioe))
265284
continue
266285
raise CosClientError(str(e))
267286

qcloud_cos/streambody.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ def pget_stream_to_file(self, fdst, offset, expected_len, auto_decompress=False)
7070
"""保存流到本地文件的offset偏移"""
7171
self._read_len = 0
7272
fdst.seek(offset, 0)
73-
73+
chunk_size = 1024 * 1024
7474
while 1:
75-
chunk = self.read(1024, auto_decompress)
75+
chunk = self.read(chunk_size, auto_decompress)
7676
if not chunk:
7777
break
7878
self._read_len += len(chunk)

qcloud_cos/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11

2-
__version__ = '5.1.8.5'
2+
__version__ = '5.1.8.6'

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def long_description():
1616

1717
setup(
1818
name='cos-python-sdk-v5',
19-
version='1.8.5',
19+
version='1.8.6',
2020
url='https://www.qcloud.com/',
2121
license='MIT',
2222
author='tiedu, lewzylu, channingliu',

0 commit comments

Comments
 (0)