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.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- 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 Vision API, and enable it, if necessary.
- Return to the API Library page, search for Cloud Translation API, and enable it, if necessary.
- Create the required buckets.
- From the main navigation, go to Storage > Browser.
- Choose Create bucket.
- In the Name field, enter a unique name for the bucket to hold the incoming image.
- From the Default storage class options, choose Regional.
- Leave the remaining values as their defaults, and click Create.
- Repeat steps 2–5 to create a bucket to hold the resulting translations.
- Create the required Pub/Sub topics.
- From the main console navigation, go to Pub/Sub > Topics.
- Click Create a topic.
- In the dialog, enter the name la-image-to-text-results for the topic.
- Click Create.
- Repeat steps 2–4 to create another topic with the name la-image-to-text-translation.
- 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:
cd content-gc-functions-deepdive/cloud-functions-apis-lab - Open the Cloud Shell Editor by clicking the pencil icon.
- Navigate to the cloud-functions-apis-lab folder, and open the two files there.
- In the
main.py
file, on line 34, change theRESULT_BUCKET
value to match the name of the bucket intended to hold your translation results. - Save the file.
- Create first function.
- Navigate to the Cloud Functions dashboard.
- Click Create function.
- 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
- 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. - In the requirements.txt field of the function, paste the copied code.
- In the Function to execute field, enter process_image.
- Click Create.
- Create second function.
- Navigate to the Cloud Functions dashboard.
- Click Create function.
- 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
- 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. - In the requirements.txt field of the function, paste the copied code.
- In the Function to execute field, enter translate_text.
- Click Create.
- Create third function.
- Navigate to the Cloud Functions dashboard.
- Click Create function.
- 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
- 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. - In the requirements.txt field of the function, paste the copied code.
- In the Function to execute field, enter save_result.
- Click Create.
- Test in Cloud Shell and review 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: