Uploading images to Google App Engine’s Blobstore with bottle.py

For the past couple days, I’ve been working on a web app in bottle.py, a micro web framework done in Python, with plans to host it on Google App Engine (GAE), Google’s web-app hosting service. Uploading an image to GAE’s Blobstore turned out to be a little more difficult than I anticipated. This tutorial has sample code on how to upload images using GAE’s API, but it resulted in broken image links when I did it in bottle. After Googling and trying different things for a couple hours, including logging output of the form’s request object, I finally stumbled upon this stack overflow post, which pointed me to the magic code:

foo = Foo()
foo.image = db.Blob(request.POST.file.read())
foo.put()

The sample code from GAE’s docs didn’t require the “.file.read()” part. I haven’t looked too deeply; maybe it has to do with bottle.py and GAE returning the form’s request differently.

Note that this is only for uploading of images. To dynamically serve images, you still need to create a script with a class that inherits webapp.RequestHandler and have the script run run_wsgi_app like Google’s sample code does. Then, set your app.yaml to point to that script for URLs that start with /img*