Robert Mosolgo

Handling Files in WebApp2 and Google App Engine

On Google App Engine, I had to display the user-submitted image, if there was one, else display a default image. The given object could only have on image, so I was using the NDB BlobProperty:

1
2
class Sensor(ndb.Model):
  image = ndb.BlobProperty()

I put the default image in my application root (alongside app.yaml):

1
2
3
4
5
6
app_root:
  request_handlers:
    - __init__.py
    - handlers.py
  - app.yaml
  - default_sensor.jpg

In my request handler, I checked for the presence of an image, and gave the default image if there wasn’t one there:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# responds to "/sensors/(\w+)/image"
class SensorImage(webapp2.RequestHandler):
  DEFAULT_IMAGE = 'default_sensor.jpg'
  def get(self, hex):
    r = self.response
    r.headers['Content-Type'] = "image/jpg"
    this_sensor = Sensor.find_by_hex(hex)

    if (not this_sensor) or (not this_sensor.image):

      image = open(SensorImage.DEFAULT_IMAGE, 'rb')
      r.body_file.write( image.read() )
      image.close()

    else:
      r.body_file.write( this_sensor.image )

That way, I had one URL that didn’t change whether a user uploaded an image or not.