1414from requests import Request , Session
1515from datetime import datetime
1616from six .moves .urllib .parse import quote , unquote , urlencode
17+ from six import text_type , binary_type
1718from hashlib import md5
1819from dicttoxml import dicttoxml
1920from .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
0 commit comments