Skip to content

Commit

Permalink
modify auth logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tiedu committed Jun 24, 2020
1 parent 61fb5d8 commit c72ca76
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
9 changes: 4 additions & 5 deletions qcloud_cos/cos_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ def __call__(self, r):
path = self._path
uri_params = self._params
headers = filter_headers(r.headers)
uri_params = dict([(k.lower(), v) for k, v in uri_params.items()])
# reserved keywords in headers urlencode are -_.~, notice that / should be encoded and space should not be encoded to plus sign(+)
headers = dict([(k.lower(), quote(to_bytes(v), '-_.~')) for k, v in headers.items()]) # headers中的key转换为小写,value进行encode
uri_params = dict([(k.lower(), v) for k, v in uri_params.items()])
headers = dict([(quote(to_bytes(str(k)), '-_.~').lower(), quote(to_bytes(str(v)), '-_.~')) for k, v in headers.items()]) # headers中的key转换为小写,value进行encode
uri_params = dict([(quote(to_bytes(str(k)), '-_.~').lower(), quote(to_bytes(str(v)), '-_.~')) for k, v in uri_params.items()])
format_str = u"{method}\n{host}\n{params}\n{headers}\n".format(
method=r.method.lower(),
host=path,
params=urlencode(sorted(uri_params.items())).replace('+', '%20').replace('%7E', '~'),
params='&'.join(map(lambda tupl: "%s=%s" % (tupl[0], tupl[1]), sorted(uri_params.items()))),
headers='&'.join(map(lambda tupl: "%s=%s" % (tupl[0], tupl[1]), sorted(headers.items())))
)
logger.debug("format str: " + format_str)
Expand All @@ -73,7 +72,7 @@ def __call__(self, r):
ak=self._secret_id,
sign_time=sign_time,
key_time=sign_time,
params=';'.join(sorted(map(lambda k: k.lower(), uri_params.keys()))),
params=';'.join(sorted(uri_params.keys())),
headers=';'.join(sorted(headers.keys())),
sign=sign
)
Expand Down
13 changes: 9 additions & 4 deletions qcloud_cos/cos_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2755,7 +2755,7 @@ def list_buckets(self, **kwargs):
return data

# Advanced interface
def _upload_part(self, bucket, key, local_path, offset, size, part_num, uploadid, md5_lst, resumable_flag, already_exist_parts, enable_md5):
def _upload_part(self, bucket, key, local_path, offset, size, part_num, uploadid, md5_lst, resumable_flag, already_exist_parts, enable_md5, traffic_limit):
"""从本地文件中读取分块, 上传单个分块,将结果记录在md5——list中
:param bucket(string): 存储桶名称.
Expand All @@ -2778,12 +2778,13 @@ def _upload_part(self, bucket, key, local_path, offset, size, part_num, uploadid
with open(local_path, 'rb') as fp:
fp.seek(offset, 0)
data = fp.read(size)
rt = self.upload_part(bucket, key, data, part_num, uploadid, enable_md5)
rt = self.upload_part(bucket, key, data, part_num, uploadid, enable_md5, TrafficLimit=traffic_limit)
md5_lst.append({'PartNumber': part_num, 'ETag': rt['ETag']})
return None

def _get_resumable_uploadid(self, bucket, key):
"""从服务端获取未完成的分块上传任务,获取断点续传的uploadid
params=urlencode(sorted(uri_params.items())).replace('+', '%20').replace('%7E', '~'),
:param bucket(string): 存储桶名称.
:param key(string): 分块上传路径名.
Expand Down Expand Up @@ -2931,15 +2932,19 @@ def upload_file(self, Bucket, Key, LocalFilePath, PartSize=1, MAXThread=5, Enabl
logger.info("create a new uploadid in upload_file, uploadid={uploadid}".format(uploadid=uploadid))

# 上传分块
# 增加限速功能
traffic_limit = None
if 'TrafficLimit' in kwargs:
traffic_limit = kwargs['TrafficLimit']
offset = 0 # 记录文件偏移量
lst = list() # 记录分块信息
pool = SimpleThreadPool(MAXThread)

for i in range(1, parts_num+1):
if i == parts_num: # 最后一块
pool.add_task(self._upload_part, Bucket, Key, LocalFilePath, offset, file_size-offset, i, uploadid, lst, resumable_flag, already_exist_parts, EnableMD5)
pool.add_task(self._upload_part, Bucket, Key, LocalFilePath, offset, file_size-offset, i, uploadid, lst, resumable_flag, already_exist_parts, EnableMD5, traffic_limit)
else:
pool.add_task(self._upload_part, Bucket, Key, LocalFilePath, offset, part_size, i, uploadid, lst, resumable_flag, already_exist_parts, EnableMD5)
pool.add_task(self._upload_part, Bucket, Key, LocalFilePath, offset, part_size, i, uploadid, lst, resumable_flag, already_exist_parts, EnableMD5, traffic_limit)
offset += part_size

pool.wait_completion()
Expand Down
2 changes: 1 addition & 1 deletion qcloud_cos/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

__version__ = '5.1.7.8'
__version__ = '5.1.8.1'
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def long_description():

setup(
name='cos-python-sdk-v5',
version='1.7.8',
version='1.8.1',
url='https://www.qcloud.com/',
license='MIT',
author='tiedu, lewzylu, channingliu',
Expand Down

0 comments on commit c72ca76

Please sign in to comment.