@@ -43,7 +43,8 @@ class CosConfig(object):
43
43
def __init__ (self , Appid = None , Region = None , SecretId = None , SecretKey = None , Token = None , CredentialInstance = None , Scheme = None , Timeout = None ,
44
44
Access_id = None , Access_key = None , Secret_id = None , Secret_key = None , Endpoint = None , IP = None , Port = None ,
45
45
Anonymous = None , UA = None , Proxies = None , Domain = None , ServiceDomain = None , KeepAlive = True , PoolConnections = 10 ,
46
- PoolMaxSize = 10 , AllowRedirects = False , SignHost = True , EndpointCi = None , EndpointPic = None , EnableOldDomain = True , EnableInternalDomain = True , SignParams = True ):
46
+ PoolMaxSize = 10 , AllowRedirects = False , SignHost = True , EndpointCi = None , EndpointPic = None , EnableOldDomain = True , EnableInternalDomain = True , SignParams = True ,
47
+ AutoSwitchDomainOnRetry = True ):
47
48
"""初始化,保存用户的信息
48
49
49
50
:param Appid(string): 用户APPID.
@@ -74,6 +75,7 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token
74
75
:param EnableOldDomain(bool): 是否使用旧的myqcloud.com域名访问COS
75
76
:param EnableInternalDomain(bool): 是否使用内网域名访问COS
76
77
:param SignParams(bool): 是否将请求参数算入签名
78
+ :param AutoSwitchDomainOnRetry(bool): 重试请求时是否自动切换域名
77
79
"""
78
80
self ._appid = to_unicode (Appid )
79
81
self ._token = to_unicode (Token )
@@ -98,6 +100,7 @@ def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token
98
100
self ._enable_old_domain = EnableOldDomain
99
101
self ._enable_internal_domain = EnableInternalDomain
100
102
self ._sign_params = SignParams
103
+ self ._auto_switch_domain_on_retry = AutoSwitchDomainOnRetry
101
104
102
105
if self ._domain is None :
103
106
self ._endpoint = format_endpoint (Endpoint , Region , u'cos.' , EnableOldDomain , EnableInternalDomain )
@@ -347,6 +350,7 @@ def send_request(self, method, url, bucket, timeout=30, cos_request=True, ci_req
347
350
kwargs ['headers' ] = format_values (kwargs ['headers' ])
348
351
349
352
file_position = None
353
+ domain_switched = False # 重试时如果要切换域名, 只切换一次
350
354
if 'data' in kwargs :
351
355
body = kwargs ['data' ]
352
356
if hasattr (body , 'tell' ) and hasattr (body , 'seek' ) and hasattr (body , 'read' ):
@@ -376,13 +380,25 @@ def send_request(self, method, url, bucket, timeout=30, cos_request=True, ci_req
376
380
break
377
381
else :
378
382
if j < self ._retry and client_can_retry (file_position , ** kwargs ):
383
+ if not domain_switched and self ._conf ._auto_switch_domain_on_retry and self ._conf ._ip is None :
384
+ # 重试时切换域名
385
+ logger .debug ("switch hostname, url before: " + url )
386
+ url = switch_hostname_for_url (url )
387
+ domain_switched = True
388
+ logger .debug ("switch hostname, url after: " + url )
379
389
continue
380
390
else :
381
391
break
382
392
except Exception as e : # 捕获requests抛出的如timeout等客户端错误,转化为客户端错误
383
393
logger .exception ('url:%s, retry_time:%d exception:%s' % (url , j , str (e )))
384
394
if j < self ._retry and (isinstance (e , ConnectionError ) or isinstance (e , Timeout )): # 只重试网络错误
385
395
if client_can_retry (file_position , ** kwargs ):
396
+ if not domain_switched and self ._conf ._auto_switch_domain_on_retry and self ._conf ._ip is None :
397
+ # 重试时切换域名
398
+ logger .debug ("switch hostname, url before: " + url )
399
+ url = switch_hostname_for_url (url )
400
+ domain_switched = True
401
+ logger .debug ("switch hostname, url after: " + url )
386
402
continue
387
403
raise CosClientError (str (e ))
388
404
@@ -3447,7 +3463,7 @@ def _upload_part(self, bucket, key, local_path, offset, size, part_num, uploadid
3447
3463
fp .seek (offset , 0 )
3448
3464
data = fp .read (size )
3449
3465
rt = self .upload_part (bucket , key , data , part_num , uploadid , enable_md5 , ** kwargs )
3450
- lower_rt = { k .lower (): v for k ,v in rt .items () }
3466
+ lower_rt = dict ([( k .lower (), v ) for k , v in rt .items ()])
3451
3467
md5_lst .append ({'PartNumber' : part_num , 'ETag' : lower_rt ['etag' ]})
3452
3468
if progress_callback :
3453
3469
progress_callback .report (size )
0 commit comments