def Upload(filepath, remote_path=None, user=GetConfig('default_pan')): token = GetToken(user=user) headers = { 'Authorization': 'bearer {}'.format(token), 'Content-Type': 'application/json' } if remote_path is None: remote_path = os.path.basename(filepath) if remote_path.endswith('/'): remote_path = os.path.join(remote_path, os.path.basename(filepath)) if not remote_path.startswith('/'): remote_path = '/' + remote_path remote_path = remote_path.replace('//', '/') filesize = header._filesize(filepath) InfoLogger().print_r('local file path:{}, remote file path:{}'.format( filepath, remote_path)) if filesize < 1024 * 1024 * 3.25: for msg in _upload(filepath, remote_path, user): 1 else: session_data = CreateUploadSession(remote_path, user) if session_data == False: return {'status': 'file exists!'} else: if session_data.get('uploadUrl'): InfoLogger().print_r('start upload {}'.format(filepath)) uploadUrl = session_data.get('uploadUrl') for msg in UploadSession(uploadUrl, filesize, filepath, user): 1 else: InfoLogger().print_r(session_data.get('error').get('msg')) InfoLogger().print_r( 'create upload session fail! {}'.format(remote_path)) return {'status': 'create upload session fail!'}
def Upload_for_server(filepath,remote_path=None,user=GetConfig('default_pan')): token=GetToken(user=user) headers={'Authorization':'bearer {}'.format(token),'Content-Type':'application/json'} headers.update(default_headers) if remote_path is None: remote_path=os.path.basename(filepath) if remote_path.endswith('/'): remote_path=os.path.join(remote_path,os.path.basename(filepath)) if not remote_path.startswith('/'): remote_path='/'+remote_path filepath=convert2unicode(filepath) remote_path=convert2unicode(remote_path.replace('//','/')) filesize=header._filesize(filepath) InfoLogger().print_r('local file path:{}, remote file path:{}'.format(filepath,remote_path)) if filesize<4 * 1024 * 1024: for msg in _upload(filepath,remote_path,user): yield msg else: session_data=CreateUploadSession(remote_path,user) if session_data==False: yield {'status':'file exists!'} else: if session_data.get('uploadUrl'): InfoLogger().print_r('start upload {}'.format(filepath)) uploadUrl=session_data.get('uploadUrl') for msg in UploadSession(uploadUrl,filesize,filepath,user): yield msg else: # InfoLogger().print_r('user:{} create upload session fail! {},{}'.format(user,remote_path,session_data.get('error').get('message'))) yield {'status':'user:{};create upload session fail!{}'.format(user,session_data.get('error').get('message'))}
def Upload(filepath, remote_path=None, user='******'): token = GetToken(user=user) headers = { 'Authorization': 'bearer {}'.format(token), 'Content-Type': 'application/json' } if remote_path is None: remote_path = os.path.basename(filepath) if remote_path.endswith('/'): remote_path = os.path.join(remote_path, os.path.basename(filepath)) if not remote_path.startswith('/'): remote_path = '/' + remote_path if header._filesize(filepath) < 1024 * 1024 * 3.25: for msg in _upload(filepath, remote_path, user): 1 else: session_data = CreateUploadSession(remote_path, user) if session_data == False: return {'status': 'file exists!'} else: if session_data.get('uploadUrl'): uploadUrl = session_data.get('uploadUrl') for msg in UploadSession(uploadUrl, filepath, user): 1 else: print(session_data.get('error').get('msg')) print('create upload session fail! {}'.format(remote_path)) return {'status': 'create upload session fail!'}
def _upload( filepath, remote_path, user=GetConfig('default_pan')): #remote_path like 'share/share.mp4' app_url = GetAppUrl(user) od_type = get_value('od_type', user) token = GetToken(user=user) headers = {'Authorization': 'bearer {}'.format(token)} headers['Content-Type'] = 'application/octet-stream' headers.update(default_headers) if od_type == 'nocn' or od_type is None or od_type == False: url = app_url + 'drive/root:{}:/content'.format( urllib.quote(convert2unicode(remote_path))) else: url = app_url + '_api/v2.0/me/drive/root:{}:/content'.format( urllib.quote(convert2unicode(remote_path))) timeCalc = TimeCalculator() r = browser.put(url, headers=headers, data=open(filepath, 'rb'), verify=False) # r=CurlUpload(url,headers,open(filepath,'rb')) try: data = json.loads(r.content) trytime = 1 while 1: try: if data.get('error'): InfoLogger().print_r('upload {} fail; reason: {}'.format( filepath, data.get('error').get('message'))) yield {'status': 'upload fail!'} break elif r.status_code == 201 or r.status_code == 200: speed = CalcSpeed(header._filesize(filepath), timeCalc.PassNow())['kb'] InfoLogger().print_r('upload {} success! speed:{}'.format( filepath, speed)) AddResource(data, user) yield {'status': 'upload success!', 'speed': speed} break else: InfoLogger().print_r(data) yield {'status': 'upload fail!'} break except Exception as e: exstr = traceback.format_exc() trytime += 1 ErrorLogger().print_r( 'error to opreate _upload("{}","{}"), try times {},error:{}' .format(filepath, remote_path, trytime, exstr)) yield {'status': 'upload fail! retry!'} if trytime > 3: yield {'status': 'upload fail! touch max retry time(3)'} break except Exception as e: exstr = traceback.format_exc() ErrorLogger().print_r(u'upload fail!{}'.format(exstr)) ErrorLogger().print_r(r.content)
def ContinueUpload(filepath, uploadUrl, user): headers = {'Content-Type': 'application/json'} headers.update(default_headers) r = requests.get(uploadUrl, headers=headers) data = json.loads(r.text) offset = data.get('nextExpectedRanges')[0].split('-')[0] expires_on = time.mktime(parse(data.get('expirationDateTime')).timetuple()) if time.time() > expires_on: yield 'alright expired!' else: length = 327680 * 10 trytime = 1 filesize = header._filesize(filepath) while 1: result = _upload_part(uploadUrl, filepath, offset, length, trytime=trytime) code = result['code'] #上传完成 if code == 0: AddResource(result['info'], user) yield {'status': 'upload success!'} break #分片上传成功 elif code == 1: trytime = 1 offset = result['offset'] per = round((float(offset) / filesize) * 100, 1) yield {'status': 'partition upload success! {}%'.format(per)} #错误,重试 elif code == 2: if result['sys_msg'] == 'The request has been throttled': InfoLogger().print_r(result['sys_msg'] + ' ; wait for 1800s') yield { 'status': 'The request has been throttled! wait for 1800s' } time.sleep(1800) offset = offset trytime = result['trytime'] yield {'status': 'partition upload fail! retry!'} #重试超过3次,放弃 elif code == 3: yield { 'status': 'partition upload fail! touch max retry times!' } break
def UploadSession(uploadUrl, filepath, user): length = 327680 * 10 offset = 0 trytime = 1 filesize = header._filesize(filepath) while 1: result = _upload_part(uploadUrl, filepath, offset, length, trytime=trytime) code = result['code'] #上传完成 if code == 0: AddResource(result['info'], user) yield {'status': 'upload success!', 'uploadUrl': uploadUrl} break #分片上传成功 elif code == 1: trytime = 1 offset = result['offset'] per = round((float(offset) / filesize) * 100, 1) yield { 'status': 'partition upload success! {}%'.format(per), 'uploadUrl': uploadUrl } #错误,重试 elif code == 2: if result['sys_msg'] == 'The request has been throttled': InfoLogger().print_r(result['sys_msg'] + ' ; wait for 1800s') yield { 'status': 'The request has been throttled! wait for 1800s', 'uploadUrl': uploadUrl } time.sleep(1800) offset = offset trytime = result['trytime'] yield { 'status': 'partition upload fail! retry!', 'uploadUrl': uploadUrl } #重试超过3次,放弃 elif code == 3: yield { 'status': 'partition upload fail! touch max retry times!', 'uploadUrl': uploadUrl } break
def Upload_for_server(filepath, remote_path=None, user='******'): token = GetToken(user=user) headers = { 'Authorization': 'bearer {}'.format(token), 'Content-Type': 'application/json' } if remote_path is None: remote_path = os.path.basename(filepath) if remote_path.endswith('/'): remote_path = os.path.join(remote_path, os.path.basename(filepath)) if not remote_path.startswith('/'): remote_path = '/' + remote_path filepath = convert2unicode(filepath) remote_path = convert2unicode(remote_path) print('local file path:{}, remote file path:{}'.format( filepath, remote_path)) if header._filesize(filepath) < 1024 * 1024 * 3.25: for msg in _upload(filepath, remote_path, user): yield msg else: session_data = CreateUploadSession(remote_path, user) if session_data == False: yield {'status': 'file exists!'} else: if session_data.get('uploadUrl'): uploadUrl = session_data.get('uploadUrl') for msg in UploadSession(uploadUrl, filepath, user): yield msg else: print('user:{} create upload session fail! {},{}'.format( user, remote_path, session_data.get('error').get('message'))) yield { 'status': 'user:{};create upload session fail!{}'.format( user, session_data.get('error').get('message')) }
def _upload_part(uploadUrl, filepath, offset, length, trytime=1): size = header._filesize(filepath) offset, length = map(int, (offset, length)) if offset > size: InfoLogger().print_r('offset must smaller than file size') return {'status': 'fail', 'msg': 'params mistake', 'code': 1} length = length if offset + length < size else size - offset endpos = offset + length - 1 if offset + length < size else size - 1 InfoLogger().print_r('upload file {} {}%'.format( filepath, round(float(endpos) / size * 100, 1))) filebin = header._file_content(filepath, offset, length) headers = {} # headers['Authorization']='bearer {}'.format(token) headers['Content-Length'] = str(length) headers['Content-Range'] = 'bytes {}-{}/{}'.format(offset, endpos, size) headers.update(default_headers) try: r = requests.put(uploadUrl, headers=headers, data=filebin) data = json.loads(r.content) if r.status_code == 201 or r.status_code == 200: InfoLogger().print_r(u'{} upload success!'.format(filepath)) return { 'status': 'success', 'msg': 'all upload success', 'code': 0, 'info': data } elif r.status_code == 202: offset = data.get('nextExpectedRanges')[0].split('-')[0] return { 'status': 'success', 'msg': 'partition upload success', 'code': 1, 'offset': offset } else: trytime += 1 if trytime <= 3: return { 'status': 'fail', 'msg': 'please retry', 'sys_msg': data.get('error').get('message'), 'code': 2, 'trytime': trytime } else: return { 'status': 'fail', 'msg': 'retry times limit', 'sys_msg': data.get('error').get('message'), 'code': 3 } except Exception as e: ErrorLogger().print_r( 'error to opreate _upload_part("{}","{}","{}","{}"), try times {},reason:{}' .format(uploadUrl, filepath, offset, length, trytime, e)) trytime += 1 if trytime <= 3: return { 'status': 'fail', 'msg': 'please retry', 'code': 2, 'trytime': trytime, 'sys_msg': '' } else: return { 'status': 'fail', 'msg': 'retry times limit', 'code': 3, 'sys_msg': '' }