Arvutiteaduse instituut
  1. Kursused
  2. 2016/17 kevad
  3. Pilvetehnoloogia alused (MTAT.08.027)
EN
Logi sisse

Pilvetehnoloogia alused 2016/17 kevad

  • Main
  • Lectures
  • Practicals
  • Results
  • Submit Homework

Practice 6 - Creating Google AppEngine applications

In this practice session you will learn how to create Google App engine applications and how to use Google User and BlobStore services.

References

Referred documents and web sites contain supportive information for the practice.

Manuals

  • Python 2.7 tutorial
  • Google App Engine tutorial for Python 2.7
  • WebApp2 Tutorial

Exercise 6.1. Making a GoogleAppEngine Account

  • Log in at https://console.cloud.google.com/appengine with a Google Account
    • If you do not have a Google account, make one.
  • Create a new App Engine project
    • Assign a recognizable Project ID.
    • It should use at least your partial last name.
    • It will be part of the url or your web application
  • Access your applications Dashboard and familiarize yourself with its functionalities and your application settings

Exercise 6.2. Creating your first application

  • Download the Google App Engine python sdk from:
    • Windows: https://storage.googleapis.com/appengine-sdks/featured/GoogleAppEngine-1.9.50.msi
    • Linux: https://storage.googleapis.com/appengine-sdks/featured/google_appengine_1.9.50.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
  • 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
  • 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
  • Modify the program to print out proper HTML:
    • self.response.out.write('<html><body>')
    • ....
    • self.response.out.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 AppEngine platform

  • 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 AppEngine
    • 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
    • Online Guide for appcfg.py
  • Access your application online by going to the address <app name>.appspot.com:
    • For example http://sharp-sandbox-516.appspot.com/

Exercise 6.4. Accessing Google User services from your application

Lets now add User Management to the application

  • 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
  • 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
  • Redeploy it locally and verify the changes are working.

Exercise 6.5. Accessing Google Blob service to upload files

  • 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.out.write('<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
    • self.response.out.write("""Upload File: <input type="file" name="myUploadedFile"><br />""")
    • self.response.out.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.out.write('<html><body>')
    • self.response.out.write('Uploaded file: ' + str(blob_info.filename))
    • self.response.out.write('</body></html>')
  • Finally, add a line: ('/upload', Upload), in the webapp2.WSGIApplication statement to activate a new /upload page which serves the Upload function.

Exercise 6.6. Accessing uploaded files in the Google Blob store

  • 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 the webapp2.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
  • 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

Deliverables

  • Deploy the latest version of the application in Google App Engine
    • Provide its web address as deliverable
  • Upload your Python code
6. Exercise 6
Sellele ülesandele ei saa enam lahendusi esitada.
  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Tartu Ülikooli arvutiteaduse instituudi kursuste läbiviimist toetavad järgmised programmid:
euroopa sotsiaalfondi logo