Flask 上传权限被拒绝

Flask Upload Permission Denied

我在 DigitalOcean 上托管一个带有 Flask 和 Flask Uploads 的网络服务器,我试图将文件上传到静态文件夹,但遇到权限错误 (OSError: [Errno 13] Permission denied: 'static')。

我已经使用 ls -la static 检查了该文件夹的 Linux 权限,然后返回:

drwxr-xr-x  7 root     root      4096 Feb 10 05:51 .
drwxr-xr-x  3 root     root      4096 Feb  2 07:04 ..
-rw-r--r--  1 root     root       262 Feb  2 06:36 dbconnect.py
-rw-r--r--  1 root     root       544 Feb  9 21:06 dbconnect.pyc
-rw-r--r--  1 root     root     41950 Feb 10 05:51 __init__.py
-rw-r--r--  1 www-data www-data 31080 Feb  2 08:14 __init__.pyc
drwxr-xr-x 10 root     root      4096 Jan 30 05:07 static
drwxr-xr-x  2 root     root      4096 Jan 30 05:07 templates
drwxr-xr-x  6 root     root      4096 Feb  9 20:45 venv

我使用的相同代码在我的本地 Windows 机器上运行良好,那么我还能尝试什么来允许我将文件上传到我的远程服务器?

Python代码:

import os, sys, os.path
from flask import Flask, render_template, flash, request, url_for, redirect, session, send_file, send_from_directory
from wtforms import Form, BooleanField, TextField, PasswordField, SelectField, RadioField, TextAreaField, DateField, DateTimeField, StringField, validators
from wtforms.widgets import TextArea
from wtforms.validators import DataRequired
from flask_wtf import FlaskForm, RecaptchaField
from flask_wtf.file import FileField, FileRequired, FileAllowed
from werkzeug.utils import secure_filename
from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_class
from passlib.hash import sha256_crypt
from MySQLdb import escape_string as thwart
from functools import wraps
from dbconnect import connection
app = Flask(__name__, static_folder='static')
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
app.config['UPLOADED_PHOTOS_DEST'] = 'static/user_info/prof_pic'
photos = UploadSet('photos', IMAGES)
configure_uploads(app, photos)
patch_request_class(app)  # set maximum file size, default is 16MB

class ProfilePictureForm(FlaskForm):
    prof_pic = FileField(validators=[FileAllowed(photos, u'Image only!')])

@app.route('/profile_picture_upload/', methods=['GET','POST'])
def profile_picture_upload():
    form = ProfilePictureForm()
    cid = str(session['clientcid'])
    first_name = session['first_name']
    #default_prof_pic = 'app/uploads/photos/static/user_info/prof_pic/default.jpg'
    #user_prof_pic = cid+'_'+first_name+'_'+'.png'
    if form.validate_on_submit():
        # Checks if the prof_pic is set yet. if set, then dont need to delete the old picture on the server
        if session['prof_pic'] != url_for('static', filename='user_info/prof_pic/default.jpg'):
            #need to delete or move the old prof_pic if it was set! Prevents users from adding too many pictures
            os.remove('static/user_info/prof_pic/'+cid+'_'+first_name+'.png')
            flash("You already have a file on the server!")
        filename = photos.save(form.prof_pic.data, name=cid+'_'+first_name+'.png')
        file_url = photos.url(filename) 
        session['prof_pic'] = file_url
        c, conn = connection()
        c.execute("UPDATE cpersonals SET prof_pic = %s WHERE cid = (%s)", (file_url, cid))

        conn.commit()
        c.close()
        conn.close()
    else:
        file_url = None

    return render_template('profile_picture_upload.html', form=form, file_url=file_url) 

Makes sense, but how do I set the UPLOADED_PHOTOS_DESTINATION to one up a level? Would ‘../‘ work?

您是否尝试更改:

app.config['UPLOADED_PHOTOS_DEST'] = 'static/user_info/prof_pic'

收件人:

app.config['UPLOADED_PHOTOS_DEST'] = 'app/static/user_info/prof_pic'

我 运行 遇到了类似的问题,这可能就是上面评论的意思。

试试看 os.curdir+'/static/assets/img/'

看来我们需要在上传目录前面加上点才能正常工作