0
0
GCPcloud~30 mins

State management in GCP - Mini Project: Build & Apply

Choose your learning style9 modes available
State Management with Google Cloud Storage
📖 Scenario: You are building a simple cloud application that needs to keep track of user session states. To do this, you will use Google Cloud Storage to save and retrieve session data as JSON files.
🎯 Goal: Create a Google Cloud Storage bucket and write Python code to save a session state dictionary as a JSON file, then read it back to verify the state is managed correctly.
📋 What You'll Learn
Create a dictionary called session_state with exact keys and values
Create a variable called bucket_name with the exact bucket name string
Write code to upload session_state as JSON to the bucket using blob.upload_from_string()
Write code to download the JSON file from the bucket and load it back into a dictionary called loaded_state
💡 Why This Matters
🌍 Real World
Many cloud applications need to save user session data or application state between runs. Using cloud storage to save JSON files is a simple way to manage state without a database.
💼 Career
Cloud engineers and developers often manage state in distributed systems. Knowing how to store and retrieve state data in cloud storage is a foundational skill for building scalable cloud applications.
Progress0 / 4 steps
1
Create the session state dictionary
Create a dictionary called session_state with these exact key-value pairs: 'user_id': 'abc123', 'is_logged_in': true, 'last_page': 'home'.
GCP
Need a hint?

Use curly braces to create a dictionary and include the exact keys and values.

2
Set the Google Cloud Storage bucket name
Create a variable called bucket_name and set it to the string 'my-session-bucket'.
GCP
Need a hint?

Assign the exact string to the variable bucket_name.

3
Upload the session state as JSON to the bucket
Import storage from google.cloud. Create a storage.Client() instance called client. Get the bucket using client.get_bucket(bucket_name) and assign it to bucket. Create a blob named 'session.json' from bucket.blob(). Upload the JSON string of session_state using blob.upload_from_string() with content_type='application/json'. Use json.dumps() to convert the dictionary to a JSON string.
GCP
Need a hint?

Use the Google Cloud Storage client library to upload the JSON string to the bucket.

4
Download and load the session state JSON
Create a new blob object from bucket.blob('session.json'). Download the JSON string using blob.download_as_text() and assign it to json_data. Use json.loads(json_data) to convert it back to a dictionary and assign it to loaded_state.
GCP
Need a hint?

Download the JSON text from the blob and convert it back to a dictionary.