+++ It is working – See solution at bottom +++
I am a relative newbie to Google App Engine and Python.
I have some large text content (content in a variable and not an external file) that I am manipulating in my program. GAE does not let me write to files so I would like to store them in blobstore. Can I do this in GAE and Python? and if so, how?
An example code snippet would be greatly appreciated.
Thanks.
+++ Updated Question +++
I tried following the example at the link you provided modified by the example at: http://blog.notdot.net/2010/03/Implementing-a-dropbox-service-with-the-Blobstore-API-Part-1 to enable the saving of the blobstore key in the datastore. When the time comes to retrieve the file (which is an html file), I want to retrieve the BlobKey using the TemplateName.
I ended up with something like this:
In models.py I have:
class GeneratedFiles(ndb.Model):
TemplateName = ndb.StringProperty()
BlobKey = blobstore.BlobReferenceProperty()
Status = ndb.StringProperty(default="Pending Translation")
In a class, in a .py file I have:
class TokenFileGen(BaseHandler):
def get(self):
template = jinja_environment.get_template(FileName)
blobtext = template.render(tokenvals = tokendict)
bloboutput = (blobtext.encode('utf-8'))
# Create the file
file_name = files.blobstore.create(mime_type='application/octet-stream')
# Open the file and write to it
with files.open(file_name, 'a') as fl:
fl.write(bloboutput)
# Finalize the file. Do this before attempting to read it.
files.finalize(file_name)
# Get the file's blob key
blob_key = files.blobstore.get_blob_key(file_name)
logging.info('QQQ: blob_key: %s' % blob_key)
f = GeneratedFiles(
TemplateName = templateName
, BlobKey = blob_key
, Status = 'Published'
)
f.put()
...
I get a TypeError(‘Cannot set non-property %s’ % name) TypeError: Cannot set non-property blob
My logging statement returned the following:
INFO 2012-09-21 05:20:24,177 token.py:551] QQQ: blob_key: vL117vQ4dlIPoUwXbREmbeqUnZU7nJ6ELMma8u1bFHGUfgEfOfS7HfAdFUvXc1EC
I thought I was following the example fairly closely. Any idea how I can get this to work?
Thanks for any assistance.
+++ update 2 +++
Ok, I am now able to save the Blobstore reference in the following model:
class GeneratedFiles(ndb.Model):
TemplateName = ndb.StringProperty()
BlobKey = ndb.BlobKeyProperty()
The code that saved this is:
file_name = files.blobstore.create(mime_type='application/octet-stream')
with files.open(file_name, 'a') as fl:
fl.write(bloboutput)
files.finalize(file_name)
blob_key = files.blobstore.get_blob_key(file_name)
logging.info('QQQ: blob_key: %s' % blob_key)
f = GeneratedFiles(
TemplateName = templateName
, BlobKey = blob_key
)
f.put()
I am now trying to retrieve and download the file:
class FileDownloadHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, genfile_id):
iden = int(genfile_id)
file_info = ndb.Key('GeneratedFiles', iden).get()
if not file_info or not file_info.BlobKey:
self.error(404)
return
else:
blob_key = file_info.BlobKey
logging.info('QQQ: FileDownloadHandler/blob_key: %s' % blob_key)
self.send_blob(blob_key, save_as=True)
I am getting a error: ValueError: Expected BlobInfo value for blob_key_or_info. on the last line.
Just prior to the error msg, I get my log info:
INFO 2012-09-21 19:19:44,219 genfile.py:131] QQQ: FileDownloadHandler/blob_key: sGxZRNu94u1kZ9ezpAeQFhyOLSZFYNX8RSAbXU78MLjjUKOohV0wyWnZZEQf6ScC
I found some references that mention URLencoding in conjunction with this error. Could this be the problem? If so, what would URLencoding look like in my case and where should I place it (when I store the blob_key or after I retrieve id from ndb.datastore?
Thanks for any assistance.
+++ the solution +++
The key was changing the self.send_blob statement as follows (referencing blobstore and BlobInfo).
self.send_blob(blobstore.BlobInfo(file_info.blob), save_as=True)
I also changed the name of the attribute BlobKey to blob (was getting an error and error went away after I did this although I do not understand why that would make a difference).
Look at writing to the blobstore: https://developers.google.com/appengine/docs/python/blobstore/overview#Writing_Files_to_the_Blobstore
You can give votes to comments as well.