1

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:

enter image description here

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

lolaa
  • 181
  • 1
  • 4
  • 11

3 Answers3

0

Give 777 permission to folder in server where you want to upload files.

Vipin Farswan
  • 318
  • 4
  • 12
-1

This is the error:

PermissionError: [Errno 13] Permission denied: '/var/www/cloud/cloud/static/img/uploads/3E5A33B5-7E19-461A-B0C5-6835080675E7.jpeg', referer: http://192.168.1.35/

That is an error originating from the operating system, saying that you do not have the permission to create a file in that directory.

Now, when I say "you" don't have the permission, I mean the user which is running the process. Which user that is depends on the configuration of the server.

You can run chown to change the owner of the directory, so that it is owned by the user which is going to access it, or you can go for a simpler option, which is to allow everyone to write to that directory, if you don't mind the security risk:

chmod 777 /var/www/cloud/cloud/static/img/uploads
zvone
  • 18,045
  • 3
  • 49
  • 77
  • 1
    Never change permissions to 777. Please do not advice beginners to do this. – Jürgen Gmach Jul 11 '20 at 12:15
  • @J.G. I did write _"if you don't mind the security risk"_ and I did offer a different option with `chown`. – zvone Jul 11 '20 at 12:21
  • Would you advice somebody, who is not able to judge the situation, to drive a car without brakes? I am sorry. Bad advice stays bad advice. Endangering the server is no acceptable workaround. I know. You only want to help. Unfortunately, there is no shortcut to host a web application on your own server. – Jürgen Gmach Jul 11 '20 at 13:04
  • @J.G. Please refrain from starting arguments and provide a better answer if you think the existing ones are not good enough. Saying "that is no good" to every offered solution, withut offering a better one, does not help anyone. – zvone Jul 11 '20 at 13:42
  • I already created an answer before I commented here. There is not much to add. Without all information available (user/group of process, user/group of directory, permission of directory) it is impossible to give a specific answer. We have to wait until @lolaa answers my requests. Please do not take this personal - it was never meant to be. Setting up web servers and creating web applications is my job since the nineties. This certainly does not mean I know everything, but maybe take into account theoretically I could be right. All the best! – Jürgen Gmach Jul 11 '20 at 17:34
-1

You have to adjust the permissions for this folder.

This can be done with a combination of chmod to set permissions and chown to change the owner/group of the folder.

If you have trouble to fix this on your own, please show us the current permissions / ownership.

And also you need to know which user is running your Flask app.

Jürgen Gmach
  • 5,366
  • 3
  • 20
  • 37