@@ -179,18 +179,21 @@ def format_region(region):
179
179
180
180
181
181
def format_bucket (bucket , appid ):
182
- """去除bucket结尾含有的appid """
182
+ """兼容新老bucket长短命名,appid为空默认为长命名,appid不为空则认为是短命名 """
183
183
if not isinstance (bucket , str ):
184
184
raise CosClientError ("bucket is not str" )
185
+ # appid为空直接返回bucket
186
+ if appid == "" :
187
+ return bucket
188
+ # appid不为空,检查是否以-appid结尾
185
189
if bucket .endswith ("-" + appid ):
186
- index = bucket .find ("-" + appid )
187
- return bucket [0 :index ]
188
- return bucket
190
+ return bucket
191
+ return bucket + "-" + appid
189
192
190
193
191
194
class CosConfig (object ):
192
195
"""config类,保存用户相关信息"""
193
- def __init__ (self , Appid , Region , Access_id , Access_key , Scheme = 'http' , Token = None ):
196
+ def __init__ (self , Region , Access_id , Access_key , Appid = '' , Scheme = 'http' , Token = None , Timeout = None ):
194
197
"""初始化,保存用户的信息
195
198
196
199
:param Appid(string): 用户APPID.
@@ -199,13 +202,15 @@ def __init__(self, Appid, Region, Access_id, Access_key, Scheme='http', Token=No
199
202
:param Access_key(string): 秘钥SecretKey.
200
203
:param Scheme(string): http/https.
201
204
:param Token(string): 临时秘钥使用的token.
205
+ :param Timeout(int): http超时时间.
202
206
"""
203
207
self ._appid = Appid
204
208
self ._region = format_region (Region )
205
209
self ._access_id = Access_id
206
210
self ._access_key = Access_key
207
211
self ._scheme = Scheme
208
212
self ._token = Token
213
+ self ._timeout = Timeout
209
214
logger .info ("config parameter-> appid: {appid}, region: {region}" .format (
210
215
appid = Appid ,
211
216
region = Region ))
@@ -221,18 +226,16 @@ def uri(self, bucket, path=None):
221
226
if path :
222
227
if path [0 ] == '/' :
223
228
path = path [1 :]
224
- url = u"{scheme}://{bucket}-{uid} .{region}.myqcloud.com/{path}" .format (
229
+ url = u"{scheme}://{bucket}.{region}.myqcloud.com/{path}" .format (
225
230
scheme = self ._scheme ,
226
231
bucket = to_unicode (bucket ),
227
- uid = self ._appid ,
228
232
region = self ._region ,
229
233
path = to_unicode (path )
230
234
)
231
235
else :
232
- url = u"{scheme}://{bucket}-{uid} .{region}.myqcloud.com/" .format (
236
+ url = u"{scheme}://{bucket}.{region}.myqcloud.com/" .format (
233
237
scheme = self ._scheme ,
234
238
bucket = to_unicode (bucket ),
235
- uid = self ._appid ,
236
239
region = self ._region
237
240
)
238
241
return url
@@ -272,6 +275,8 @@ def get_auth(self, Method, Bucket, Key='', Expired=300, headers={}, params={}):
272
275
273
276
def send_request (self , method , url , timeout = 30 , ** kwargs ):
274
277
"""封装request库发起http请求"""
278
+ if self ._conf ._timeout is not None : # 用户自定义超时时间
279
+ timeout = self ._conf ._timeout
275
280
if self ._conf ._token is not None :
276
281
kwargs ['headers' ]['x-cos-security-token' ] = self ._conf ._token
277
282
kwargs ['headers' ]['User-Agent' ] = 'cos-python-sdk-v5'
@@ -454,12 +459,12 @@ def head_object(self, Bucket, Key, **kwargs):
454
459
455
460
def gen_copy_source_url (self , CopySource ):
456
461
"""拼接拷贝源url"""
462
+ appid = None
457
463
if 'Appid' in CopySource .keys ():
458
464
appid = CopySource ['Appid' ]
459
- else :
460
- raise CosClientError ('CopySource Need Parameter Appid' )
461
465
if 'Bucket' in CopySource .keys ():
462
466
bucket = CopySource ['Bucket' ]
467
+ bucket = format_bucket (bucket , appid )
463
468
else :
464
469
raise CosClientError ('CopySource Need Parameter Bucket' )
465
470
if 'Region' in CopySource .keys ():
@@ -473,9 +478,8 @@ def gen_copy_source_url(self, CopySource):
473
478
path = path [1 :]
474
479
else :
475
480
raise CosClientError ('CopySource Need Parameter Key' )
476
- url = "{bucket}-{uid} .{region}.myqcloud.com/{path}" .format (
481
+ url = "{bucket}.{region}.myqcloud.com/{path}" .format (
477
482
bucket = bucket ,
478
- uid = appid ,
479
483
region = region ,
480
484
path = path
481
485
)
0 commit comments