<p>谷歌云函数提供了<a href="https://cloud.google.com/functions/docs/reference/rest" rel="nofollow noreferrer">REST API interface</a>什么包括<em>调用</em>方法,可以在另一个云函数HTTP调用中使用。
尽管文档<a href="https://cloud.google.com/apis/docs/client-libraries-explained" rel="nofollow noreferrer">mention using Google-provided client libraries</a>仍然没有一个用于Python上的云函数</p>
<p>相反,您需要使用通用的GoogleAPI客户端库。[这是python版本]。<a href="https://github.com/googleapis/google-api-python-client/tree/master/docs" rel="nofollow noreferrer">3</a></p>
<p>使用这种方法的主要困难可能是理解身份验证过程。
通常,您需要提供两件事来构建客户端服务:
<em>凭证</em>ans<em>作用域</em></p>
<p>获取凭据的最简单方法是中继应用程序默认凭据(ADC)库。关于这一点的rigth文档包括:</p>
<ol>
<li><a href="https://cloud.google.com/docs/authentication/production" rel="nofollow noreferrer">https://cloud.google.com/docs/authentication/production</a></li>
<li><a href="https://github.com/googleapis/google-api-python-client/blob/master/docs/auth.md" rel="nofollow noreferrer">https://github.com/googleapis/google-api-python-client/blob/master/docs/auth.md</a></li>
</ol>
<p>获取<em>范围</em>的地方是each restapi函数文档页面。
比如<a href="https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions/call#authorization-scopes" rel="nofollow noreferrer">OAuth scope: https://www.googleapis.com/auth/cloud-platform</a></p>
<p>下面是调用“hello world”clound function的完整代码示例。
运行前:</p>
<ol>
<li>在项目中的GCP上创建默认云函数</李>
</ol>
<ul>
<li>保留并通知要使用的默认服务帐户</li>
<li>保留默认的主体</李>
</ul>
<ol start=“2”>
<li>请注意部署函数的<em>项目id</em>,<em>函数名</em>,<em>位置</em></李>
<li>如果要调用云函数环境外的函数(例如本地),请根据上述文档设置环境变量GOOGLE\u APPLICATION\u CREDENTIALS</li>
<li>如果您将实际从另一个云函数调用,则根本不需要配置凭据</李>
</ol>
<pre class="lang-python prettyprint-override"><code>from googleapiclient.discovery import build
from googleapiclient.discovery_cache.base import Cache
import google.auth
import pprint as pp
def get_cloud_function_api_service():
class MemoryCache(Cache):
_CACHE = {}
def get(self, url):
return MemoryCache._CACHE.get(url)
def set(self, url, content):
MemoryCache._CACHE[url] = content
scopes = ['https://www.googleapis.com/auth/cloud-platform']
# If the environment variable GOOGLE_APPLICATION_CREDENTIALS is set,
# ADC uses the service account file that the variable points to.
#
# If the environment variable GOOGLE_APPLICATION_CREDENTIALS isn't set,
# ADC uses the default service account that Compute Engine, Google Kubernetes Engine, App Engine, Cloud Run,
# and Cloud Functions provide
#
# see more on https://cloud.google.com/docs/authentication/production
credentials, project_id = google.auth.default(scopes)
service = build('cloudfunctions', 'v1', credentials=credentials, cache=MemoryCache())
return service
google_api_service = get_cloud_function_api_service()
name = 'projects/{project_id}/locations/us-central1/functions/function-1'
body = {
'data': '{ "message": "It is awesome, you are develop on Stack Overflow language!"}' # json passed as a string
}
result_call = google_api_service.projects().locations().functions().call(name=name, body=body).execute()
pp.pprint(result_call)
# expected out out is:
# {'executionId': '3h4c8cb1kwe2', 'result': 'It is awesome, you are develop on Stack Overflow language!'}
</code></pre>