I am using Flask as a backend for a mobile application, and one of its feature is uploading and renaming images for profile pictures and post media attachment
Lately, the upload webservices return FileNotFoundError: [Errno 2] No such file or directory with a 500 Error when using them on the development server
I don't really think it's a code issue as it works fine on my local environment, but it crashes over there.
This is the traceback from the dev server
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/var/www/xxxxx.com/xxxxx/controllers/posts.py", line 14, in posts
return posts_service.post(request)
File "/usr/local/lib/python3.6/dist-packages/flask_jwt/__init__.py", line 177, in decorator
return fn(*args, **kwargs)
File "/var/www/xxxxx.com/xxxxx/services/posts.py", line 82, in post
file.save(filepath)
File "/usr/local/lib/python3.6/dist-packages/werkzeug/datastructures.py", line 2800, in save
dst = open(dst, "wb")
FileNotFoundError: [Errno 2] No such file or directory: 'xxxxx/uploads/fc23dfb1-6852-47cc-9090-4aa80a1f1d9d.jpe'
And here is the post function
@jwt_required()
def post(request):
if 'file' not in request.files:
return {
'success': False,
'message': 'No file part in the request',
}, 200
file = request.files['file']
if file.filename == '':
return {
'success': False,
'message': 'No file selected for uploading',
}, 200
if file and allowed_file(file.mimetype):
filepath = makepath(file.mimetype)
file.save(filepath)
post = post_repo.create(
current_identity.id,
request.form['matchId'],
request.form['teamSelected'],
request.form['content'],
request.form['postType'],
filepath,
request.form['location']
)
if post:
return {
'success': True,
'message': 'Post successfully created',
'post': post_repo.to_dto(post),
}, 201
return {
'success': False,
'message': 'File extension not allowed',
}, 200
I have already chmoded the uploads directory to be writeable by others
The dev environment is hosted within a docker environment using apache 2.4 and wsgi in order to serve it
What should I ask the server administrators to check in there?
Edit: I just noticed that when I ls -la the directory, there is no group column. Could that be why the uploads are not working?
[echo] Check final permissions
[exec] drwxr-xrwx 2 33 www-data 4096 Feb 10 09:35 .
[exec] drwxr-xr-x 12 33 www-data 4096 Feb 10 09:35 ..
[exec] -rw-r--r-- 1 33 www-data 1791 Feb 10 09:35 726f052d-2ad8-41cd-98fa-41eb2c9ba728.png
[exec] -rw-r--r-- 1 33 www-data 12605 Feb 10 09:35 camera.jpg
[exec] -rw-r--r-- 1 33 www-data 4208 Feb 10 09:35 camera.png
Update: As suggested in the comments, I have tried using absolute path when using the save function, which resulted in an odd error message in the stacktrace, it generated something else, like this FileNotFoundError: [Errno 2] No such file or directory: '/xxxxx/uploads/b94a6fd0-af54-45f1-95d4-5cbe1dc7ca38.jpe', adding a / just before the folder name, and it makes sense why it would fail here, as that path doesn't exist on the server