
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Create a bucket
/ 50
Upload objects to your bucket
/ 50
Cloud Storage allows world-wide storage and retrieval of any amount of data at any time. You can use Cloud Storage for a range of scenarios including serving website content, storing data for archival and disaster recovery, or distributing large data objects to users via direct download.
In this lab you'll configure an application to use Cloud Storage to store and retrieve application data. The application is an online Quiz, the data is the form data, including an image you upload from your local machine.
In this lab, you learn how to perform the following tasks:
Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources are made available to you.
This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.
To complete this lab, you need:
Click the Start Lab button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).
The lab spins up resources, and then opens another tab that shows the Sign in page.
Tip: Arrange the tabs in separate windows, side-by-side.
If necessary, copy the Username below and paste it into the Sign in dialog.
You can also find the Username in the Lab Details pane.
Click Next.
Copy the Password below and paste it into the Welcome dialog.
You can also find the Password in the Lab Details pane.
Click Next.
Click through the subsequent pages:
After a few moments, the Google Cloud console opens in this tab.
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
Click Activate Cloud Shell at the top of the Google Cloud console.
Click through the following windows:
When you are connected, you are already authenticated, and the project is set to your Project_ID,
gcloud
is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
Output:
Output:
gcloud
, in Google Cloud, refer to the gcloud CLI overview guide.
YOUR-PROJECT-ID
with your Project ID:In this section, you access Cloud Shell, clone the git repository containing the Quiz application, and run the application.
Change the working directory:
Set the region using a variable:
Configure the application:
This script file:
GCLOUD_PROJECT
.pip install -r requirements.txt
.Run the application:
The application is running when you see the following output:
To view the application, click Web preview > Preview on port 8080.
Click the Create Question link in the toolbar.
You should see a simple form that contains text boxes for the question and answers and radio buttons to select the correct answer
In this section, you review the case study application code.
In this lab you'll view and edit files. You can use the shell editors that are installed on Cloud Shell, such as nano
or vim
or the Cloud Shell code editor.
This lab uses the Cloud Shell code editor to review the Quiz application code.
Navigate to the /training-data-analyst/courses/developingapps/python/cloudstorage/start
folder using the file browser panel on the left side of the editor.
Select the add.html
file in the ...quiz/webapp/templates/
folder.
This file contains the template for the Create Question form.
Notice how the form has been modified to use multipart/form-data
as the enc-type
, and there are two new form controls:
image
imageUrl
Select the routes.py
file in the ...quiz/webapp
folder.
This file contains the route for the POST handler that receives the form data. It has been modified to get the image file from the form.
Select the questions.py
file in the ...quiz/webapp
folder.
This file contains the handler that processes the form data extracted in the routes.py
file. You will modify this file to use a new module that is a client for Cloud Storage.
Select the ...quiz/gcp/storage.py
file.
This is the file where you will write code to save image file data into Cloud Storage.
In this section, you create a Cloud Storage bucket and export an environment variable that references it.
Return to the Cloud Shell command line.
Stop the application by pressing Ctrl+C.
Create a Cloud Storage bucket named <Project ID>-media
:
You can create a bucket using the gsutil mb command, passing through the name of the bucket as gs://BUCKET_NAME
You can use $DEVSHELL_PROJECT_ID as the bucket name prefix followed by -media
To export the Cloud Storage bucket name as an environment variable named GCLOUD_BUCKET
, execute the following command:
Click Check my progress to verify the objective.
In this section, you write code to save uploaded files into Cloud Storage.
For information on API documentation for Cloud Storage, refer to the APIs and reference reference.
...quiz/gcp/storage.py
file.GCLOUD_BUCKET
environment variable.google.client
package.quiz/gcp/storage.py
storage.py
, in the upload_file(...)
function, remove the existing pass statement, then use the Cloud Storage client to upload a file to your Cloud Storage bucket and make it publicly available.quiz/gcp/storage.py - upload)file(...)
function
storage.py
....quiz/webapp/questions.py
file.upload_file(...)
function. Use your storage client to upload a file, and assign the returned public URL to a variable.save_question(...)
function. Write an if test to see if the image_file
is present.upload_file(...)
function, and assign the public URL to a entity property named imageUrl.quiz/webapp/questions.py
questions.py
....gcp/storage.py
and ...webapp/questions.py
files, and then return to the Cloud Shell command.Download an image file to your local machine from Google storage.
In Cloud Shell, click Web preview > Preview on port 8080 to preview the Quiz application.
Click the Create Question link.
Complete the form with the following values, and then click Save.
Form Field | Value |
---|---|
Author | Your name |
Quiz | Google Cloud Platform |
Title | Which product does this logo relate to? |
Image | Upload the Google_Cloud_Storage_logo.png file you previously downloaded |
Answer 1 | App Engine |
Answer 2 |
Cloud Storage (Select the Answer 2 radio button) |
Answer 3 | Compute Engine |
Answer 4 | Container Engine |
Return to the Cloud Console and navigate to Navigation menu > Cloud Storage.
On the Cloud Storage > Buckets page, click the correct bucket (named <Project ID>-media
).
You should see your new object named Google_Cloud_Storage_logo.png
.
Add /api/quizzes/gcp
to the end of the application's URL.
You should see that JSON data has been returned to the client corresponding to the Question you added in the web application.
The imageUrl property should have a value corresponding to the object in Cloud Storage.
Return to the application home page and click the Take Test link.
Click GCP, and answer each question.
When you get to the question you just added, you should see the image has been formatted inside the client-side web application!
This concluded the lab, App Dev: Storing Image and Video Files in Cloud Storage - Python. You used Cloud Storage to store and retrieve application data.
See what else you can do with images and video:
Learn more about Python on the Google Cloud.
Manual Last Updated January 24, 2025
Lab Last Tested January 24, 2025
Copyright 2025 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
Ta treść jest obecnie niedostępna
Kiedy dostępność się zmieni, wyślemy Ci e-maila z powiadomieniem
Świetnie
Kiedy dostępność się zmieni, skontaktujemy się z Tobą e-mailem
One lab at a time
Confirm to end all existing labs and start this one