cloud functions python to access Datastore

陌路散爱 提交于 2020-08-08 06:42:24

问题


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


回答1:


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:

  • Python Client for Google Cloud Datastore
  • Setting Up Authentication for Server to Server Production Applications


来源:https://stackoverflow.com/questions/52078058/cloud-functions-python-to-access-datastore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!