问题描述
我在nodeJS中具有主要的Google云功能.在此函数内部,我想以编程方式在同一项目中创建另一个云函数.这项新的云功能的代码可以存储在Google云存储中的zip文件中.我尝试按照以下指南进行操作: https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions/create
I have a main Google cloud function in nodeJS. Inside this function I want programmatically create another cloud function inside the same project. The code of this new cloud function can be stored someone in a zip file in Google cloud storage. I tried to follow the guides over here: https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions/create
我能够在测试模拟器中运行查询,但是当我在Google Cloud函数中运行查询时,我不知道确切的代码!最好是我想在NodeJS中做到这一点.否则为Python.
I was able to run the query in the test emulator but when I run it inside a google cloud function I don't know the exact code! preferably I want to do this in NodeJS. Otherwise Python.
有人可以帮我吗?
推荐答案
我是在python中完成的:
I did this in python:
-
创建第一个云函数
hello_world
.
压缩功能并将其移动到Google Cloud Storage.
Zip the function and move it to Google Cloud Storage.
创建第二个云功能(默认情况下是使用具有编辑者角色的App Engine默认服务帐户创建的.如果您要分配其他服务帐户作为身份,请确保您的服务帐户具有查询权限元数据服务器并创建云功能)
Create the second cloud function (by default is created with App Engine default service account, which has Editor Role. In case you will assign a different service account as identity, make sure your service account has the permissions to query metadata server and to create cloud functions)
import requests
import json
def make_func(request):
# Get the access token from the metadata server
metadata_server_token_url = 'http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token?scopes=https://www.googleapis.com/auth/cloud-platform'
token_request_headers = {'Metadata-Flavor': 'Google'}
token_response = requests.get(metadata_server_token_url, headers=token_request_headers)
token_response_decoded = token_response.content.decode("utf-8")
jwt = json.loads(token_response_decoded)['access_token']
# Use the api you mentioned to create the function
response = requests.post('https://cloudfunctions.googleapis.com/v1/projects/your-project/locations/us-central1/functions',
json={"name":"projects/your-project/locations/us-central1/functions/funct","runtime":"python37","sourceArchiveUrl":"gs://bucket/main.zip","entryPoint":"hello_world","httpsTrigger": {} },
headers={'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer {}'.format(jwt)} )
if response:
return 'Success! Function Created'
else:
return str(response.json())
如果您有任何疑问,请告诉我
Let me know if you have any questions
这篇关于通过Google云功能创建Google功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!