I’ve been working on a simple App Engine application that offers upload and download functionality to and from Google Cloud Storage. When it came time to actually download the content I needed to write a webapp2 RequestHandler that will retrieve the file from Cloud Storage and return it to the client.

The trick to this is to set the proper content type in your response header. In the example below I used the Cloud Storage Client Library to open and read the file, then set the response appropriately.

import webapp2
import cloudstorage

class FileDownloadHandler(webapp2.RequestHandler):

  def get(self, filename):
    self.response.headers['Content-Type'] = 'application/x-gzip'
    self.response.headers['Content-Disposition'] = 'attachment; filename=%s' % filename

    filename = '/bucket/' + filename
    gcs_file = cloudstorage.open(filename)
    data = gcs_file.read()
    gcs_file.close()

    self.response.write(data)