123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- def upload_file(upload_data, f):
- headers = utils.get_headers(upload_data['token'])
- version_id = upload_data['id']
- bg_blender.progress('uploading %s' % f['type'])
- upload_info = {
- 'assetId': version_id,
- 'fileType': f['type'],
- 'fileIndex': f['index'],
- 'originalFilename': os.path.basename(f['file_path'])
- }
- upload_create_url = paths.get_api_url() + 'uploads/'
- upload = rerequests.post(upload_create_url, json=upload_info, headers=headers, verify=True)
- upload = upload.json()
- #
- chunk_size = 1024 * 1024 * 2
- utils.pprint(upload)
- # file gets uploaded here:
- uploaded = False
- # s3 upload is now the only option
- for a in range(0, 5):
- if not uploaded:
- try:
- upload_response = requests.put(upload['s3UploadUrl'],
- data=upload_in_chunks(f['file_path'], chunk_size, f['type']),
- stream=True, verify=True)
- if upload_response.status_code == 200:
- uploaded = True
- else:
- print(upload_response.text)
- bg_blender.progress(f'Upload failed, retry. {a}')
- except Exception as e:
- print(e)
- bg_blender.progress('Upload %s failed, retrying' % f['type'])
- time.sleep(1)
- # confirm single file upload to bkit server
- upload_done_url = paths.get_api_url() + 'uploads_s3/' + upload['id'] + '/upload-file/'
- upload_response = rerequests.post(upload_done_url, headers=headers, verify=True)
- bg_blender.progress('finished uploading')
- return uploaded
|