Region ID
The REGION_ID
is an abbreviated code that Google assigns
based on the region you select when you create your app. The code does not
correspond to a country or province, even though some region IDs may appear
similar to commonly used country and province codes. For apps created after
February 2020, REGION_ID.r
is included in
App Engine URLs. For existing apps created before this date, the
region ID is optional in the URL.
Learn more about region IDs.
Use authenticated user information to store and retrieve user-specific data and personalize each user's experience with your web service.
In a previous step, you updated the web service to display the last ten requests from all users. In this step, you use authenticated user information to update your web service so that the page displays only a list of the last ten requests made by the currently authenticated user.
Before you Begin
If you have completed all the previous steps in this guide, skip this section. Otherwise, complete one of the following:
Start from Build a Python 3 App and complete all the steps leading up to this one.
If you already have a Google Cloud project, you can continue by downloading a copy of the web service and adding Firebase:
Download the sample application repository using Git:
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
Alternatively, you can download the sample as a zip file and then extract it.
Navigate to the directory that contains a copy of the files from the previous step:
cd python-docs-samples/appengine/standard_python3/building-an-app/building-an-app-3
Store and retrieve user-specific data
You can indicate that data is connected to a certain user by using Firestore in Datastore mode (Datastore) ancestors, which allow you to organize your Datastore data hierarchically.
To do this, complete the following steps:
Update your
store_time
andfetch_time
methods to use Datastore ancestors for storing and retrievingvisit
entities:Each
visit
entity now has an ancestor that it is connected to. These ancestors are Datastore entities that represent individual authenticated users. Each ancestor's key includes theUser
kind and a custom ID, which is the authenticated user's email address. You use the ancestor key to query the database for only the times that are associated with a specific user.Update the
store_times
method call in yourroot
method and move it inside theid_token
conditional so that it only runs if the server has authenticated a user:
Configure indexes
Datastore makes queries based on indexes. For simple entities,
Datastore automatically generates these indexes. However, it cannot
automatically generate indexes for more complicated entities, including those
with ancestors.
Because of this, you need to manually create an index for visit
entities
so that Datastore can perform queries involving visit
entities.
To create an index for visit
entities, complete the following steps:
Create an
index.yaml
file in the root directory of your project, for examplebuilding-an-app
, and add the following index:Deploy your
index.yaml
indexes in Datastore by running the following command and following the prompts:gcloud datastore indexes create index.yaml
It can take a while for Datastore to create indexes. Creating indexes before deploying your web service to App Engine both allows you to test locally using those indexes and prevents exceptions that might occur for queries that require an index that is still in the process of being built.
For more information on making Datastore indexes, see Configuring Datastore Indexes.
Testing your web service
Test your web service by running it locally in a virtual environment:
Run the following command in your project's main directory to run your web service. If you have not set up a virtual environment for local testing, see testing your web service.
python main.py
Enter the following address in your web browser to view your web service:
http://localhost:8080
Deploy your web service
Now that you have Datastore working locally, you can re-deploy your web service to App Engine.
Run the following command from the root directory of your project,
where your app.yaml
file is located:
gcloud app deploy
All traffic is automatically routed to the new version you deployed.
For more information on managing versions, see Manage Services and Versions.
View your service
To quickly launch your browser and access your web service at
https://PROJECT_ID.REGION_ID.r.appspot.com
, run the following command:
gcloud app browse
Next Steps
Congratulations! You've successfully built a web service that uses Datastore data storage and Firebase authentication to provide authenticated users with a personalized web page.
Now you can clean up by shutting down, deactivating, or disabling billing for your project.