I am hosting a web application on my server 8Ubuntu 18.04 using Flask.
The goal of my application is to upload a upload a picture (or any kind of files for now). However, when attempting to upload a file to the img folder I get permissions error :
ERROR:cloud:Exception on / [POST], referer: http://192.168.1.35/
Traceback (most recent call last):, referer: http://192.168.1.35/
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2447, in wsgi_app, referer: http://192.168.1.35/
response = self.full_dispatch_request(), referer: http://192.168.1.35/
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1952, in full_dispatch_request, referer: http://192.168.1.35/
rv = self.handle_user_exception(e), referer: http://192.168.1.35/
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1821, in handle_user_exception, referer: http://192.168.1.35/
reraise(exc_type, exc_value, tb), referer: http://192.168.1.35/
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in reraise, referer: http://192.168.1.35/
raise value, referer: http://192.168.1.35/
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1950, in full_dispatch_request, referer: http://192.168.1.35/
rv = self.dispatch_request(), referer: http://192.168.1.35/
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1936, in dispatch_request, referer: http://192.168.1.35/
return self.view_functions[rule.endpoint](**req.view_args), referer: http://192.168.1.35/
File "/var/www/cloud/cloud/__init__.py", line 15, in upload_image, referer: http://192.168.1.35/
image.save(os.path.join(app.config["IMAGE_UPLOADS"], image.filename)), referer: http://192.168.1.35/
File "/usr/local/lib/python3.6/dist-packages/werkzeug/datastructures.py", line 3066, in save, referer: http://192.168.1.35/
dst = open(dst, "wb"), referer: http://192.168.1.35/
PermissionError: [Errno 13] Permission denied: '/var/www/cloud/cloud/static/img/uploads/3E5A33B5-7E19-461A-B0C5-6835080675E7.jpeg', referer: http://192.168.1.35/
, referer: http://192.168.1.35/
AH00491: caught SIGTERM, shutting down
AH00489: Apache/2.4.29 (Ubuntu) mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
AH00094: Command line: '/usr/sbin/apache2'
So I understand that there is a permission Error. How is it possible to fix this ?
My file structure is the following:
My code (python3) for __init__.py
is the following :
import os
from flask import Flask, flash, request, redirect, url_for, render_template
app = Flask(__name__)
app.config["IMAGE_UPLOADS"] = "/var/www/cloud/cloud/static/img/uploads"
@app.route("/", methods = ["GET", "POST"])
def upload_image():
if request.method == "POST":
if request.files:
image = request.files["image"]
print(image)
image.save(os.path.join(app.config["IMAGE_UPLOADS"], image.filename))
return "IMAGE SAVED"
return render_template('upload-image.html')
if __name__ == "__main__":
app.run()
and for upload-image.html i is the following :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HELLO</title>
</head>
<body>
<div>
<h1>This is a test</h1>
<form action="/" method="POST" enctype="multipart/form-data">
<div>
<input type="file" name = "image" id="image">
<label for="image">Select Image</label>
</div>
<button type="submit">Upload</button>
</form>
</div>
</body>
</html>
I do not get this error when it is working locally but only when I deploy t so that it is accessible on the computers in the same LAN as mine.
This is the tutorial I am following : https://www.youtube.com/watch?v=6WruncSoCdI