Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Labs

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.

Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 1h 0m
Published
Clock icon Oct 04, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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 Build API, and enable it, if necessary.
    4. Return to the API Library page, search for Cloud Vision API, and enable it, if necessary.
    5. Return to the API Library page, search for Cloud Translation API, and enable it, if necessary.
  2. Challenge

    Create the Required Buckets

    1. From the main navigation, go to Storage > Buckets.
    2. Choose Create bucket.
    3. In the Name field, enter a unique name for the bucket to hold the incoming image, and click Continue.
    4. From the Choose where to store your data options window, choose Region and select us-east1 as the region.
    5. Leave the remaining values at their defaults, and click Create.
    6. Repeat steps 2–5 to create a bucket to hold the resulting translations.
  3. Challenge

    Create the Required Pub/Sub Topics

    1. From the main console navigation, go to Pub/Sub > Topics.
    2. Click Create Topic.
    3. In the dialog, enter the name acg-image-to-text-results for the topic.
    4. Leave the options at their defaults, and click Create Topic.
    5. Return to the main Pub/Sub page and repeat steps 2–4 to create another topic with the name acg-image-to-text-translation.
  4. Challenge

    Retrieve Files from Repo and Configure

    1. Activate the Cloud Shell.

    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: content-gc-functions-deepdive/cloud-functions-apis-lab.

    4. Expand the Projects panel by clicking the triangle next to the current project name.

    5. Copy the current project ID.

    6. Open the Cloud Shell Editor.

    7. Navigate to the cloud-functions-apis-lab folder, and open main.py.

    8. In the main.py file, on line 29, change the PROJECT_ID value to match your project ID.

    9. On line 34, change the RESULT_BUCKET value to match the name of the bucket intended to hold your translation results.

    10. Save the file.

  5. Challenge

    Create the First Function

    1. Navigate to the Cloud Functions dashboard.
    2. Click Create function.
    3. 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
    4. On the next page, add the following settings:
      • Runtime: Python 3.10
      • Source code: Inline editor
    5. Make sure to expand out the "Runtime, build, connections and security settings" section, and set the "Maximum number of instances" to 1.
    6. From the Cloud Shell Editor, select all of the main.py code, and copy it.
    7. In the main.py field of the function, paste the copied code.
    8. From the Cloud Shell Editor, open requirements.txt, and copy all contents.
    9. In the requirements.txt field of the function, paste the copied code.
    10. In the Entry Point field, enter process_image.
    11. Click Deploy.
  6. Challenge

    Create the Second Function

    1. From the Cloud Functions dashboard, click the newly created function.
    2. Click Copy
    3. 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.
    4. Make sure to expand out the "Runtime, build, connections and security settings" section, and set the "Maximum number of instances" to 1.
    5. On the next page, add the following settings:
      • Runtime: Python 3.10
      • Source code: Inline editor
    6. In the Entry Point field, enter translate_text.
    7. Click Deploy.
  7. Challenge

    Create the Third Function

    1. From the Cloud Functions dashboard, click the newly created function.
    2. Click Copy
    3. 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.
    4. Make sure to expand out the "Runtime, build, connections and security settings" section, and set the "Maximum number of instances" to 1.
    5. On the next page, add the following settings:
      • Runtime: Python 3.10
      • Source code: Inline editor
    6. In the Entry Point field, enter save_result.
    7. Click Deploy.
  8. Challenge

    Test in Cloud Shell and Review the Results

    1. 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>
      
    2. From the console, navigate to Storage > Browser.
    3. Open your bucket that contains the translated results.
    4. Open any generated files.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

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.

Start learning by doing today

View Plans