Uploads a file for a given case
Step 1:
Create an entry for the file in the cloud. The pre-signed url is where you can upload the actual file using the 'upload_file'
method.
Returns: a json object with meta data for the file entry in the cloud
:param int case_Id:
id of the case you want to upload a file to
:param string file_name:
the name of the file
:param int size:
the size of the file in bytes
:param int component_Id:
optional parameter, id of the component that you want to upload a file for. Leave this parameter open if you want to upload a step file containging all components.
def get_file_upload_url(self, case_Id, file_name, size, component_Id=None) -> Dict:
file_url = self.fileserver_url + "/cases/" + str(case_Id) + "/files"
payload = json.dumps({"fileName": file_name,"fileSize": size, "caseComponentId": component_Id})
response = requests.post(file_url, data=payload, headers=self.put_header)
if(response.ok):
print("url acquired: \n", response.text, "\n")
return response.json()
else:
logger.error(f"failed to acquire url: {response.content}")
raise Exception
Step 2:
Upload a file to the given url. The url has to be a pre-signed url.
:param string presigned_url:
a pre signed url to where you want to upload your file
:param string file_path:
path to the file you want to upload.
API call function:
def upload_file(self, presigned_url, file_path):
with open(file_path, 'rb') as file:
file_data = file.read()
#response = requests.put(presigned_url, data=file_data, headers={'Content-Type': 'application/octet-stream'})
response = requests.put(presigned_url, data=file_data, headers=self.url_post_header)
if(response.ok):
print("file upload successful \n")
else:
logger.error(f"file upload failed: {response.content}")
raise Exception