Skip to content

Latest commit

 

History

History
 
 

datastore

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Google Cloud Platform logo

Google Cloud Functions Cloud Datastore sample

This recipe shows you how to read and write an entity in Cloud Datastore from a Cloud Function.

View the source code.

Deploy and Test

  1. Follow the Cloud Functions quickstart guide to setup Cloud Functions for your project.

  2. Clone this repository:

     git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
     cd nodejs-docs-samples/functions/datastore
    
  3. Create a Cloud Storage Bucket to stage our deployment:

     gsutil mb gs://YOUR_BUCKET_NAME
    
    • Replace YOUR_BUCKET_NAME here and in subsequent commands with the name of your Cloud Storage Bucket.
  4. Ensure the Cloud Datastore API is enabled:

Click here to enable the Cloud Datastore API

  1. Deploy the "get" function with an HTTP trigger:

     gcloud beta functions deploy get --stage-bucket YOUR_BUCKET_NAME --trigger-http
    
  2. Deploy the "set" function with an HTTP trigger:

     gcloud beta functions deploy set --stage-bucket YOUR_BUCKET_NAME --trigger-http
    
  3. Deploy the "del" function with an HTTP trigger:

     gcloud beta functions deploy del --stage-bucket YOUR_BUCKET_NAME --trigger-http
    
  4. Call the "set" function to create a new entity:

     gcloud beta functions call set --data '{"kind":"Task","key":"sampletask1","value":{"description":"Buy milk"}}'
    

    or

     curl -H "Content-Type: application/json" -X POST -d '{"kind":"Task","key":"sampletask1","value":{"description":"Buy milk"}}' "https://[YOUR_REGION]-[YOUR_PROJECT_ID].cloudfunctions.net/set"
    
    • Replace [YOUR_REGION] with the region where your function is deployed.
    • Replace [YOUR_PROJECT_ID] with your Google Cloud Platform project ID.
  5. Call the "get" function to read the newly created entity:

     gcloud beta functions call get --data '{"kind":"Task","key":"sampletask1"}'
    

    or

     curl -H "Content-Type: application/json" -X POST -d '{"kind":"Task","key":"sampletask1"}' "https://[YOUR_REGION]-[YOUR_PROJECT_ID].cloudfunctions.net/get"
    
    • Replace [YOUR_REGION] with the region where your function is deployed.
    • Replace [YOUR_PROJECT_ID] with your Google Cloud Platform project ID.
  6. Call the "del" function to delete the entity:

     gcloud alpha functions call del --data '{"kind":"Task","key":"sampletask1"}'
    

    or

     curl -H "Content-Type: application/json" -X POST -d '{"kind":"Task","key":"sampletask1"}' "https://[YOUR_REGION]-[YOUR_PROJECT_ID].cloudfunctions.net/del"
    
    • Replace [YOUR_REGION] with the region where your function is deployed.
    • Replace [YOUR_PROJECT_ID] with your Google Cloud Platform project ID.
  7. Call the "get" function again to verify it was deleted:

     gcloud beta functions call get --data '{"kind":"Task","key":"sampletask1"}'
    

    or

     curl -H "Content-Type: application/json" -X POST -d '{"kind":"Task","key":"sampletask1"}' "https://[YOUR_REGION]-[YOUR_PROJECT_ID].cloudfunctions.net/get"
    
    • Replace [YOUR_REGION] with the region where your function is deployed.
    • Replace [YOUR_PROJECT_ID] with your Google Cloud Platform project ID.