Google App Engine 上的 Django:无法上传图片 [英] Django on Google App Engine: cannot upload images

查看:31
本文介绍了Google App Engine 上的 Django:无法上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的 Django 应用程序中的模型实现一个 ImageField.该应用程序在 Google App Engine 上运行.然而,在上传(本地机器,使用 GAE SDK 1.7.7)时,我得到一个 [Errno 78] 函数未实现.

错误源自对django.core.files.storage.FileSystemStorage._save()中的os.makedirs()的调用;调用 makedirs 的参数是:

u'/Users/.../PycharmProjects/myproject/media/uploaded

我在 SETTINGS.PY 中的 MEDIA_ROOT 条目包含:

/Users/.../PycharmProjects/myproject/media/

我在 SETTINGS.PY 中的 MEDIA_URL 条目包含:

/media/

媒体目录包含一个名为已上传"的子目录.我检查了权限,他们需要读/写访问权限.

我的 ImageField 的字段定义是:

image = models.ImageField(upload_to = "uploaded/"

出于某种原因,Django 想要创建已经存在的目录.使用 Django 控制台 os.path.exists(u'path/to/media/upload') 返回 True(这是正确的)所以我不明白为什么 Django 要创建目录.>

此外,我使用 Google Cloud SQL 进行存储并安装 PILLOW 进行图像处理.我还在 app.yaml 中添加了 PIL 作为库.

我可能遗漏了一些基本的东西,但目前我对导致这种情况的原因一无所知......

解决方案

是的,我认为您在这里遗漏了一些非常基本的东西.您无权访问 Google App Engine 上的文件系统,因此 os.makedirs() 将不起作用.如果您想上传图像(或一般文件),您必须将它们存储在 Blobstore 或 Google Cloud Storage 中.在做任何其他事情之前,我建议您先阅读 Blobstore Python API 概述 在这里您可以看到有关如何上传文件的完整示例.

此外,如果您将图片上传为 blob,您将能够通过 get_serving_url() 通过提供 blob_key.使用此 URL,您可以为特定图像请求任何大小,甚至无需任何额外工作即可在服务器端对其进行裁剪.

I'm trying to implement an ImageField for a model in my Django app. The app runs on Google App Engine. However upon upload (local machine, using GAE SDK 1.7.7) I get an [Errno 78] Function not implemented.

The error originates from a call to os.makedirs() in django.core.files.storage.FileSystemStorage._save(); the argument for the call to makedirs is:

u'/Users/.../PycharmProjects/myproject/media/uploaded

My MEDIA_ROOT entry in SETTINGS.PY contains:

/Users/.../PycharmProjects/myproject/media/

My MEDIA_URL entry in SETTINGS.PY contains:

/media/

The media directory contains a sub-directory named 'uploaded'. I checked the privileges and they have required read/write access.

The field definition for my ImageField is:

image = models.ImageField(upload_to = "uploaded/"

For some reason Django wants to create the directory which already exists. Using the Django console os.path.exists(u'path/to/media/upload') returns True (which is correct) so I do not understand why Django wants to create the directory.

Additionally, I use Google Cloud SQL for storage and installed PILLOW for image handling. I also added PIL as library in my app.yaml.

I'm probably missing something elementary, but am clueless at the moment to what's causing this...

解决方案

Yes, I think you are missing something very basic here. You don't have access to the file system on Google App Engine so the os.makedirs() won't work. If you want to upload images (or files in general) you have to store them in Blobstore or in Google Cloud Storage. Before doing anything else I would suggest you to go through the Blobstore Python API Overview where you can see the fully working example on how to upload files.

Further if you have images uploaded as blobs you will be able to get the image serving URL through get_serving_url() by providing the blob_key. With this URL you can request any size for a particular image or even crop it server side without any extra effort.

这篇关于Google App Engine 上的 Django:无法上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆