Exemplo n.º 1
0
 def _download_part(self, part):
     get_object_request = GetObjectRequest(versionId=self.versionId)
     get_object_header = self._copy_get_object_header(self.header)
     get_object_header.range = util.to_string(
         part['offset']) + '-' + util.to_string(part['length'])
     if not self._is_abort():
         response = None
         try:
             resp = self.obsClient._getObjectWithNotifier(
                 bucketName=self.bucketName,
                 objectKey=self.objectKey,
                 getObjectRequest=get_object_request,
                 headers=get_object_header,
                 notifier=self.notifier)
             if resp.status < 300:
                 respone = resp.body.response
                 chunk_size = 65536
                 if respone is not None:
                     with open(_to_unicode(self._tmp_file), 'rb+') as fs:
                         fs.seek(part['offset'], 0)
                         while True:
                             chunk = respone.read(chunk_size)
                             if not chunk:
                                 break
                             fs.write(chunk)
                 self._record['downloadParts'][part['partNumber'] -
                                               1]['isCompleted'] = True
                 if self.enableCheckPoint:
                     with self._lock:
                         self._write_record(self._record)
             else:
                 if resp.status > 300 and resp.status < 500:
                     self._do_abort(
                         'errorCode:{0}, errorMessage:{1}'.format(
                             resp.errorCode, resp.errorMessage))
                 self._exception.append(
                     'response from server is something wrong. ErrorCode:{0}, ErrorMessage:{1}'
                     .format(resp.errorCode, resp.errorMessage))
                 self.obsClient.log_client.log(
                     ERROR,
                     'response from server is something wrong. ErrorCode:{0}, ErrorMessage:{1}'
                     .format(resp.errorCode, resp.errorMessage))
         except Exception as e:
             self.obsClient.log_client.log(
                 DEBUG,
                 'download part %s error, %s' % (part['partNumber'], e))
             self.obsClient.log_client.log(ERROR, traceback.format_exc())
         finally:
             if response is not None:
                 respone.close()
Exemplo n.º 2
0
 def _download_part(self, part):
     get_object_request = GetObjectRequest(versionId=self.versionId,
                                           imageProcess=self.imageProcess)
     get_object_header = self._copy_get_object_header(self.header)
     get_object_header.range = util.to_string(
         part['offset']) + '-' + util.to_string(part['length'])
     if not self._is_abort():
         response = None
         try:
             resp = self.obsClient._getObjectWithNotifier(
                 bucketName=self.bucketName,
                 objectKey=self.objectKey,
                 getObjectRequest=get_object_request,
                 headers=get_object_header,
                 notifier=self.notifier,
                 extensionHeaders=self.extensionHeaders)
             if resp.status < 300:
                 respone = resp.body.response
                 self._download_part_write(respone, part)
                 self._record['downloadParts'][part['partNumber'] -
                                               1]['isCompleted'] = True
                 if self.enableCheckPoint:
                     with self._lock:
                         self._write_record(self._record)
             else:
                 if 300 < resp.status < 500:
                     self._do_abort(
                         'errorCode:{0}, errorMessage:{1}'.format(
                             resp.errorCode, resp.errorMessage))
                 self._exception.append(
                     'response from server is something wrong. ErrorCode:{0}, ErrorMessage:{1}'
                     .format(resp.errorCode, resp.errorMessage))
                 self.obsClient.log_client.log(
                     ERROR,
                     'response from server is something wrong. ErrorCode:{0}, ErrorMessage:{1}'
                     .format(resp.errorCode, resp.errorMessage))
         except Exception as e:
             self.obsClient.log_client.log(
                 DEBUG,
                 'download part %s error, %s' % (part['partNumber'], e))
             self.obsClient.log_client.log(ERROR, traceback.format_exc())
         finally:
             if response is not None:
                 respone.close()
Exemplo n.º 3
0
 def _download_part(self, part):
     get_object_request = GetObjectRequest(versionId=self.versionId)
     self.header.range = str(part['offset'])+'-'+str(part['offset']+part['length'])
     try:
         resp = self.obsClient.getObject(bucketName=self.bucketName, objectKey=self.objectKey, getObjectRequest=get_object_request, headers=self.header)
         if resp.status < 300:
             respone = resp.body.response
             chunk_size = 65536
             with self._lock:
                 if respone is not None:
                     with open(_to_unicode(self._tmp_file), 'rb+') as fs:
                         fs.seek(part['offset'], 0)
                         position = to_int(part['offset'])
                         while True:
                             chunk = respone.read(chunk_size)
                             if not chunk:
                                 break
                             fs.write(chunk)
                             position += chunk_size
                             fs.seek(position, 0)
                         fs.close()
                     respone.close()
                 self._downinfos.append(True)
                 self._record['downloadParts'][part['partNumber']-1]['isCompleted'] = True
         elif resp.status > 300 and resp.status < 500:
             with self._lock:
                 self._downinfos.append(False)
                 self._change_status()
                 self._exception.append('response from server is something wrong. ErrorCode:{0}, ErrorMessage:{1}'
                                        .format(resp.errorCode, resp.errorMessage))
                 self.obsClient.log_client.log(ERROR, 'response from server is something wrong. ErrorCode:{0}, ErrorMessage:{1}'
                                               .format(resp.errorCode, resp.errorMessage))
         else:
             self._downinfos.append(False)
             self._exception.append('response from server is something wrong. ErrorCode:{0}, ErrorMessage:{1}'
                                    .format(resp.errorCode, resp.errorMessage))
     except Exception as e:
         self.obsClient.log_client.log(ERROR, 'something wraong happened. Please check.')
         raise e
     finally:
         if self.enableCheckPoint:
             with self._lock:
                 self._record['tmpFileStatus'][1] = os.path.getmtime(self._tmp_file)
                 self._write_record(self._record)
Exemplo n.º 4
0
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