upload_bg_3.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. def upload_file(upload_data, f):
  2. headers = utils.get_headers(upload_data['token'])
  3. version_id = upload_data['id']
  4. bg_blender.progress('uploading %s' % f['type'])
  5. upload_info = {
  6. 'assetId': version_id,
  7. 'fileType': f['type'],
  8. 'fileIndex': f['index'],
  9. 'originalFilename': os.path.basename(f['file_path'])
  10. }
  11. upload_create_url = paths.get_api_url() + 'uploads/'
  12. upload = rerequests.post(upload_create_url, json=upload_info, headers=headers, verify=True)
  13. upload = upload.json()
  14. #
  15. chunk_size = 1024 * 1024 * 2
  16. utils.pprint(upload)
  17. # file gets uploaded here:
  18. uploaded = False
  19. # s3 upload is now the only option
  20. for a in range(0, 5):
  21. if not uploaded:
  22. try:
  23. upload_response = requests.put(upload['s3UploadUrl'],
  24. data=upload_in_chunks(f['file_path'], chunk_size, f['type']),
  25. stream=True, verify=True)
  26. if upload_response.status_code == 200:
  27. uploaded = True
  28. else:
  29. print(upload_response.text)
  30. bg_blender.progress(f'Upload failed, retry. {a}')
  31. except Exception as e:
  32. print(e)
  33. bg_blender.progress('Upload %s failed, retrying' % f['type'])
  34. time.sleep(1)
  35. # confirm single file upload to bkit server
  36. upload_done_url = paths.get_api_url() + 'uploads_s3/' + upload['id'] + '/upload-file/'
  37. upload_response = rerequests.post(upload_done_url, headers=headers, verify=True)
  38. bg_blender.progress('finished uploading')
  39. return uploaded