1

我在 DigitalOcean 上使用 Flask 和 Flask Uploads 托管 Web 服务器,我正在尝试将文件上传到静态文件夹,但出现权限错误 ( OSError: [Errno 13] Permission denied: 'static')。

我已经检查了该文件夹的 Linux 权限ls -la static并返回:

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

2 回答 2

0

有道理,但是如何将 UPLOADED_PHOTOS_DESTINATION 设置为更高级别?会工作?

您是否尝试更改:

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

至:

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

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

于 2018-03-03T20:44:29.680 回答
-1

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

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

于 2021-01-07T22:29:56.247 回答