2

I am looking for a tutorial or document on how to access datastore using cloud functions (python).

However, it seems there is only tutorial for nodejs. https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/functions/datastore Can anybody help me out? Thanks

John
  • 3,888
  • 11
  • 46
  • 84
  • Use the general documentation on accessing Cloud Datastore from Python. I recommend you use the Cloud Client Libraries as documented here (https://cloud.google.com/datastore/docs/reference/libraries#client-libraries-install-python). – DazWilkin Aug 30 '18 at 01:48
  • Possible duplicate of [Write to Google Cloud Storage from Cloud Function (python)](https://stackoverflow.com/questions/52249978/write-to-google-cloud-storage-from-cloud-function-python) – Elijas Dapšauskas Dec 07 '18 at 15:00

1 Answers1

2

There are no special setup needed to access datastore from cloud functions in python.

You just need to add google-cloud-datastore into requirements.txt and use datastore client as usual.

requirements.txt

# Function dependencies, for example:
# package>=version
google-cloud-datastore==1.8.0

main.py

from google.cloud import datastore
datastore_client = datastore.Client()

def foo(request):
    """Responds to any HTTP request.
    Args:
        request (flask.Request): HTTP request object.
    Returns:
        The response text or any set of values...
    """
    query = datastore_client.query(kind=<KindName>)
    data = query.fetch()

    for e in data:
        print(e)

Read more:

manRo
  • 1,455
  • 1
  • 14
  • 19