- Lab
- A Cloud Guru
Powering Google Cloud APIs with Cloud Functions
One of the most powerful aspects of the Cloud Functions service is its flexibility while working with other Google Cloud services, including some of the most advanced APIs. Not only can your function be triggered by multiple sources, you can include multiple functions all in the app. This lab illustrates a perfect example of that flexibility. Here, you will take a photo that has been uploaded, examine it for text, extract any text that is found, translate that text into various languages, and then store those translations for easy retrieval. Sounds like a lot, doesn’t it? Well, as you'll experience firsthand in this lab, Cloud Functions can handle it all.
Path Info
Table of Contents
-
Challenge
Enable APIs and Services
- From the main Google Cloud console navigation, choose APIs & Services > Library.
- Search for Cloud Functions API, and enable the service, if necessary.
- Return to the API Library page, search for Cloud Build API, and enable it, if necessary.
- Return to the API Library page, search for Cloud Vision API, and enable it, if necessary.
- Return to the API Library page, search for Cloud Translation API, and enable it, if necessary.
-
Challenge
Create the Required Buckets
- From the main navigation, go to Storage > Buckets.
- Choose Create bucket.
- In the Name field, enter a unique name for the bucket to hold the incoming image, and click Continue.
- From the Choose where to store your data options window, choose Region and select
us-east1
as the region. - Leave the remaining values at their defaults, and click Create.
- Repeat steps 2–5 to create a bucket to hold the resulting translations.
-
Challenge
Create the Required Pub/Sub Topics
- From the main console navigation, go to Pub/Sub > Topics.
- Click Create Topic.
- In the dialog, enter the name
acg-image-to-text-results
for the topic. - Leave the options at their defaults, and click Create Topic.
- Return to the main Pub/Sub page and repeat steps 2–4 to create another topic with the name
acg-image-to-text-translation
.
-
Challenge
Retrieve Files from Repo and Configure
-
Activate the Cloud Shell.
-
From the Cloud Shell, issue the following command to clone the repository for this course:
git clone https://github.com/linuxacademy/content-gc-functions-deepdive
-
Change directory to the lab folder:
content-gc-functions-deepdive/cloud-functions-apis-lab
. -
Expand the Projects panel by clicking the triangle next to the current project name.
-
Copy the current project ID.
-
Open the Cloud Shell Editor.
-
Navigate to the
cloud-functions-apis-lab
folder, and openmain.py
. -
In the
main.py
file, on line 29, change thePROJECT_ID
value to match your project ID. -
On line 34, change the
RESULT_BUCKET
value to match the name of the bucket intended to hold your translation results. -
Save the file.
-
-
Challenge
Create the First Function
- Navigate to the Cloud Functions dashboard.
- Click Create function.
- Apply the following settings:
- Name: Enter ocr-extract
- Region: Selectus-east1
- Trigger: Select Cloud Storage
- Event Type: Select finalizing/creating
- Bucket: Your storage bucket for incoming files
- On the next page, add the following settings:
- Runtime: Python 3.9
- Source code: Inline editor
- Make sure to expand out the "Runtime, build, connections and security settings" section, and set the "Maximum number of instances" to 1.
- From the Cloud Shell Editor, select all of the
main.py
code, and copy it. - In the
main.py
field of the function, paste the copied code. - From the Cloud Shell Editor, open
requirements.txt
, and copy all contents. - In the
requirements.txt
field of the function, paste the copied code. - In the Entry Point field, enter process_image.
- Click Deploy.
-
Challenge
Create the Second Function
- From the Cloud Functions dashboard, click the newly created function.
- Click Copy
- Apply the following settings:
- Name: Select ocr-translate
- Region: Select us-east1
- Trigger type: Select Cloud Pub/Sub
- Select a Cloud Pub/Sub topic: Select the topic ending in acg-image-to-text-translation.
- Make sure to expand out the "Runtime, build, connections and security settings" section, and set the "Maximum number of instances" to 1.
- On the next page, add the following settings:
- Runtime: Python 3.9
- Source code: Inline editor
- In the Entry Point field, enter translate_text.
- Click Deploy.
-
Challenge
Create the Third Function
- From the Cloud Functions dashboard, click the newly created function.
- Click Copy
- Apply the following settings:
- Name: Select ocr-save
- Region: Select us-east1
- Trigger: Select Cloud Pub/Sub
- Select a Cloud Pub/Sub topic: Select the topic ending in acg-image-to-text-results.
- Make sure to expand out the "Runtime, build, connections and security settings" section, and set the "Maximum number of instances" to 1.
- On the next page, add the following settings:
- Runtime: Python 3.9
- Source code: Inline editor
- In the Entry Point field, enter save_result.
- Click Deploy.
-
Challenge
Test in Cloud Shell and Review the Results
- In the Cloud Shell, issue the following commands:
cd images ls gsutil cp sign.png gs://<BUCKET_NAME> gsutil cp menu.jpg gs://<BUCKET_NAME>
- From the console, navigate to Storage > Browser.
- Open your bucket that contains the translated results.
- Open any generated files.
- In the Cloud Shell, issue the following commands:
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.