'Flask-Uploads Permission Denied

I'm using Flask Uploads for an upload form in my Flask application. However, whenever I try to save a file I get this error:

File "/Users/Documents/virtual_environment/bin/../lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/app'

It seems like uploads doesn't have the necessary permissions to save files? Here's the configuration I'm using for flask-uploads:

UPLOADS_DEFAULT_URL = os.environ.get("UPLOADS_URL", "http://localhost:5000/")
UPLOADS_DEFAULT_DEST = "/app/uploads/"
UPLOAD_EXTENSIONS = set(["csv", "xls", "xsls"])

Also, here's how I save the actual file:

@app.route('/upload', methods = ['GET', 'POST'])
@app.route('/upload/', methods = ['GET', 'POST'])
@roles_accepted('admin', 'team')
def r_upload():
    form = FileUploadForm()

    if form.validate_on_submit():
        filename = uploadSet.save(form.uploadfile.data)
        url = uploadSet.url(filename)
        flash("%s uploaded <a href=\'%s\'>HERE</a>!" % (filename, url))

    return render_template('/uploads.html',
        dashboard_title = "%s Uploads" % g.name,
        form = form)

The error is caused by the save line. Any suggestions on how to fix this? Thanks.



Solution 1:[1]

Does your linux user have file permissions on /app/uploads/? Check that using ls -la /app/uploads.

Note that /app would try to write files in the root of the filesystem at /.

If you are looking to write files within your app, use app/uploads instead of /app/uploads/. From the error, it looks like /app simply does not exist and writing to app/uploads is precisely what you wanted to do.

Solution 2:[2]

I know this is 5 years late, but I'll put this here for people who see this in the future.

My solution was to create an empty file with the same name (open("path/to/file.png").close()) and then save the file

Solution 3:[3]

If you are also facing problem of (PermissionError: [Errno 13] Permission denied:) so may this is helpful for you.

#imports from flask import Flask, render_template, reques

#code [python] @app.route("/img", methods=["GET", "POST"]) def get_img(): r = request.files.get("file1") with open("n.png", "wb") as fp: for itm in r: fp.write(itm) return "done"

code [html]

<html>
<head>

</head>

<body>
<form action="/img" method="post" enctype="multipart/form-data">
    <input type="file" id="22" style="margin-top: 100px;" name="file1">
    <button type="submit" style="margin-top: 100px;">send</button>
</form>
</body>
</html>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 R242