FileNotFoundError: [Errno 2] No such file or directory: /opt/render/project/src/src/static/temp/f6d2f007_Testing-1-2.txt'

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

Hi there,

This shouldn’t be an issue from a permissions perspective on Render.

To test this out you can try simply writing a file to that location, i.e.

touch /opt/render/project/src/src/static/temp/f6d2f007_Testing-1-2.txt
echo test >> /opt/render/project/src/src/static/temp/f6d2f007_Testing-1-2.txt
cat /opt/render/project/src/src/static/temp/f6d2f007_Testing-1-2.txt

You can give that a try to confirm you have the necessary permissions to read and write to that directory.

If that works, I’d suggest a bit more troubleshooting in the application code. It could be worth making sure the directories are created before trying to write to the file, or maybe even creating the file first (despite what the docs say).

Regards,

Matt

1 Like

So I have solved the issue. And just like you said, @mmadex, it wasn’t a permission issue from Render.

Though I had an empty ‘temp’ folder in my project directory, it was not created. I have no idea why, probably because it was empty.

This was the fix:

...
current_chunk = int(request.form["dzchunkindex"])
temp_dir = Path(__file__).resolve().parent.parent / "static" / "temp" 
if not temp_dir.exists():
    temp_dir.mkdir() #Creates the temp folder

temp_file = Path(temp_dir / unique_filename)
if current_chunk == 0:
    temp_file.touch() #Creates the file successfully

with open(temp_file, "ab") as f:
    f.seek(int(request.form["dzchunkbyteoffset"]))
    f.write(uploaded_file.stream.read())
...

Thank you so much. I really appreciate the help. And also, please what is the storage limit for the free tier?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.