如何通过Django中的表单数据发布将文件上传到GAE?

问题描述 投票:0回答:1

我有我的Django Webapp,其中包含一个模型中的方法,该方法可将用户选择的文件(从表单)上传到指定的文件目录。但是在将我的应用程序部署到Google App Engine(使用gcloud)后,我无法使用该模型。我正在为我的Django应用程序使用Google MySQL数据库。另外,由于服务器提示存在只读权限,因此我无法在我的应用程序中使用os.makedir()方法。

MODELS.PY

def upload_to(instance, filename):
    now = timezone_now()
    base, ext = os.path.splitext(filename)
    ext = ext.lower()
    rootpath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    return rootpath + f"/face_detector/static/face_detector/img/{now:%Y%m%d%H%M%S}{ext}"
    # Do add the date as it prevents the same file name from occuring twice
    # the ext is reserved for the file type and don't remove it


class PicUpClass(models.Model):
    picture = models.ImageField(_("picture"), upload_to=upload_to, blank=True, null=True)

我正在获取错误

/ add-dataset [Errno 30]处的OSError,只读文件系统:'/srv/face_detector/static/face_detector/img/20200228084934.jpg'请求方法:POST请求网址:https://agevent-269406.appspot.com/add-dataset Django版本:3.0.2异常类型:OSError异常值:[Errno 30]只读文件系统:'/srv/face_detector/static/face_detector/img/20200228084934.jpg'例外位置:/env/lib/python3.7/site-packages/django/core/files/storage.py在_save中,第267行Python可执行文件:/env/bin/python3.7 Python版本:3.7.6

我是python的新秀,所以请向我建议一些基本的解决方案。

我有我的Django Webapp,其中包含一个模型中的方法,该方法可将用户选择的文件(从表单)上传到指定的文件目录。但是,在部署我的...

django python-3.x google-app-engine google-cloud-datastore gcloud
1个回答
0
投票

出现此只读文件系统错误的原因是默认模型。ImageField将上传的图像保存到您的项目文件夹中,并且它是read-only for Google App Engine apps

© www.soinside.com 2019 - 2024. All rights reserved.