Skip to content

Commit

Permalink
Support Endpoint Feature and Apply coding standard; to SecretId,Secre…
Browse files Browse the repository at this point in the history
…tKey
  • Loading branch information
dt3310321 committed Jul 31, 2018
1 parent 1b106a4 commit 52f8435
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 38 deletions.
60 changes: 32 additions & 28 deletions qcloud_cos/cos_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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):
Expand All @@ -62,36 +70,36 @@ 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): 存储桶名称.
:param path(string): 请求COS的路径.
: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")
path = to_unicode(path)
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

Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
27 changes: 20 additions & 7 deletions qcloud_cos/cos_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion qcloud_cos/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

# 文件流 简单上传
Expand Down
5 changes: 3 additions & 2 deletions ut/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 52f8435

Please sign in to comment.