Code for Uploading Static Files to Google App Engine

Published in: Code

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

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.

Published in: Code

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch