在 Apache2 ubuntu 上授予 Python Flask 应用程序 运行 权限
Give permission to Python Flask application running on Apache2 ubuntu
我是 运行 Ubuntu 上 Apache2 服务器上的 Flask 应用程序。该应用程序将从表单中获取输入并将其保存到文本文件中。该文件仅在上传到 S3 时存在。之后就删除了。:
foodforthought = request.form['txtfield']
with open("filetos3.txt", "w") as file:
file.write(foodforthought)
file.close()
s3.Bucket("bucketname").upload_file(Filename = "filetos3.txt", Key = usr+"-"+str(datetime.now()))
os.remove("filetos3.txt")
但该应用没有创建文件的权限:
[Errno 13] Permission denied: 'filetos3.txt'
我已经尝试授予应用程序所在文件夹的权限:
sudo chmod -R 777 /var/www/webApp/webApp
但是没用
我的猜测是该应用程序 运行 来自不同的位置。你从中得到什么输出:
import os
print(os.getcwd())
您需要为该目录设置权限。更好的是,使用绝对路径。由于该文件是临时文件,因此使用 tempfile
作为 detailed here.
foodforthought = request.form['txtfield']
with tempfile.NamedTemporaryFile() as fd:
fd.write(foodforthought)
fd.flush()
# Name of file is in the .name attribute.
s3.Bucket("bucketname").upload_file(Filename = fd.name, Key = usr+"-"+str(datetime.now()))
# The file is automatically deleted when closed, which is when the leaving the context manager.
一些最后的说明:您不需要 close
文件,因为您使用了上下文管理器。另外,避免递归设置 777。最安全的方法是设置 +wX
以便仅在目录上设置 execute
位,在所有内容上设置 write
位。或者更好,更具体。
我是 运行 Ubuntu 上 Apache2 服务器上的 Flask 应用程序。该应用程序将从表单中获取输入并将其保存到文本文件中。该文件仅在上传到 S3 时存在。之后就删除了。:
foodforthought = request.form['txtfield']
with open("filetos3.txt", "w") as file:
file.write(foodforthought)
file.close()
s3.Bucket("bucketname").upload_file(Filename = "filetos3.txt", Key = usr+"-"+str(datetime.now()))
os.remove("filetos3.txt")
但该应用没有创建文件的权限:
[Errno 13] Permission denied: 'filetos3.txt'
我已经尝试授予应用程序所在文件夹的权限:
sudo chmod -R 777 /var/www/webApp/webApp
但是没用
我的猜测是该应用程序 运行 来自不同的位置。你从中得到什么输出:
import os
print(os.getcwd())
您需要为该目录设置权限。更好的是,使用绝对路径。由于该文件是临时文件,因此使用 tempfile
作为 detailed here.
foodforthought = request.form['txtfield']
with tempfile.NamedTemporaryFile() as fd:
fd.write(foodforthought)
fd.flush()
# Name of file is in the .name attribute.
s3.Bucket("bucketname").upload_file(Filename = fd.name, Key = usr+"-"+str(datetime.now()))
# The file is automatically deleted when closed, which is when the leaving the context manager.
一些最后的说明:您不需要 close
文件,因为您使用了上下文管理器。另外,避免递归设置 777。最安全的方法是设置 +wX
以便仅在目录上设置 execute
位,在所有内容上设置 write
位。或者更好,更具体。