Code for Uploading Static Files to Google App Engine

Now that you have read my tutorial on how to host websites with Google App Engine, here are the two files – app.yaml and main.py - 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 main.py file handles the HTTP requests when serving the website.

1. Source code for app.yaml

application:
version: 1
runtime: python
api_version: 1

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

- url: .*
  script: main.py

2. Source code for main.py

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.

Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

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.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.