1717
1818class 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