【问题标题】:Create a Google function from a Google cloud function从 Google 云函数创建 Google 函数
【发布时间】:2020-06-21 20:38:04
【问题描述】:

我在 nodeJS 中有一个主要的谷歌云功能。在这个函数中,我想以编程方式在同一个项目中创建另一个云函数。这个新的云功能的代码可以存储在谷歌云存储中的一个 zip 文件中。我尝试按照这里的指南进行操作:https://cloud.google.com/functions/docs/reference/rest/v1/projects.locations.functions/create

我能够在测试模拟器中运行查询,但是当我在谷歌云函数中运行它时,我不知道确切的代码!最好我想在 NodeJS 中执行此操作。否则Python。

有人可以帮帮我吗?

【问题讨论】:

  • 不清楚你在问什么。我觉得有一个现有的云功能......你能告诉我们什么吗?你拥有它吗?你有它的来源吗?什么是新的云功能......它与现有的云功能有什么关系?
  • 您要部署的功能是否已经存在,或者您想即时创建它?如果存在,存储在哪里?
  • @guillaumeblaquiere 我更新了描述。即时创建它是什么意思?实际代码?代码可以存储在 zip 文件中的某个位置。我想将此代码部署到新函数中。
  • @Kolban 我更新了描述。我拥有这些功能。我想在同一个项目中部署一个新功能。这个函数的代码我很小。我可以将代码存储在 zip 文件中的某个位置。

标签: node.js google-cloud-platform google-cloud-functions


【解决方案1】:

我在 python 中做了这个:

  1. 创建第一个云函数hello_world

  2. 压缩函数并将其移动到 Google Cloud Storage。

  3. 创建第二个云函数(默认使用具有编辑角色的 App Engine 默认服务帐户创建。如果您将分配不同的服务帐户作为身份,请确保您的服务帐户具有查询权限元数据服务器和创建云功能)


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())  


如果您有任何问题,请告诉我

【讨论】:

  • 非常感谢!这就是我需要的!
  • 如何添加“允许未经身份验证”的功能。从上面的脚本创建这个函数时?
猜你喜欢
  • 1970-01-01
  • 2019-11-15
  • 2020-11-25
  • 1970-01-01
  • 2020-08-17
  • 2018-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多