Hello everyone, My web instance is a Flask app that processes file uploads. When a user uploads a file, DropzoneJs breaks the file and send it to the backend chunk by chunk one after another. When the last chunk of the file has been sent, the app sends the file to a Backblaze bucket.
To achieve this, the files are written to a path in a static empty ‘temp’ folder and appended to in binary mode. ("ab")
The expected behaviour for the ‘ab’ mode is to create the file if does not exist and append to it in binary.
‘ab’
Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
My app works well locally but on render, I get the FileNotFoundError
. My wild guess is that the Render server has some file permission constraint.
Here’s the code segment of the view that uploads the file:
...
from pathlib import Path
...
#Inside view func
...
file = request.files.get("file", None)
if file:
uploaded_file = file
uploaded_file_uid = request.form['dzuuid']
unique_filename = f"{uploaded_file_uid[:8]}_{secure_filename(uploaded_file.filename)}"
current_chunk = int(request.form["dzchunkindex"])
temp_dir = Path(__file__).resolve().parent.parent / "static" / "temp"
#temp_dir should be: /opt/render/project/src/src/static/temp/
temp_file = Path(temp_dir / unique_filename)
current_app.logger.info(f"Does temp-file exist?: {temp_file.exists()}")
#LOCALLY, the above line is False on receiving the first chunk and True on
#subsequent chunk uploads
with open(temp_file, "ab") as f:
current_app.logger.info(f"Does temp-file exist?: {temp_file.exists()}") #True on Local
f.seek(int(request.form["dzchunkbyteoffset"]))
f.write(uploaded_file.stream.read())
...
...
os.remove(temp_file)
The error log:
Oct 12 12:04:01 AM File "/opt/render/project/src/src/utils/decorators.py", line 13, in decorated_function
Oct 12 12:04:01 AM return func(*args, **kwargs)
Oct 12 12:04:01 AM File "/opt/render/project/src/src/resource/routes.py", line 179, in file_upload
Oct 12 12:04:01 AM with open(temp_file, "ab") as f:
Oct 12 12:04:01 AM FileNotFoundError: [Errno 2] No such file or directory: '/opt/render/project/src/src/static/temp/f6d2f007_Testing-1-2.txt'
Start command for the project:
$ gunicorn --workers 1 --threads 3 --worker-class 'gthread' wsgi:app