0
votes

I am trying to set up a simple cloud function and connect to my Google Cloud Platform datastore. It all works when I run it in Google colab but when I try running it as a cloud function it fails. When I strip it down to see where it fails it seems to fail already when I try to use:

from google.cloud import datastore

The entire code looks like this:

  def gdl_find_noTranslate(request):
  
  os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="cred.json"

  request_json = json.loads(request)

  if request_json and 'bookId' in request_json:
    book_id = request_json['bookId']

  # Instantiates a client - have removed the real names
  client = datastore.Client(project='my-project', namespace='my-namespace')
  
  if request_json and 'kind' in request_json:
    entity_kind = request_json['kind']
  
  # Find book kind for the new entity  
  key = client.key(entity_kind, book_id)
  query = client.query(kind=entity_kind)

  # if bookid is in parameters - add filter
  if request_json and 'bookId' in request_json:
    query.add_filter('bookID', '=', book_id)
  
  results = list(query.fetch())

  return results
1
Can you include your requirements.txt file? - Dustin Ingram
Please share the error and requirements.txt - Vikram Shinde

1 Answers

1
votes

Assuming you've an existing project (${PROJECT}) in which you've created a Datastore (this can only be done through Cloud Console).

You should not os.environ["GOOGLE_APPLICATION_CREDENTIALS"]. When you run a Cloud Function, it will run as a Service Account. You may create a specific Service Account for your function or (just) use the default account. The default Service Account is a project Editor and has sufficient permissions for Datastore.

You'll want a requirements.txt that references flask and datastore:

flask==1.1.2
google-cloud-datastore==1.13.2

Here's my trivial mash-up using Datastore, main.py:

from flask import escape
from google.cloud import datastore

client = datastore.Client()

def add_dog(request):
    key = client.key("Dog", "Freddie")
    entity = datastore.Entity(key=key)
    entity.update({
        "Breed": "Border Collie",
        "Age": 2,
        "Loves": "Swimming",
    })
    client.put(entity)

    result = client.get(key)
    return result

When you deploy your function, these imports will be pulled for you:

gcloud functions deploy addDog \
--entry-point=add_dog \
--runtime=python37 \
--trigger-http \
--allow-unauthenticated \
--project=${PROJECT}

Then, when you invoke the function, it should create|update the entity.