Background
I have a program where a client can see a list of files on the server, and then choose a file to upload from the server to Google Cloud Storage. While a file is uploading from the server to Google Cloud Storage, I want to let the client know what the progress of the file upload is. So far I'm able to show the progress of the upload on the server's terminal screen using the wrapattr method of tqdm, as shown below:
object_address = str(uuid.uuid4())
upload_url, upload_method = get_upload_url(object_address) # fetches signed upload URL
size = os.path.getsize(filename)
with open(filename, "rb") as in_file:
total_bytes = os.fstat(in_file.fileno()).st_size
with tqdm.wrapattr(
in_file,
"read",
total=total_bytes,
miniters=1,
desc="Uploading to my bucket",
) as file_obj:
response = requests.request(
method=upload_method,
url=upload_url,
data=file_obj,
headers={"Content-Type": "application/octet-stream"},
)
response.raise_for_status()
return object_address, size
Problem
I need to send the progress of the upload to the client. It's not helpful if you can only see the progress of the upload on the server's terminal screen.
Question
How can I retrieve the progress of the upload so that I can send it to the client?
Unfortunately, I don't think the wrapattr function has any parameters for callback functions that I could call every time a chunk is uploaded to Google Cloud Storage. I'm open to other ways I could retrieve progress without using the tqdm wrapattr method.
I know Google Cloud Storage has resumable uploads that allow you to track progress, but they use cURL so I would have to use a subprocess and I would rather use something cleaner than that.
Thanks!