Skip to main content

Powering Google Cloud APIs with Cloud Functions

Hands-On Lab

 

Photo of Joseph Lowery

Joseph Lowery

Google Cloud Training Architect II in Content

Length

00:30:00

Difficulty

Beginner

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, we 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 we’ll experience firsthand in this lab, Cloud Functions can handle it all.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Powering Google Cloud APIs with Cloud Functions

Introduction

In this hands-on lab, we 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.

How to Log in to Google Lab Accounts

On the lab page, right-click Open GCP Console and select the option to open it in a new private browser window (this option will read differently depending on your browser — e.g., in Chrome, it says "Open Link in Incognito Window"). Then, sign in to Google Cloud Platform using the credentials provided on the lab page.

On the Welcome to your new account screen, review the text, and click Accept. In the "Welcome L.A.!" pop-up once you're signed in, check to agree to the terms of service, choose your country of residence, and click Agree and Continue.

Solution

Enable APIs and Services

  1. From the main Google Cloud console navigation, choose APIs & Services > Library.
  2. Search for Cloud Functions API, and enable the service, if necessary.
  3. Return to the API Library page, search for Cloud Vision API, and enable it, if necessary.
  4. Return to the API Library page, search for Cloud Translation API, and enable it, if necessary.

Create the Required Buckets

  1. From the main navigation, go to Storage > Browser.
  2. Choose Create bucket.
  3. In the Name field, enter a globally unique name (e.g., “la-image-to-text-input-” with a series of numbers at the end, like “8675309").
  4. Set Default storage class to Regional.
  5. Leave the remaining values as their defaults, and click Create.
  6. Repeat steps 2–5 to create a bucket to hold the resulting translations. (The name could be something like “la-image-to-text-results-” with a series of numbers at the end.)

Create the Required Pub/Sub Topics

  1. From the main console navigation, go to Pub/Sub > Topics.
  2. Click Create a topic.
  3. In the dialog, enter the name "la-image-to-text-results" for the topic.
  4. Click Create.
  5. Repeat steps 2–4 to create another topic with the name "la-image-to-text-translation".

Retrieve Files from Repo and Configure

  1. Activate the Cloud Shell by clicking the icon in the top row.

  2. 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
  3. Change directory to the lab folder:

    cd content-gc-functions-deepdive/cloud-functions-apis-lab
  4. Open the Cloud Shell Editor by clicking the pencil icon.

  5. Navigate to the cloud-functions-apis-lab folder, and open the two files there.

  6. In the main.py file, on line 34, change the RESULT_BUCKET value to match the name of the bucket intended to hold your translation results.

  7. Save the file.

Create First Function

  1. In the console, navigate to the Cloud Functions dashboard.
  2. Click Create function.
  3. Apply the following settings:
    • Name: ocr-extract
    • Trigger: Cloud Storage
    • Bucket: Your storage bucket for incoming files
    • Source code: Inline editor
    • Runtime: Python 3.7
  4. From the Cloud Shell Editor, select all of the main.py code and copy it.
  5. In the main.py field of the function, delete the existing code and paste in the copied code.
  6. From the Cloud Shell Editor, open requirements.txt and copy all.
  7. In the requirements.txt field of the function, delete the existing code and paste in the copied code.
  8. In the Function to execute field, enter "process_image".
  9. Click Create.

Create Second Function

  1. Navigate to the Cloud Functions dashboard.
  2. Click Create function.
  3. Apply the following settings:
    • Name: ocr-translate
    • Trigger: Cloud Pub/Sub
    • Topic: la-image-to-text-translation
    • Source code: Inline editor
    • Runtime: Python 3.7
  4. From the Cloud Shell Editor, select all of the main.py code and copy it.
  5. In the main.py field of the function, delete the existing code and paste in the copied code.
  6. From the Cloud Shell Editor, open requirements.txt and copy all.
  7. In the requirements.txt field of the function, delete the existing code and paste in the copied code.
  8. In the Function to execute field, enter "translate_text".
  9. Click Create.

Create Third Function

  1. Navigate to the Cloud Functions dashboard.
  2. Click Create function.
  3. Apply the following settings:
    • Name: ocr-save
    • Trigger: Cloud Pub/Sub
    • Topic: la-image-to-text-results
    • Source code: Inline editor
    • Runtime: Python 3.7
  4. From the Cloud Shell Editor, select all of the main.py code and copy it.
  5. In the main.py field of the function, delete the existing code and paste in the copied code.
  6. From the Cloud Shell Editor, open requirements.txt and copy all.
  7. In the requirements.txt field of the function, delete the existing code and paste in the copied code.
  8. In the Function to execute field, enter "save_result".
  9. Click Create.

Test in Cloud Shell and Review Results

  1. In the Cloud Shell, change directory:

    cd images
  2. List the contents:

    ls
  3. Copy the sign.png file:

    gsutil cp sign.png gs://<BUCKET_NAME>
  4. Copy the menu.jpg file:

    gsutil cp menu.jpg gs://<BUCKET_NAME>
  5. From the console, navigate to Storage > Browser.

  6. Open your bucket that contains the translated results.

  7. Open any generated files.

Conclusion

Congratulations on completing this hands-on lab!