Skip to content

Commit 2f307bc

Browse files
committed
format bucket and support to set Timeout
1 parent 90d4fe8 commit 2f307bc

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

qcloud_cos/cos_client.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,21 @@ def format_region(region):
179179

180180

181181
def format_bucket(bucket, appid):
182-
"""去除bucket结尾含有的appid"""
182+
"""兼容新老bucket长短命名,appid为空默认为长命名,appid不为空则认为是短命名"""
183183
if not isinstance(bucket, str):
184184
raise CosClientError("bucket is not str")
185+
# appid为空直接返回bucket
186+
if appid == "":
187+
return bucket
188+
# appid不为空,检查是否以-appid结尾
185189
if bucket.endswith("-"+appid):
186-
index = bucket.find("-"+appid)
187-
return bucket[0:index]
188-
return bucket
190+
return bucket
191+
return bucket + "-" + appid
189192

190193

191194
class CosConfig(object):
192195
"""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):
194197
"""初始化,保存用户的信息
195198
196199
:param Appid(string): 用户APPID.
@@ -199,13 +202,15 @@ def __init__(self, Appid, Region, Access_id, Access_key, Scheme='http', Token=No
199202
:param Access_key(string): 秘钥SecretKey.
200203
:param Scheme(string): http/https.
201204
:param Token(string): 临时秘钥使用的token.
205+
:param Timeout(int): http超时时间.
202206
"""
203207
self._appid = Appid
204208
self._region = format_region(Region)
205209
self._access_id = Access_id
206210
self._access_key = Access_key
207211
self._scheme = Scheme
208212
self._token = Token
213+
self._timeout = Timeout
209214
logger.info("config parameter-> appid: {appid}, region: {region}".format(
210215
appid=Appid,
211216
region=Region))
@@ -221,18 +226,16 @@ def uri(self, bucket, path=None):
221226
if path:
222227
if path[0] == '/':
223228
path = path[1:]
224-
url = u"{scheme}://{bucket}-{uid}.{region}.myqcloud.com/{path}".format(
229+
url = u"{scheme}://{bucket}.{region}.myqcloud.com/{path}".format(
225230
scheme=self._scheme,
226231
bucket=to_unicode(bucket),
227-
uid=self._appid,
228232
region=self._region,
229233
path=to_unicode(path)
230234
)
231235
else:
232-
url = u"{scheme}://{bucket}-{uid}.{region}.myqcloud.com/".format(
236+
url = u"{scheme}://{bucket}.{region}.myqcloud.com/".format(
233237
scheme=self._scheme,
234238
bucket=to_unicode(bucket),
235-
uid=self._appid,
236239
region=self._region
237240
)
238241
return url
@@ -272,6 +275,8 @@ def get_auth(self, Method, Bucket, Key='', Expired=300, headers={}, params={}):
272275

273276
def send_request(self, method, url, timeout=30, **kwargs):
274277
"""封装request库发起http请求"""
278+
if self._conf._timeout is not None: # 用户自定义超时时间
279+
timeout = self._conf._timeout
275280
if self._conf._token is not None:
276281
kwargs['headers']['x-cos-security-token'] = self._conf._token
277282
kwargs['headers']['User-Agent'] = 'cos-python-sdk-v5'
@@ -454,12 +459,12 @@ def head_object(self, Bucket, Key, **kwargs):
454459

455460
def gen_copy_source_url(self, CopySource):
456461
"""拼接拷贝源url"""
462+
appid = None
457463
if 'Appid' in CopySource.keys():
458464
appid = CopySource['Appid']
459-
else:
460-
raise CosClientError('CopySource Need Parameter Appid')
461465
if 'Bucket' in CopySource.keys():
462466
bucket = CopySource['Bucket']
467+
bucket = format_bucket(bucket, appid)
463468
else:
464469
raise CosClientError('CopySource Need Parameter Bucket')
465470
if 'Region' in CopySource.keys():
@@ -473,9 +478,8 @@ def gen_copy_source_url(self, CopySource):
473478
path = path[1:]
474479
else:
475480
raise CosClientError('CopySource Need Parameter Key')
476-
url = "{bucket}-{uid}.{region}.myqcloud.com/{path}".format(
481+
url = "{bucket}.{region}.myqcloud.com/{path}".format(
477482
bucket=bucket,
478-
uid=appid,
479483
region=region,
480484
path=path
481485
)

0 commit comments

Comments
 (0)