1
votes

I have an entity which is comprised of a String and a BlobReferenceProperty:

class NameKeyPair(db.Model):
    human_readable_name = db.StringProperty()   
    blobkey = blobstore.BlobReferenceProperty()

When I save some text in a blob, I set the blobkey like this:

...
#Create the file
file_name = files.blobstore.create(mime_type='text/plain')

#Open the file and write to it
with files.open(file_name, 'a') as f:
    f.write(data.encode('utf8'))

#Finalize the file
files.finalize(file_name)

#Get the file's blob key
blobkey = files.blobstore.get_blob_key(file_name)

#store it in DB
nameKeyPair = NameKeyPair()
nameKeyPair.blobkey = blobkey
nameKeyPair.human_readable_name = human_readable_name
nameKeyPair.put()
...

The human_readable_name is a string which I send out the user. Now when the user sends me the name back, I want to return the text of the blob:

...
human_readable_name = self.request.get('human_readable_name') 
q = NameKeyPair.all()
q.filter("human_readable_name =", human_readable_name)

blobkey = ""
for p in q.run(limit=1):
    blobkey = p.blobkey

blob_info = blobstore.BlobInfo.get(blobkey)
self.send_blob(blob_info)

This doesn't work. Appspot just says 'server error.'

If I print out my variable blobkey it says <google.appengine.ext.blobstore.blobstore.BlobInfo object at 0x54dbe8bfa91b53a0> Shouldn't it be a blobkey?

This page: https://developers.google.com/appengine/docs/python/blobstore/blobkeyclass says "You can fetch the BlobInfo entity with a BlobKey by passing the key to the BlobInfo.get() class method."

That's why I was passing my blobkey to BlobInfo.get. But if my "blobkey" is in fact already a BlobInfo object, maybe I don't need to do that. I tried passing it directly to send_blob instead:

self.send_blob(blobkey)

But I get the same result.

And based on this page: https://developers.google.com/appengine/docs/python/blobstore/ which shows code example: self.send_blob(blob_info) it seems like that should be all I need... But it just doesn't work. Any ideas?

1
Is there a more detailed error in the log?Greg

1 Answers

0
votes

I cant comment but try use [object].key() I find it a bit obtuse when is the key an object and when is it a string