Storing Data in the Cloud

  • Cloud applications can't use files.
    • Files live on computers
    • Entities live in the cloud
  • Cloud applications require the use of databases.
  • There are many options for using databases in the cloud
  • We'll use Google's Cloud Datastore
    • Datastore is a No-SQL database
    • SQL type databases work similarly in Python.

We're making an app for App Engine Standard Environment: It must be Python 2 :-(

You must first upgrade pip so that we have the latest:

sudo pip install --upgrade pip 

You will need to add flask as a vendor library:

mkdir lib 
pip install -t lib flask 

When you vendor in flask you'll need to tell appengine. Complete instructions can be found here: https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27

The short version is add this to appengine_config.py

# appengine_config.py
from google.appengine.ext import vendor
 
# Add any libraries install in the "lib" folder.
vendor.add('lib')

You need an app.yaml:

runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /static 
  static_dir: static 
- url: /.*
  script: main.app
  
libraries:
- name: flask
  version: 0.12

Run your App

dev_appserver.py --host=0.0.0.0 --port=8080 ./app.yaml

Entities in the Datastore are organized into classes:

class Author(ndb.Model):
    """Sub model for representing an author."""
    identity = ndb.StringProperty(indexed=False)
    email = ndb.StringProperty(indexed=False)

Reference Code

You can download this week's code sample here: week14.tar.gz