14
14
from requests import Request , Session
15
15
from datetime import datetime
16
16
from six .moves .urllib .parse import quote , unquote , urlencode
17
+ from six import text_type , binary_type
17
18
from hashlib import md5
18
19
from dicttoxml import dicttoxml
19
20
from .streambody import StreamBody
@@ -237,7 +238,12 @@ def send_request(self, method, url, bucket, timeout=30, cos_request=True, **kwar
237
238
elif bucket is not None :
238
239
kwargs ['headers' ]['Host' ] = self ._conf .get_host (bucket )
239
240
kwargs ['headers' ] = format_values (kwargs ['headers' ])
241
+
242
+ file_position = None
240
243
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 () # 记录文件当前位置
241
247
kwargs ['data' ] = to_bytes (kwargs ['data' ])
242
248
if self ._conf ._ip is not None and self ._conf ._scheme == 'https' :
243
249
kwargs ['verify' ] = False
@@ -261,7 +267,20 @@ def send_request(self, method, url, bucket, timeout=30, cos_request=True, **kwar
261
267
break
262
268
except Exception as e : # 捕获requests抛出的如timeout等客户端错误,转化为客户端错误
263
269
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 ))
265
284
continue
266
285
raise CosClientError (str (e ))
267
286
0 commit comments