I serve static KML from the blobstore as a file that I saved to the blobstore
class KMLHandler2(blobstore_handlers.BlobstoreDownloadHandler):
def get(self):
resource = 'AMIfv965WtxAc_rWOVjSSx423_oe6f-g5obWYNKX5scg-1gqvISyaZCnv6lRaqro2wOVNOogttyMOylFLsRYZ3Y9UYIe-A69vAt4pdJB2-SHUcdVEM2v0XVLxzT3fTlxwXQVhzmsHPwALH_rCSFIvmYcuV37asVD0Q'
resource = str(urllib.unquote(resource))
blob_info = blobstore.BlobInfo.get(resource)
self.send_blob(blob_info)
This works but I want to update the file regularly. The file is generated from /list.kml so I could read it directly from there but then it times out so my plan is to make my first appenine task that reads the file from list.kml and writes it to the blobstore with that same key but how do I do that when the only examples are how to create a new file? I have the places in the code where I update the data layer and it’ not very often. Can you suggest how I should do when I update the file? I suppose I want the key and id to be the same and replace the old blob instead of writing a new and refreshing the key. Can you help me? The app engine docs say how to write a blobstore file but only a new one, I don’t know how to edit or replace a file given it’s key and I think that is what I need to do as a task or a cron job so I ask for your help.
I could make a handler like in the docs but that is for creating a file, not modifying / replacing one:
class CreateKMLHandler(webapp2.RequestHandler):
def get(self):
# 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 f:
f.write('data')
# 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)
Thanks
Update
I try this code to make a new file but it get a deadline error applicationerror 5 which I suppose is a timeout issue. How can I make it a task instead?
class CreateKMLHandler(webapp2.RequestHandler):
def get(self):
# Create the file
file_name = files.blobstore.create(mime_type='application/octet-stream')
url = 'http://montaoproject.appspot.com/list.kml'
result = urlfetch.fetch(url)
if not result.content:
return
# Open the file and write to it
with files.open(file_name, 'a') as f:
f.write(result.content)
# 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)
self.out.write(blob_key)
Once blobs have been written, they can not be changed (only read or deleted). You could use datastore entities to keep track of the current blob key associated with a given KML ‘document’.
You might also be interested in the Cloud Storage API ( http://code.google.com/appengine/docs/python/googlestorage/overview.html ), which allows you to overwrite ‘bucket’ objects by creating new objects with the same name.