def rapid_uploadfile(filepath, pcspath): if os.stat(filepath).st_size <= 262144: logger.error( 'Rapid upload file "%s" failed: flie size must be greater than or equal to 256KB.' % (filepath)) return 1 logger.debug('start rapid upload file "%s".' % (filepath)) crc, contentmd5, slicemd5 = BaiduPcsApi.__rapid_checkcode(filepath) sycurl = SynCurl() url = 'https://pcs.baidu.com/rest/2.0/pcs/file' querydata = { 'method': 'rapidupload', 'access_token': SynConfig.token['access_token'], 'path': pcspath, 'content-length': os.stat(filepath).st_size, 'content-md5': contentmd5, 'slice-md5': slicemd5, 'content-crc32': crc, 'ondup': 'newcopy' } retcode, responses = sycurl.request(url, querydata, '', 'POST', SynCurl.Normal) responses = json.loads(responses) if retcode != 200 or 'error_code' in responses: if responses['error_code'] == 31079: logger.info( ' File md5 not found, will upload the whole file "%s".' % (filepath)) return 1 else: logger.error('Errno:%d: Rapid upload file "%s" failed: %s.' % (retcode, filepath, responses['error_msg'])) return 1 else: time.sleep(1) url = 'https://pcs.baidu.com/rest/2.0/pcs/file' querydata = { 'method': 'meta', 'access_token': SynConfig.token['access_token'], 'path': pcspath } retcode, responses = sycurl.request(url, querydata, '', 'GET', SynCurl.Normal) responses = json.loads(responses) if retcode != 200 or 'error_code' in responses: logger.error( 'Errno:%d: File "%s" is rapid uploaded, but get remote file\'s mate failed: %s.' % (retcode, filepath, responses['error_msg'])) return 1 responses = responses['list'][0] if responses['size'] == os.stat(filepath).st_size: logger.info(' Rapid upload file "%s" completed.' % (filepath)) return 0 else: logger.error( 'File "%s" is rapid uploaded, but remote file size not equal to local.' % (filepath)) return 1
def get_pcs_quota(): sycurl = SynCurl() url = 'https://pcs.baidu.com/rest/2.0/pcs/quota' querydata = { 'method': 'info', 'access_token': SynConfig.token['access_token'] } retcode, responses = sycurl.request(url, querydata, '', 'GET', SynCurl.Normal) responses = json.loads(responses) if retcode != 200 or 'error_code' in responses: logger.error('Errno:%d: Get pcs quota failed: %s.' % (retcode, responses['error_msg'])) return 1 logger.info(' PCS quota is %dG,used %dG.' % (responses['quota'] / 1024 / 1024 / 1024, responses['used'] / 1024 / 1024 / 1024)) return 0
def rapid_uploadfile(filepath, pcspath): if os.stat(filepath).st_size <= 262144: logger.error('Rapid upload file "%s" failed: flie size must be greater than or equal to 256KB.' % ( filepath)) return 1 logger.debug('start rapid upload file "%s".' % (filepath)) crc, contentmd5, slicemd5 = BaiduPcsApi.__rapid_checkcode(filepath) sycurl = SynCurl() url = 'https://pcs.baidu.com/rest/2.0/pcs/file' querydata = { 'method': 'rapidupload', 'access_token': SynConfig.token['access_token'], 'path': pcspath, 'content-length': os.stat(filepath).st_size, 'content-md5': contentmd5, 'slice-md5': slicemd5, 'content-crc32': crc, 'ondup': 'newcopy' } retcode, responses = sycurl.request(url, querydata, '', 'POST', SynCurl.Normal) responses = json.loads(responses) if retcode != 200 or 'error_code' in responses: if responses['error_code'] == 31079: logger.info(' File md5 not found, will upload the whole file "%s".' % (filepath)) return 1 else: logger.error('Errno:%d: Rapid upload file "%s" failed: %s.' % (retcode, filepath, responses['error_msg'])) return 1 else: time.sleep(1) url = 'https://pcs.baidu.com/rest/2.0/pcs/file' querydata = { 'method': 'meta', 'access_token': SynConfig.token['access_token'], 'path': pcspath } retcode, responses = sycurl.request(url, querydata, '', 'GET', SynCurl.Normal) responses = json.loads(responses) if retcode != 200 or 'error_code' in responses: logger.error('Errno:%d: File "%s" is rapid uploaded, but get remote file\'s mate failed: %s.' % (retcode, filepath, responses['error_msg'])) return 1 responses = responses['list'][0] if responses['size'] == os.stat(filepath).st_size: logger.info(' Rapid upload file "%s" completed.' % (filepath)) return 0 else: logger.error('File "%s" is rapid uploaded, but remote file size not equal to local.' % (filepath)) return 1
def download_file(filepath, pcspath, filerange): logger.debug('start download file "%s" : range %s.' % (filepath, filerange)) sycurl = SynCurl() url = 'https://d.pcs.baidu.com/rest/2.0/pcs/file' querydata = { 'method': 'download', 'access_token': SynConfig.token['access_token'], 'path': pcspath } retcode, responses = sycurl.request(url, querydata, filerange, 'GET', SynCurl.Download, filepath) if (retcode != 200 and retcode != 206) or responses != '': responses = json.loads(responses) logger.error('Errno:%d: Download file "%s" failed: %s.' % (retcode, pcspath, responses['error_msg'])) return 1 logger.info(' download file "%s" completed.' % (filepath)) return 0
def slice_upload_createsuperfile(pcspath, param): sycurl = SynCurl() url = 'https://pcs.baidu.com/rest/2.0/pcs/file' querydata = { 'method': 'createsuperfile', 'access_token': SynConfig.token['access_token'], 'path': pcspath, 'ondup': 'newcopy' } retcode, responses = sycurl.request(url, querydata, {'param': json.dumps(param)}, 'POST', SynCurl.Normal) responses = json.loads(responses) if retcode != 200 or 'error_code' in responses: logger.error('Errno:%d: Create superfile "%s" failed: %s.' % (retcode, pcspath, responses['error_msg'])) return 1 logger.info(' Slice upload file "%s" completed.' % (pcspath)) return 0
def cp_pcsfile(srcpcspath, destpcspath): sycurl = SynCurl() url = 'https://pcs.baidu.com/rest/2.0/pcs/file' querydata = { 'method': 'copy', 'access_token': SynConfig.token['access_token'], 'from': srcpcspath, 'to': destpcspath } retcode, responses = sycurl.request(url, querydata, '', 'POST', SynCurl.Normal) responses = json.loads(responses) if retcode != 200 or 'error_code' in responses: logger.error('Errno:%d: Copy remote file or directory "%s" to "%s" failed: %s.' % (retcode, srcpcspath, destpcspath, responses['error_msg'])) return 1 logger.info(' Copy remote file or directory "%s" to "%s" completed.' % (srcpcspath, destpcspath)) return 0
def upload_file(filepath, pcspath): logger.debug('start upload whole file "%s".' % (filepath)) sycurl = SynCurl() url = 'https://c.pcs.baidu.com/rest/2.0/pcs/file' querydata = { 'method': 'upload', 'access_token': SynConfig.token['access_token'], 'path': pcspath, 'ondup': 'newcopy' } retcode, responses = sycurl.request(url, querydata, '0-%d' % (os.stat(filepath).st_size - 1), 'POST', SynCurl.Upload, filepath) responses = json.loads(responses) if retcode != 200 or 'error_code' in responses: logger.error('Errno:%d: Upload file to pcs failed: %s, %s.' % (retcode, filepath, responses['error_msg'])) return 1 logger.info(' Upload file "%s" completed.' % (filepath)) return 0
def rm_pcsfile(pcspath, slient=False): sycurl = SynCurl() url = 'https://pcs.baidu.com/rest/2.0/pcs/file' querydata = { 'method': 'delete', 'access_token': SynConfig.token['access_token'], 'path': pcspath } retcode, responses = sycurl.request(url, querydata, '', 'POST', SynCurl.Normal) responses = json.loads(responses) if retcode != 200 or 'error_code' in responses: if not slient: logger.error('Errno:%d: Delete remote file or directory "%s" failed: %s.' % (retcode, pcspath, responses['error_msg'])) return 1 if not slient: logger.info(' Delete remote file or directory "%s" completed.' % (pcspath)) return 0
def upload_file(filepath, pcspath): logger.debug('start upload whole file "%s".' % (filepath)) sycurl = SynCurl() url = 'https://c.pcs.baidu.com/rest/2.0/pcs/file' querydata = { 'method': 'upload', 'access_token': SynConfig.token['access_token'], 'path': pcspath, 'ondup': 'newcopy' } retcode, responses = sycurl.request( url, querydata, '0-%d' % (os.stat(filepath).st_size - 1), 'POST', SynCurl.Upload, filepath) responses = json.loads(responses) if retcode != 200 or 'error_code' in responses: logger.error('Errno:%d: Upload file to pcs failed: %s, %s.' % (retcode, filepath, responses['error_msg'])) return 1 logger.info(' Upload file "%s" completed.' % (filepath)) return 0
def cp_pcsfile(srcpcspath, destpcspath): sycurl = SynCurl() url = 'https://pcs.baidu.com/rest/2.0/pcs/file' querydata = { 'method': 'copy', 'access_token': SynConfig.token['access_token'], 'from': srcpcspath, 'to': destpcspath } retcode, responses = sycurl.request(url, querydata, '', 'POST', SynCurl.Normal) responses = json.loads(responses) if retcode != 200 or 'error_code' in responses: logger.error( 'Errno:%d: Copy remote file or directory "%s" to "%s" failed: %s.' % (retcode, srcpcspath, destpcspath, responses['error_msg'])) return 1 logger.info(' Copy remote file or directory "%s" to "%s" completed.' % (srcpcspath, destpcspath)) return 0
def rm_pcsfile(pcspath, slient=False): sycurl = SynCurl() url = 'https://pcs.baidu.com/rest/2.0/pcs/file' querydata = { 'method': 'delete', 'access_token': SynConfig.token['access_token'], 'path': pcspath } retcode, responses = sycurl.request(url, querydata, '', 'POST', SynCurl.Normal) responses = json.loads(responses) if retcode != 200 or 'error_code' in responses: if not slient: logger.error( 'Errno:%d: Delete remote file or directory "%s" failed: %s.' % (retcode, pcspath, responses['error_msg'])) return 1 if not slient: logger.info(' Delete remote file or directory "%s" completed.' % (pcspath)) return 0