Code for Uploading Static Files to Google App Engine

Written by Amit Agarwal on Mar 4, 2011

Now that you have read my tutorial on how to host websites with Google App Engine, here are the two files – app.yaml and – that are responsible for all the magic. Thank you Pratham for writing this.

With app.yaml, we share a list of files with Google App Engine SDK that should be uploaded only while file handles the HTTP requests when serving the website.

1. Source code for app.yaml

application: <app-name>
version: 1
runtime: python
api_version: 1

- url: /(.*\.(gif|png|jpg|ico|js|css))
  static_files: \1
  upload: (.*\.(gif|png|jpg|ico|js|css))

- url: .*

2. Source code for

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
  def get (self, q):
    if q is None:
      q = 'index.html'

    path = os.path.join (os.path.dirname (__file__), q)
    self.response.headers ['Content-Type'] = 'text/html'
    self.response.out.write (template.render (path, {}))

def main ():
  application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True)
  util.run_wsgi_app (application)

if __name__ == '__main__':
  main ()

In our example, we are putting all the static files like images, CSS, html, etc. in the root folder but you may also organize them as sub-directories and the app.yaml file should be updated according.

Subscribe to our Email Newsletter