diff --git a/qcloud_cos/cos_client.py b/qcloud_cos/cos_client.py index a8150287..88aed1bd 100644 --- a/qcloud_cos/cos_client.py +++ b/qcloud_cos/cos_client.py @@ -27,23 +27,28 @@ class CosConfig(object): """config类,保存用户相关信息""" - def __init__(self, Appid=None, Region=None, Secret_id=None, Secret_key=None, Token=None, Scheme=None, Timeout=None, Access_id=None, Access_key=None): + def __init__(self, Appid=None, Region=None, SecretId=None, SecretKey=None, Token=None, Scheme=None, Timeout=None, + Access_id=None, Access_key=None, Secret_id=None, Secret_key=None, Endpoint=None): """初始化,保存用户的信息 :param Appid(string): 用户APPID. :param Region(string): 地域信息. - :param Secret_id(string): 秘钥SecretId. - :param Secret_key(string): 秘钥SecretKey. + :param SecretId(string): 秘钥SecretId. + :param SecretKey(string): 秘钥SecretKey. :param Token(string): 临时秘钥使用的token. - :param Schema(string): http/https + :param Schema(string): http/https. :param Timeout(int): http超时时间. :param Access_id(string): 秘钥AccessId(兼容). :param Access_key(string): 秘钥AccessKey(兼容). + :param Secret_id(string): 秘钥SecretId(兼容). + :param Secret_key(string): 秘钥SecretKey(兼容). + :param Endpoint(string): endpoint. """ self._appid = to_unicode(Appid) - self._region = format_region(Region) self._token = to_unicode(Token) self._timeout = Timeout + self._region = Region + self._endpoint = format_endpoint(Endpoint, Region) if Scheme is None: Scheme = u'https' @@ -53,7 +58,10 @@ def __init__(self, Appid=None, Region=None, Secret_id=None, Secret_key=None, Tok self._scheme = Scheme # 兼容(SecretId,SecretKey)以及(AccessId,AccessKey) - if(Secret_id and Secret_key): + if(SecretId and SecretKey): + self._secret_id = to_unicode(SecretId) + self._secret_key = to_unicode(SecretKey) + elif(Secret_id and Secret_key): self._secret_id = to_unicode(Secret_id) self._secret_key = to_unicode(Secret_key) elif(Access_id and Access_key): @@ -62,7 +70,7 @@ def __init__(self, Appid=None, Region=None, Secret_id=None, Secret_key=None, Tok else: raise CosClientError('SecretId and SecretKey is Required!') - def uri(self, bucket, path=None, scheme=None, region=None): + def uri(self, bucket, path=None, endpoint=None): """拼接url :param bucket(string): 存储桶名称. @@ -70,10 +78,10 @@ def uri(self, bucket, path=None, scheme=None, region=None): :return(string): 请求COS的URL地址. """ bucket = format_bucket(bucket, self._appid) - if scheme is None: - scheme = self._scheme - if region is None: - region = self._region + scheme = self._scheme + if endpoint is None: + endpoint = self._endpoint + if path is not None: if not path: raise CosClientError("Key is required not empty") @@ -81,17 +89,17 @@ def uri(self, bucket, path=None, scheme=None, region=None): if path[0] == u'/': path = path[1:] path = quote(to_bytes(path), '/-_.~') - url = u"{scheme}://{bucket}.{region}.myqcloud.com/{path}".format( + url = u"{scheme}://{bucket}.{endpoint}/{path}".format( scheme=to_unicode(scheme), bucket=to_unicode(bucket), - region=to_unicode(region), + endpoint=to_unicode(endpoint), path=to_unicode(path) ) else: - url = u"{scheme}://{bucket}.{region}.myqcloud.com/".format( - scheme=self._scheme, + url = u"{scheme}://{bucket}.{endpoint}/".format( + scheme=to_unicode(scheme), bucket=to_unicode(bucket), - region=self._region + endpoint=to_unicode(endpoint) ) return url @@ -1988,11 +1996,11 @@ def upload_file(self, Bucket, Key, LocalFilePath, PartSize=1, MAXThread=5, **kwa def _inner_head_object(self, CopySource): """查询源文件的长度""" - bucket, path, region, versionid = get_copy_source_info(CopySource) + bucket, path, endpoint, versionid = get_copy_source_info(CopySource) params = {} if versionid != '': params['versionId'] = versionid - url = self._conf.uri(bucket=bucket, path=path, scheme=self._conf._scheme, region=region) + url = self._conf.uri(bucket=bucket, path=path, endpoint=endpoint) rt = self.send_request( method='HEAD', url=url, @@ -2017,13 +2025,9 @@ def _upload_part_copy(self, bucket, key, part_number, upload_id, copy_source, co md5_lst.append({'PartNumber': part_number, 'ETag': rt['ETag']}) return None - def _check_same_region(self, dst_region, CopySource): - if 'Region' in CopySource: - src_region = CopySource['Region'] - src_region = format_region(src_region) - else: - raise CosClientError('CopySource Need Parameter Region') - if src_region == dst_region: + def _check_same_region(self, dst_endpoint, CopySource): + src_endpoint = get_copy_source_info(CopySource)[2] + if src_endpoint == dst_endpoint: return True return False @@ -2053,7 +2057,7 @@ def copy(self, Bucket, Key, CopySource, CopyStatus='Copy', PartSize=10, MAXThrea ) """ # 同园区直接走copy_object - if self._check_same_region(self._conf._region, CopySource): + if self._check_same_region(self._conf._endpoint, CopySource): response = self.copy_object(Bucket=Bucket, Key=Key, CopySource=CopySource, CopyStatus=CopyStatus, **kwargs) return response @@ -2302,7 +2306,7 @@ def change_object_storage_class(self, Bucket, Key, StorageClass): copy_source = { 'Bucket': Bucket, 'Key': Key, - 'Region': self._conf._region, + 'Endpoint': self._conf._endpoint, 'Appid': self._conf._appid } response = self.copy_object( @@ -2336,7 +2340,7 @@ def update_object_meta(self, Bucket, Key, **kwargs): copy_source = { 'Bucket': Bucket, 'Key': Key, - 'Region': self._conf._region, + 'Endpoint': self._conf._endpoint, 'Appid': self._conf._appid } response = self.copy_object( diff --git a/qcloud_cos/cos_comm.py b/qcloud_cos/cos_comm.py index 612e5b96..72acc0aa 100644 --- a/qcloud_cos/cos_comm.py +++ b/qcloud_cos/cos_comm.py @@ -192,6 +192,17 @@ def format_values(data): return data +def format_endpoint(endpoint, region): + """格式化终端域名""" + if not endpoint and not region: + raise CosClientError("Region or Endpoint is required not empty!") + if not endpoint: + region = format_region(region) + return u"{region}.myqcloud.com".format(region=region) + else: + return to_unicode(endpoint) + + def format_region(region): """格式化地域""" if not isinstance(region, string_types): @@ -267,6 +278,8 @@ def get_copy_source_info(CopySource): """获取拷贝源的所有信息""" appid = u"" versionid = u"" + region = u"" + endpoint = u"" if 'Appid' in CopySource: appid = CopySource['Appid'] if 'Bucket' in CopySource: @@ -276,27 +289,27 @@ def get_copy_source_info(CopySource): raise CosClientError('CopySource Need Parameter Bucket') if 'Region' in CopySource: region = CopySource['Region'] - region = format_region(region) - else: - raise CosClientError('CopySource Need Parameter Region') + if 'Endpoint' in CopySource: + endpoint = CopySource['Endpoint'] + endpoint = format_endpoint(endpoint, region) if 'Key' in CopySource: path = to_unicode(CopySource['Key']) else: raise CosClientError('CopySource Need Parameter Key') if 'VersionId' in CopySource: versionid = to_unicode(CopySource['VersionId']) - return bucket, path, region, versionid + return bucket, path, endpoint, versionid def gen_copy_source_url(CopySource): """拼接拷贝源url""" - bucket, path, region, versionid = get_copy_source_info(CopySource) + bucket, path, endpoint, versionid = get_copy_source_info(CopySource) path = format_path(path) if versionid != u'': path = path + u'?versionId=' + versionid - url = u"{bucket}.{region}.myqcloud.com/{path}".format( + url = u"{bucket}.{endpoint}/{path}".format( bucket=bucket, - region=region, + endpoint=endpoint, path=path ) return url diff --git a/qcloud_cos/demo.py b/qcloud_cos/demo.py index 5b5afe92..89329ecc 100644 --- a/qcloud_cos/demo.py +++ b/qcloud_cos/demo.py @@ -21,7 +21,7 @@ secret_key = 'csivKvxxrMvSvQpMWHuIz12pThQQlWRW' # 替换为用户的secret_key region = 'ap-beijing-1' # 替换为用户的region token = '' # 使用临时秘钥需要传入Token,默认为空,可不填 -config = CosConfig(Region=region, Secret_id=secret_id, Secret_key=secret_key, Token=token) # 获取配置对象 +config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token) # 获取配置对象 client = CosS3Client(config) # 文件流 简单上传 diff --git a/ut/test.py b/ut/test.py index 937496b2..9afcefde 100644 --- a/ut/test.py +++ b/ut/test.py @@ -18,8 +18,9 @@ special_file_name = "中文" + "→↓←→↖↗↙↘! \"#$%&'()*+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" conf = CosConfig( Region="ap-beijing-1", - Secret_id=SECRET_ID, - Secret_key=SECRET_KEY + SecretId=SECRET_ID, + SecretKey=SECRET_KEY, + Endpoint="cos.ap-beijing-1.myqcloud.com" # support endpoint ) client = CosS3Client(conf)