简体   繁体   中英

Django on Google App Engine: cannot upload images

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM