Posted by Wesley Chun (@wescpy), Developer Advocate, Google Cloud
The most recent Serverless Migration Station video demonstrated how to add use of the App Engine's Blobstore service to a sample Python 2 App Engine app, kicking off the first of a 2-part series on migrating away from Blobstore. In today's Module 16 video, we complete this journey, arriving at Cloud Storage. Moving away from proprietary App Engine services like Blobstore makes apps more portable, giving them enough flexibility to:
- Update to more recent language releases, say from Python 2 to 3 or Java 8 to 17, and run on App Engine's 2nd generation service
- Shift across to other serverless platforms, like Cloud Functions or Cloud Run (with or without Docker), or
- Move to VM-based services like GKE or Compute Engine, or to other compute platforms
As described previously, a
Blobstore for Python 2 dependency on webapp
made the Module 15 content more straightforward to implement if it was still
using webapp2
. To completely modernize this app here in Module 16, the following
migrations should be carried out:
- Migrate from
webapp2
(andwebapp
) to Flask- Migrate from App Engine NDB to Cloud NDB
- Migrate from App Engine Blobstore to Cloud Storage
- Migrate from Python 2 to Python (2 and) 3
Prior to modifying the application code, a variety of configuration updates need to be made. Updates applying only to Python 2 feature a "Py2" designation while those migrating to Python 3 will see "Py3" annotations.
- Remove the built-in Jinja2 library from
app.yaml
—Jinja2 already comes with Flask, so remove use of the older built-in version which may possibly conflict with the contemporary Flask version you're using. (Py2)- Use of Cloud client libraries (such as those for Cloud NDB and Cloud Storage) require a pair of built-in libraries,
grpcio
andsetuptools
, so add those toapp.yaml
(Py2)- Remove everything in
app.yaml
except for a valid runtime (Py3)- Add Cloud NDB and Cloud Storage client libraries to
requirements.txt
(Py2 & Py3)- Create an
appengine_config.py
supporting both built-in (those inapp.yaml
) and non built-in (those inrequirements.txt
) libraries used (Py2)
The Module 15 app already migrated away from
webapp2
's (Django) templating system to Jinja2. This is useful when
migrating to Flask because Jinja2 is Flask's default template system. Switching from App
Engine NDB to Cloud NDB is fairly straightforward as the latter was designed to be mostly
compatible with the original. The only change visible in this sample app is to move Datastore
calls into Python with
blocks.
The most significant changes occur when moving the upload and download handlers from webapp to Cloud Storage. The video and corresponding codelab go more in-depth into the necessary changes, but in summary, these are the updates required in the main application:
webapp2
is replaced by Flask. Instead of using the
older built-in version of Jinja2, use the version that comes with Flask.
webapp
Blobstore handler functionality is replaced by a
combination of the io
standard library module plus components from Flask
and Werkzeug. Furthermore, the handler classes and methods are replaced by Flask functions.
| ||
The sample app's most recent visits page. |
The only difference is that four migrations have been completed where all of the "infrastructure" is now taken care of by non-App Engine legacy services. Furthermore, the Module 16 app could be either a Python 2 or 3 app. As far as the end-user is concerned, "nothing happened."
Migrating sample app from App
Engine Blobstore to Cloud Storage |