Skip to content

Commit

Permalink
Merge pull request #138 from jayzhenghan/master
Browse files Browse the repository at this point in the history
修复request超时重发导致文件流偏移异常的bug
  • Loading branch information
dt3310321 authored Sep 24, 2020
2 parents 24bc583 + 2a1cb76 commit 9fb840c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
21 changes: 20 additions & 1 deletion qcloud_cos/cos_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from requests import Request, Session
from datetime import datetime
from six.moves.urllib.parse import quote, unquote, urlencode
from six import text_type, binary_type
from hashlib import md5
from dicttoxml import dicttoxml
from .streambody import StreamBody
Expand Down Expand Up @@ -237,7 +238,12 @@ def send_request(self, method, url, bucket, timeout=30, cos_request=True, **kwar
elif bucket is not None:
kwargs['headers']['Host'] = self._conf.get_host(bucket)
kwargs['headers'] = format_values(kwargs['headers'])

file_position = None
if 'data' in kwargs:
body = kwargs['data']
if hasattr(body, 'tell') and hasattr(body, 'seek') and hasattr(body, 'read'):
file_position = body.tell() # 记录文件当前位置
kwargs['data'] = to_bytes(kwargs['data'])
if self._conf._ip is not None and self._conf._scheme == 'https':
kwargs['verify'] = False
Expand All @@ -261,7 +267,20 @@ def send_request(self, method, url, bucket, timeout=30, cos_request=True, **kwar
break
except Exception as e: # 捕获requests抛出的如timeout等客户端错误,转化为客户端错误
logger.exception('url:%s, retry_time:%d exception:%s' % (url, j, str(e)))
if j < self._retry:
can_retry = False
if 'data' in kwargs:
body = kwargs[data]
if hasattr(body, 'tell') and hasattr(body, 'seek') and hasattr(body, 'read'):
can_retry = True
elif isinstance(body, text_type) or isinstance(body, binary_type):
can_retry = True

if j < self._retry and can_retry:
if file_position is not None:
try:
kwargs['data'].seek(file_position)
except IOError as ioe:
raise CosClientError(str(ioe))
continue
raise CosClientError(str(e))

Expand Down
4 changes: 2 additions & 2 deletions qcloud_cos/streambody.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ def pget_stream_to_file(self, fdst, offset, expected_len, auto_decompress=False)
"""保存流到本地文件的offset偏移"""
self._read_len = 0
fdst.seek(offset, 0)

chunk_size = 1024 * 1024
while 1:
chunk = self.read(1024, auto_decompress)
chunk = self.read(chunk_size, auto_decompress)
if not chunk:
break
self._read_len += len(chunk)
Expand Down
2 changes: 1 addition & 1 deletion qcloud_cos/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

__version__ = '5.1.8.5'
__version__ = '5.1.8.6'
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def long_description():

setup(
name='cos-python-sdk-v5',
version='1.8.5',
version='1.8.6',
url='https://www.qcloud.com/',
license='MIT',
author='tiedu, lewzylu, channingliu',
Expand Down

0 comments on commit 9fb840c

Please sign in to comment.