Skip to content

Commit 0e5e36d

Browse files
authored
Merge pull request #191 from zhy1985555/master
高级下载支持进度回调
2 parents 3b6e2f1 + baf2a3e commit 0e5e36d

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

qcloud_cos/cos_client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3194,7 +3194,7 @@ def _check_all_upload_parts(self, bucket, key, uploadid, local_path, parts_num,
31943194
already_exist_parts[part_num] = part['ETag']
31953195
return True
31963196

3197-
def download_file(self, Bucket, Key, DestFilePath, PartSize=20, MAXThread=5, EnableCRC=False, **Kwargs):
3197+
def download_file(self, Bucket, Key, DestFilePath, PartSize=20, MAXThread=5, EnableCRC=False, progress_callback=None, **Kwargs):
31983198
"""小于等于20MB的文件简单下载,大于20MB的文件使用续传下载
31993199
32003200
:param Bucket(string): 存储桶名称.
@@ -3224,8 +3224,13 @@ def download_file(self, Bucket, Key, DestFilePath, PartSize=20, MAXThread=5, Ena
32243224
response['Body'].get_stream_to_file(DestFilePath)
32253225
return
32263226

3227+
# 支持回调查看进度
3228+
callback = None
3229+
if progress_callback:
3230+
callback = ProgressCallback(file_size, progress_callback)
3231+
32273232
downloader = ResumableDownLoader(self, Bucket, Key, DestFilePath, object_info, PartSize, MAXThread, EnableCRC,
3228-
**Kwargs)
3233+
callback, **Kwargs)
32293234
downloader.start()
32303235

32313236
def upload_file(self, Bucket, Key, LocalFilePath, PartSize=1, MAXThread=5, EnableMD5=False, progress_callback=None,

qcloud_cos/resumable_downloader.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717

1818
class ResumableDownLoader(object):
1919
def __init__(self, cos_client, bucket, key, dest_filename, object_info, part_size=20, max_thread=5,
20-
enable_crc=False, **kwargs):
20+
enable_crc=False, progress_callback=None, **kwargs):
2121
self.__cos_client = cos_client
2222
self.__bucket = bucket
2323
self.__key = key
2424
self.__dest_file_path = os.path.abspath(dest_filename)
2525
self.__object_info = object_info
2626
self.__max_thread = max_thread
2727
self.__enable_crc = enable_crc
28+
self.__progress_callback = progress_callback
2829
self.__headers = kwargs
2930

3031
self.__max_part_count = 100 # 取决于服务端是否对并发有限制
@@ -50,6 +51,11 @@ def start(self):
5051
assert self.__tmp_file
5152
open(self.__tmp_file, 'a').close()
5253

54+
# 已完成分块先设置下载进度
55+
if self.__progress_callback:
56+
for finished_part in self.__finished_parts:
57+
self.__progress_callback.report(finished_part.length)
58+
5359
parts_need_to_download = self.__get_parts_need_to_download()
5460
logger.debug('parts_need_to_download: {0}'.format(parts_need_to_download))
5561
pool = SimpleThreadPool(self.__max_thread)
@@ -126,6 +132,9 @@ def __download_part(self, part, headers):
126132

127133
self.__finish_part(part)
128134

135+
if self.__progress_callback:
136+
self.__progress_callback.report(part.length)
137+
129138
def __finish_part(self, part):
130139
logger.debug('download part finished,bucket: {0}, key: {1}, part_id: {2}'.
131140
format(self.__bucket, self.__key, part.part_id))

0 commit comments

Comments
 (0)