Skip to content

Latest commit

 

History

History
93 lines (67 loc) · 4.69 KB

README.md

File metadata and controls

93 lines (67 loc) · 4.69 KB

Google Cloud Functions Sample

This sample demonstrates how to use the following Google Cloud products together:

Setup

Create a Project

  1. Create a project with the Google Cloud Platform Console Cloud Resource Manager.
  2. Make note of your project ID, which may be different than your project name.
  3. Make sure to Enable Billing for your project.

Create a Firestore Database

  1. Go to the Google Cloud Platform Console
  2. Click the button in the top left and select Firestore.
    1. Click Select Native Mode.
    2. Select a region close to you geographically.
    3. Click Create Database.
    4. Wait for your database to be created.

Enable the Vision API

  1. Go to the Google Cloud Platform Console
  2. Click the button in the top left.
  3. Select APIs & services.
  4. Click Enable APIs and Services at the top.
  5. Search for Vision.
  6. Click the first result.
  7. Click the Enable button.

Download the Sample Code

Run the following command to clone the Github repository:

git clone https://github.com/GoogleCloudPlatform/hackathon-toolkit.git

Change directory to the sample code location:

cd hackathon-toolkit/functions

Deploying to Google Cloud Functions

Deploy the code to Google Cloud Functions:

  1. Go to the Google Cloud Platform Console
  2. Click the button in the top left and select Cloud Functions.
  3. Click Enable API then click Create Function.

Create a Google Cloud Function

Create a Google Cloud Function with a Cloud Storage trigger in Python:

  1. Give your new Cloud Function the name process_image.
  2. Change the Trigger from HTTP to Cloud Storage.
  3. For the Bucket field, click Browse and then click the basket icon (New bucket) to create a new Cloud Storage bucket.
  4. Name the bucket the same as your Google Cloud project.
  5. Leave all fields the same and click Create to create a Cloud Storage bucket.
  6. Click Select to select this bucket as the trigger for your Cloud Function.
  7. Change the Runtime to Python 3.7.
  8. Copy and paste the main.py code from this code sample into the main.py file in the inline editor.
  9. Copy and paste the requirements.txt code from this code sample into the requirements.txt file in the inline editor.
  10. Change Function to execute to process_image.
  11. Click the Create button at the bottom to start the deployment process. Your Cloud Function should take <2 minutes to deploy.

Testing the Cloud Function

Test the Google Cloud Function by first going to Storage to upload a file:

  1. Go to the Google Cloud Platform Console.
    1. Click the button in the top left, scroll down and select Storage
    2. Select the Cloud Storage bucket you created in the previous section.
    3. Click the Upload Files button and select an image from your computer to upload.
  2. After the image uploads, click the navigation menu in the top left of your Google Cloud Platform Console and select Firestore.
    1. Make sure you have a photos collection and a document within it named the same as your uploaded image.
    2. Within that document, make sure that there is a labelsDetected field with an array of labels detected using the Vision API.

Note: If you have any issues, visit the dashboard for your Cloud Function in the Cloud Functions section of the Google Cloud Platform Console. See if there are any errors in the Errors in the last 7 days section at the bottom. You can also click the View Logs button at the top of your Cloud Function's dashboard.

Further Reading