0

我正在使用 Flask 在我的服务器 8Ubuntu 18.04 上托管一个 Web 应用程序。

我的应用程序的目标是上传图片(或任何类型的文件)。但是,当尝试将文件上传到 img 文件夹时,出现权限错误:

 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'

所以我知道存在权限错误。怎么可能解决这个问题?

我的文件结构如下:

在此处输入图像描述

我的代码(python3)__init__.py如下:

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()

对于upload-image.html,我如下:

<!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>

当它在本地工作时,我没有收到此错误,但只有当我部署 t 以便它可以在与我的同一 LAN 中的计算机上访问时。

这是我正在关注的教程:https ://www.youtube.com/watch?v=6WruncSoCdI

4

3 回答 3

0

为您要上传文件的服务器中的文件夹授予 777 权限。

于 2020-07-11T12:07:52.993 回答
-1

这是错误:

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

这是源自操作系统的错误,表示您无权在该目录中创建文件。

现在,当我说“你”没有权限时,我指的是正在运行该进程的用户。哪个用户取决于服务器的配置。

您可以运行chown以更改目录的所有者,以便它由将要访问它的用户拥有,或者您可以选择一个更简单的选项,即允许每个人都写入该目录,如果您不这样做'不要介意安全风险:

chmod 777 /var/www/cloud/cloud/static/img/uploads
于 2020-07-11T12:12:07.407 回答
-2

您必须调整此文件夹的权限。

这可以通过chmod设置权限和chown更改文件夹的所有者/组来完成。

如果您无法自行解决此问题,请向我们展示当前的权限/所有权。

您还需要知道哪个用户正在运行您的 Flask 应用程序。

于 2020-07-11T12:14:11.400 回答