def get_file_from_bucket()

in microservices/hitl_service/src/routes/hitl.py [0:0]


def get_file_from_bucket(case_id: str,
                         uid: str,
                         download: Optional[bool] = False):
  storage_client = storage.Client()
  #listing out all blobs with case_id and uid
  blobs = storage_client.list_blobs(
      BUCKET_NAME, prefix=case_id + "/" + uid + "/", delimiter="/")

  target_blob = None
  #Selecting the last blob which would be the pdf file
  for blob in blobs:
    target_blob = blob

  #If file is not found raise 404
  if target_blob is None:
    return None, None

  filename = target_blob.name.split("/")[-1]
  #Downloading the pdf file into a byte string
  return_data = target_blob.download_as_bytes()

  #Checking for download flag and setting headers
  headers = None
  if download:
    headers = {"Content-Disposition": "attachment;filename=" + filename}
  else:
    headers = {"Content-Disposition": "inline;filename=" + filename}
  return (return_data, headers)