Flask上传权限被拒绝

时间:2018-02-12 02:41:32

标签: python linux flask flask-uploads

我正在使用DigitalOcean上的Flask和Flask Uploads托管一个Web服务器,我正在尝试将文件上传到静态文件夹,但是我收到了权限错误(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) 

2 个答案:

答案 0 :(得分:0)

  

有道理,但如何将UPLOADED_PHOTOS_DESTINATION设置为1   升级? '../'会工作吗?

你有没有尝试改变:

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

要:

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

我遇到了类似的问题,这可能是上述评论的含义。

答案 1 :(得分:-1)

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

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