【问题标题】:permission denied AWS EC2 file.save from Flask App来自 Flask App 的权限被拒绝 AWS EC2 file.save
【发布时间】:2016-03-23 01:50:36
【问题描述】:

我正在用 Flask 编写一个应用程序并将其部署在 EC2 中。我正在尝试让用户在本地目录中上传文件。但是在保存上传的文件时出现权限被拒绝错误。我附上下面的代码。请帮我解决它。我试图将文件夹权限更改为 sudo chmod -R 777 /var/www 但这没有帮助。我确实理解给予写权限是不好的,但这适用于开发模式并希望通过它 这是代码

@app.route('/upload', methods=['GET', 'POST'])
         def upload_file():
        # Get the name of the uploaded files
        uploaded_files = request.files.getlist("file[]")
        filenames = []
        for file in uploaded_files:
        # Check if the file is one of the allowed types/extensions
         if file and allowed_file(file.filename):
        # Make the filename safe, remove unsupported chars
        filename = secure_filename(file.filename)
        # Move the file form the temporal folder to the upload
        # folder we setup
        current_date = time.strftime("%x").split("/")           [0]+time.strftime("%x").split("/")[1]+time.strftime("%x").split("/")[2]
        directory = os.path.join('loadedfiles/')
        uploadedfilesdir = directory + current_date
        if not os.path.exists(uploadedfilesdir):
            os.makedirs(uploadedfilesdir)
        file.save(os.path.join(uploadedfilesdir, filename)) 
        # Save the filename into a list, we'll use it later
        filenames.append(filename)
        # Redirect the user to the uploaded_file route, which
        # will basicaly show on the browser the uploaded file
# Load an html page with a link to each uploaded file
return render_template('upload.html', filenames=filenames)

【问题讨论】:

  • 它可以帮助将上传目标文件夹的所有者和组更改为 www-data(或网络服务器运行的任何用户/组)

标签: python amazon-ec2 flask


【解决方案1】:

尝试给出绝对路径而不是这个

path = ./static/upload_folder

试试这个

path = /var/www/folder/folder/static/upload_folder

然后不要忘记通过此命令授予该文件夹的所有权限

sudo chmod -R 777 /var/www/folder/folder/static/upload_folder

这对我有用!

【讨论】:

  • 不要在普通文本上使用 Camel Case 并且绝对不要使用全部大写字母。编辑您的答案,让我取消投票。
【解决方案2】:

可能与这个question有关,将相对路径转换为绝对路径的解决方案。我不知道程序是如何在 EC2 上启动的,但似乎工作目录位于不允许执行的程序写入的某个 Python / 库目录中。使用绝对路径的修复可能如下所示:

directory = os.path.join('/var/www/', 'loadedfiles/', current_date)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2021-11-09
    • 2019-05-27
    • 2021-11-18
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    相关资源
    最近更新 更多