Practice 6 - Creating Google App Engine applications
In this practice session you will learn how to create and deploy applications for the Google App Engine PaaS and how to use Google User service for user Authentication and BlobStore services for uploading files.
References
Referred documents and web sites contain supportive information for the practice.
Manuals
Exercise 6.1. Make a new Google App Engine Project
Create a new Google App engine project, where you will later upload your application.
- Log in at https://console.cloud.google.com/appengine with a Google Account
- If you do not have a Google account, register a new account.
- Create a new App Engine project
- Assign a recognizable Project name/ID.
- It should contain your last name.
- It will be part of the url or your web application
- Assign a recognizable Project name/ID.
- Access your applications Dashboard and familiarize yourself with its functionalities and your application settings
Exercise 6.2. Creating your first application
Create a new Python demo Google App Engine application and modify it.
- Download the Google App Engine python sdk from:
- Windows: https://storage.googleapis.com/appengine-sdks/featured/GoogleAppEngine-1.9.67.msi
- Linux: https://storage.googleapis.com/appengine-sdks/featured/google_appengine_1.9.67.zip
- You may also have to install python 2.7 if you run into problems: https://www.python.org/download/releases/2.7/
- If you still encounter problems, install a 32 bit version of Python 2.7
- If you're using Windows
- Install the SDK by executing the
GoogleAppEngine-x.y.zz.msi file
- Open Google App Launcher and create a new application.
- Press the start button to start it on localserver
- Install the SDK by executing the
- If you're using Linux,
- Download the
google_appengine_x.y.zz.zip
file. - Unpack the file
- We will use the contents of the
demos/python
folder inside the unpacked folder as a basis for our Google App.- Copy these file to another folder in your computer
- Deploy the Application in localhost
- You have to use dev_appserver.py python script to launch the application locally. Details how to do it is in the following guide: Online Guide for dev_appserver.py
- Download the
- Go to http://localhost:8080 in a web browser to see your application running. (The actual port might be different from 8080. In such case, check the post value in Application Launcher)
- Browse your computer to find the python source file for your application.
- From the Google App Engine Launcher, you can choose
Edit -> Open in Explorer
- From the Google App Engine Launcher, you can choose
- Modify the program to print out proper HTML:
self.response.write('<html><body>')
....
self.response.write('</body></html>')
- Modify the printed HTML to personalize the look of your application
- Redeploy it locally and verify the changes are working.
Exercise 6.3. Deploying the Application in the live Google App Engine platform
Upload the modified application into your Google App Engine project to see how it works in a live system.
- Change the Application ID in app.yaml to match the name you chose in Exercise 6.1
- Currently it is engineapp in the .yaml file
- Activate your project in Google App Engine
- Go to Google App Engine Web Console: https://console.cloud.google.com/appengine
- Browse to your newly created project and click the "Cloud Shell" icon top right part of the window.
- Run the following command in the command line shell that appears to activate your new project:
gcloud app create
- Use appcfg.py or Deploy in Google App Engine Launcher to upload the application to Google Cloud
- Access your application online by going to the address <app name>.appspot.com:
- For example http://sharp-sandbox-516.appspot.com/
Exercise 6.4. Using Google User service for Authentication
Add User authentication to the application using Google User service and require user to log in using their Google account.
- First we need to import the Google Users API
from google.appengine.api import users
- Modify the
get(self)
method in MainHandler class- Add line
user = users.get_current_user()
to get access to Google User objects
- Add line
- Now modify the function to first check if a user object exists:
if user:
- If the object exists then continue normally and show the page content as before
- Else, if user does not exist, use a redirection statement to redirect to user login page:
self.redirect(users.create_login_url(self.request.uri))
- Add a greeting for the logged in user on your page.
- You can access user data through the user object, for example
user.nickname()
for getting their username - Full info about the user object is available at: https://cloud.google.com/appengine/docs/python/users/userobjects
- You can access user data through the user object, for example
- Redeploy it locally and verify the changes are working.
Exercise 6.5. Using Google Blobstore service for uploading files
Add a HTML form for uploading files and use the Google Blobstore service to save the uploaded file in the Google cloud.
- First add the following import statements
- import os
- import urllib
- from google.appengine.ext.webapp import blobstore_handlers
- from google.appengine.ext import blobstore
- Modify the content of the get method in MainHandler class to generate a BlobStore upload url:
upload_url = blobstore.create_upload_url('/upload')
- Modify the front page of your application to display a file upload form:
self.response.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
self.response.write("""Upload File: <input type="file" name="myUploadedFile"><br />""")
self.response.write("""<input type="submit" name="submit" value="Submit"> </form>""")
- Create a new class Upload for handling uploads:
class Upload(blobstore_handlers.BlobstoreUploadHandler):
def post(self):
upload_files = self.get_uploads('myUploadedFile')
blob_info = upload_files[0]
- In the Upload Class post function write out a confirmation that a file was uploaded:
self.response.write('<html><body>')
self.response.write('Uploaded file: ' + str(blob_info.filename))
self.response.write('</body></html>')
- Finally, add a line:
('/upload', Upload),
in thewebapp2.WSGIApplication
statement to activate a new /upload page which serves the Upload function.
Exercise 6.6. Downloading files from the Google Blobstore service
You also need to make it possible for users to download the files that have been uploaded. Update your application to add Blobstore download capability and generate links for downloads.
- Create a new class that serves files from Blob store:
class ServeFile(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, resource):
resource = str(urllib.unquote(resource))
blob_info = blobstore.BlobInfo.get(resource)
self.send_blob(blob_info)
- Add a line:
('/serve/([^/]+)?', ServeFile),
in thewebapp2.WSGIApplication
statement to activate a new /serve page (with a subfolder extension) which serves the uploaded files and also remembers the rest of the url string. - Now we need to display links to the uploaded files.
- You need two pieces of information about the uploaded files (file name and file identification (Blob key))
- All the information of the uploaded file is accessible from the blob_info object:
- <File name> - blob_info.filename
- <Blob key ID> - blob_info.key()
- Blob key`s are used to identify all the files that are uploaded to google blob store
- serve page needs to get blob key as an argument like this: /serve/<key>
- Modify the Upload function to print out a link to the just uploaded file
- The link needs to include the name of the file and link to the
"/serve/<blob_key>
page:<a href="/serve/blob_key">file_name</a>
- replace blob_key and file_name with the actual values for the uploaded file
- The link needs to include the name of the file and link to the
- Modify the MainHandler class to print out a link of all the uploaded files
- You can create a loop over all the blob files like this:
for blob_info in blobstore.BlobInfo.all():
- And inside the loop print out a link for the blob object
- You can create a loop over all the blob files like this:
Deliverables
- Deploy the latest version of the application in Google App Engine
- Provide its web address as deliverable
- Upload your Python code