def upload_file(path, filepath): drive = AliyunDrive(DRIVE_ID, ROOT_PATH, REFRESH_TOKEN, CHUNK_SIZE) # 刷新token drive.token_refresh() realpath = path + filepath drive.load_file(filepath, realpath) # 创建目录 common.LOCK.acquire() try: parent_folder_id = drive.get_parent_folder_id(filepath) finally: common.LOCK.release() # 断点续传 if RESUME and drive.filepath_hash in common.DATA['tasks']: c_task = common.DATA['tasks'][drive.filepath_hash] if 0 not in ( c_task['drive_id'], c_task['file_id'], c_task['upload_id'], c_task['part_number'], c_task['chunk_size'], ): drive.drive_id = c_task['drive_id'] drive.file_id = c_task['file_id'] drive.upload_id = c_task['upload_id'] drive.part_number = c_task['part_number'] drive.chunk_size = c_task['chunk_size'] # 获取上传地址 drive.part_upload_url_list = drive.get_upload_url() # 上传 drive.upload() # 提交 if drive.complete(): return drive.filepath_hash return False # 创建上传 create_post_json = drive.create(parent_folder_id) if 'rapid_upload' in create_post_json and create_post_json['rapid_upload']: print_success('【{filename}】秒传成功!消耗{s}秒'.format(filename=drive.filename, s=time.time() - drive.start_time)) return drive.filepath_hash # 上传 drive.upload() # 提交 if drive.complete(): return drive.filepath_hash return False
# 配置信息 try: with open(os.getcwd() + '/config.json', 'rb') as f: config = json.loads(f.read()) REFRESH_TOKEN = config.get('REFRESH_TOKEN') FILE_PATH = config.get('FILE_PATH') DRIVE_ID = config.get('DRIVE_ID') ROOT_PATH = config.get('ROOT_PATH').replace('/', os.sep).replace('\\\\', os.sep).rstrip(os.sep) + os.sep # 启用多线程 MULTITHREADING = bool(config.get('MULTITHREADING')) # 线程池最大线程数 MAX_WORKERS = config.get('MAX_WORKERS') except Exception as e: print_error('请配置好config.json后重试') raise e drive = AliyunDrive(DRIVE_ID, ROOT_PATH, REFRESH_TOKEN) # 刷新token drive.token_refresh() # 命令行参数上传 if len(sys.argv) == 2: if os.path.isdir(sys.argv[1]): # 目录上传 FILE_PATH = sys.argv[1] file_list = get_all_file_relative(FILE_PATH) else: # 单文件上传 FILE_PATH = os.path.dirname(sys.argv[1]) file_list = [os.path.basename(sys.argv[1])] task = {} else: file_list = get_all_file_relative(FILE_PATH)
def upload_file(self, task): save_task(task['id'], { 'status': 2 }) drive = AliyunDrive(DATA['config']['DRIVE_ID'], DATA['config']['ROOT_PATH'], DATA['config']['CHUNK_SIZE']) # 加载任务队列 drive.load_task(task) # 刷新token # drive.token_refresh() if not os.path.exists(task['realpath']): drive.status = -1 return drive drive.load_file(task['filepath'], task['realpath']) # 创建目录 LOCK.acquire() try: parent_folder_id = drive.get_parent_folder_id(drive.filepath) finally: LOCK.release() # 断点续传 if DATA['config']['RESUME'] and DATA['config']['DRIVE_ID'] == task['drive_id']: if 0 not in [ drive.drive_id, drive.part_number, drive.chunk_size, ] and not drive.file_id and not drive.upload_id: # 获取上传地址 drive.part_upload_url_list = drive.get_upload_url() # 上传 return self.__upload(drive) # 创建上传 create_post_json = drive.create(parent_folder_id) if 'rapid_upload' in create_post_json and create_post_json['rapid_upload']: drive.finish_time = get_timestamp() drive.spend_time = drive.finish_time - drive.start_time self.print('【{filename}】秒传成功!消耗{s}秒'.format(filename=drive.filename, s=drive.spend_time), 'success', drive.id) drive.status = 1 return drive # 上传 return self.__upload(drive)
def crontab_tasks(): # 定时刷新token (AliyunDrive(DATA['config']['DRIVE_ID'], DATA['config']['ROOT_PATH'], DATA['config']['CHUNK_SIZE'])).token_refresh()