【发布时间】: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