def __init__(self, bucketName, objectKey, fileName, partSize, taskNum, enableCheckPoint, checkPointFile, progressCallback, obsClient, notifier=progress.NONE_NOTIFIER): self.bucketName = bucketName self.objectKey = objectKey self.fileName = util.safe_trans_to_gb2312(fileName) self.partSize = partSize self.taskNum = taskNum self.enableCheckPoint = enableCheckPoint self.checkPointFile = util.safe_trans_to_gb2312(checkPointFile) self.progressCallback = progressCallback self.notifier = notifier self.obsClient = obsClient self._lock = threading.Lock() self._abortLock = threading.Lock() self._abort = False self._record = None self._exception = None
def _get_record(self): self.obsClient.log_client.log(INFO, 'load record file...') if not os.path.exists(self.checkPointFile): return None try: with open(safe_trans_to_gb2312(self.checkPointFile), 'r') as f: content = json.load(f) except ValueError: return None else: return _parse_string(content)
def _get_record(self): # logger.info('load record file: %s' % self.checkpoint_file) if not os.path.exists(self.checkpoint_file): return None try: with open(safe_trans_to_gb2312(self.checkpoint_file), 'r') as f: content = json.load(f) except ValueError: logger.warning( 'part task checkpoint file is broken. try to rebuild multipart task. file path is:{0}'.format( self.checkpoint_file)) # if 'upload' in self.checkpoint_file: # self.obscmdutil.remove_object_multipart(self.bucket, self.objkey) self._delete_record() logger.warning('delete checkpoint, file path is:{0}'.format(self.checkpoint_file)) return None else: return _parse_string(content)
def _download_files(obsClient, bucketName, prefix, downloadFolder=None, taskNum=const.DEFAULT_TASK_NUM, taskQueueSize=const.DEFAULT_TASK_QUEUE_SIZE, headers=GetObjectHeader(), imageProcess=None, interval=const.DEFAULT_BYTE_INTTERVAL, taskCallback=None, progressCallback=None, threshold=const.DEFAULT_MAXIMUM_SIZE, partSize=5 * 1024 * 1024, subTaskNum=1, enableCheckpoint=False, checkpointFile=None): try: executor = None notifier = None if downloadFolder is None or not os.path.isdir(downloadFolder): raise Exception('%s is not a Folder' % downloadFolder) if taskCallback is not None and not callable(taskCallback): raise Exception('Invalid taskCallback') (taskNum, taskQueueSize, interval, threshold) = bulktasks._checkBulkTasksPara(taskNum, taskQueueSize, interval, threshold) taskCallback = taskCallback if taskCallback is not None else util.lazyCallback executor = bulktasks.ThreadPool(taskNum, taskQueueSize) state = bulktasks.ExecuteProgress() totalTasks = const.LONG(0) totalAmount = const.LONG(0) notifier = progress.ProgressNotifier( progressCallback, totalAmount, interval ) if progressCallback is not None else progress.NONE_NOTIFIER notifier.start() query = GetObjectRequest(imageProcess=imageProcess) prefix = prefix if prefix is not None else '' prefixDir = prefix[:prefix.rfind('/') + 1] for content in _list_objects(obsClient, bucketName, prefix=prefix): objectKey = content.key totalTasks += 1 totalAmount += content.size objectPath = objectKey.replace(prefixDir, '', 1) if objectPath.startswith('/') or objectPath.find( '//') != -1 or objectPath.find('\\') != -1: state._failed_increment() taskCallback(objectKey, Exception('illegal path: %s' % objectKey)) obsClient.log_client.log(ERROR, 'illegal path: %s' % objectKey) continue downloadPath = os.path.join(downloadFolder, objectPath) downloadPath = util.safe_encode(downloadPath) if const.IS_WINDOWS: downloadPath = util.safe_trans_to_gb2312(downloadPath) dirName = os.path.dirname(downloadPath) if not os.path.exists(dirName): try: os.makedirs(dirName, 0o755) except Exception as e: state._failed_increment() taskCallback(objectKey, e) obsClient.log_client.log(ERROR, traceback.format_exc()) continue if objectKey.endswith(('/')): state._successful_increment() elif content.size < threshold: executor.execute(_task_wrap, obsClient, obsClient._getObjectWithNotifier, key=objectKey, taskCallback=taskCallback, state=state, bucketName=bucketName, objectKey=objectKey, getObjectRequest=query, headers=headers, downloadPath=downloadPath, notifier=notifier) else: executor.execute(_task_wrap, obsClient, obsClient._downloadFileWithNotifier, key=objectKey, taskCallback=taskCallback, state=state, bucketName=bucketName, objectKey=objectKey, downloadFile=downloadPath, partSize=partSize, taskNum=subTaskNum, enableCheckpoint=enableCheckpoint, checkpointFile=checkpointFile, header=headers, imageProcess=imageProcess, notifier=notifier) state.total_tasks = totalTasks notifier.totalAmount = totalAmount finally: if executor is not None: executor.shutdown() if notifier is not None: notifier.end() return state
def _delete_record(self): if os.path.exists(safe_trans_to_gb2312(self.checkpoint_file)): os.remove(safe_trans_to_gb2312(self.checkpoint_file)) logger.info('del record file success. path is:{0}'.format(self.checkpoint_file))
def _delete_record(self): if os.path.exists(safe_trans_to_gb2312(self.checkPointFile)): os.remove(safe_trans_to_gb2312(self.checkPointFile)) self.obsClient.log_client.log(INFO, 'del record file success. path is:{0}'.format(self.checkPointFile))