Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing BlobKey in DataStore with app engine

So I decided to rewrite my image gallery because of the new high performance image serving thing. That meant using Blobstore which I have never used before. It seemed simple enough until I tried to store the BlobKey in my model.

How on earth do I store reference to a blobstorekey in a Model? Should I use string or should I use some special property that I don't know about? I have this model

class Photo(db.Model):
 date = db.DateTimeProperty(auto_now_add=True)
 title = db.StringProperty()
 blobkey = db.StringProperty()
 photoalbum = db.ReferenceProperty(PhotoAlbum, collection_name='photos') 

And I get this error: Property blobkey must be a str or unicode instance, not a BlobKey

Granted, I am a newbie in app engine but this is the first major wall I have hit yet. Have googled around extensively without any success.

like image 823
Símon Óttar Vésteinsson Avatar asked Aug 27 '10 20:08

Símon Óttar Vésteinsson


1 Answers

The following works for me. Note the class is blobstore.blobstore instead of just blobstore.

Model:

from google.appengine.ext.blobstore import blobstore

class Photo(db.Model):
  imageblob = blobstore.BlobReferenceProperty()

Set the property:

from google.appengine.api import images
from google.appengine.api import blobstore
from google.appengine.ext.webapp import blobstore_handlers

class UploadHandler(blobstore_handlers.BlobstoreUploadHandler):
  def post(self):
    upload_files = self.get_uploads('file')  # 'file' is file upload field in the form
    blob_info = upload_files[0]
    entity = models.db.get(self.request.get('id'))
    entity.imageblob = blob_info.key()

Get the property:

image_url = images.get_serving_url(str(photo.imageblob.key()))
like image 84
Kenan Avatar answered Oct 18 '22 02:10

Kenan