Flask文件写入权限 [英] Flask permissions for file writing

查看:528
本文介绍了Flask文件写入权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在EC2 Ubuntu实例上运行Flask webserver。目的是捕获传入POST的主体,将其写入本地文件,然后将该文件上传到S3。



代码基本上是:

  @ app.route('/',methods = ['GET','POST'])
@ app.route ('/index.html',methods = ['GET','POST'])
def index_home():
data = request.data
with open('test.json' ,'w')as f:
f.write(data)
##上传东西到S3

当我在本地Flask web服务器实例上运行它并从Postman发送带有json主体的POST时,它完美地工作。但在EC2实例上,我得到了'test.json'文件的权限错误(根据apache error.log),导致页面加载时出现500错误。



我搜刮了Google和Stackoverflow(这是一个类似的问题,没有解决方案)无济于事。这似乎是一个简单的问题,但我已经尝试了一切,似乎无法得到它的工作:我已经将我的用户添加到www数据组,我已经更改了/ var / www文件夹和文件的权限到root,ubuntu(默认EC2 Ubuntu用户)和www-data我能想到的每一个组合,我已经直接777的目录...似乎没有任何工作。



显然,我有点新手。是否有一个配置文件或需要调整,以使这个工作?

解决方案

你应该确保程序是实际试图写入你想要写入的目录。这可能是因为它试图写入Python二进制文件(或其他)的目录,这取决于你的命令和当前的工作目录。
出于测试的目的,您可能会尝试更改路径(请确保/ tmp可写入用户,情况应该如此):

<$ p $ ($ / $ / $ / $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' pre>

I'm running a Flask webserver on an EC2 Ubuntu instance. The purpose is to capture the body of an incoming POST, write it to a file locally, then upload that file to S3.

The code is, basically:

@app.route('/', methods=['GET','POST'])
@app.route('/index.html', methods=['GET','POST'])
def index_home():
    data = request.data
    with open('test.json', 'w') as f:
        f.write(data)
## Upload the stuff to S3

When I run it on the local Flask webserver instance and send a POST with a json body from Postman, it works perfectly. But on the EC2 instance I'm getting a permissions error (according to the apache error.log) on the 'test.json' file, which results in 500 error on page-load.

I've scoured Google and Stackoverflow (Here is a similar question, with no solution) to no avail. This seems like an easy problem, but I've tried everything and can't seem to get it to work: I've added my user to the www-data group, I've changed the /var/www folders and files permissions to every combination of root, ubuntu (the default EC2 Ubuntu user) and www-data I could think of, I've straight 777 the directories...nothing seems to work.

Obviously, I'm a bit of a newbie. Is there a configuration file or something that requires a tweak to get this to work?

解决方案

You should make sure the program is actually trying to write in the directory you want it to write to. It could be that it tries to write into the directory of the Python binary (or anything else), that depends on your command and the current working directory. For testing purposes you might try to change the path like so (make sure /tmp is writeable for the user, which should be the case):

with open('/tmp/test.json', 'w') as f:
    f.write(data)

这篇关于Flask文件写入权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆