【问题标题】:Limiting Google cloud function Quota限制谷歌云功能配额
【发布时间】:2020-03-15 20:26:25
【问题描述】:

我在 python 中有一个谷歌云函数,用于在同一个项目中部署新的谷歌云函数(来自我的云存储的 .zip 文件)。函数包含以下代码:

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/my-project/locations/us-central1/functions',
                               json={"name":"projects/my-project/locations/us-central1/functions/funct","runtime":"python37","sourceArchiveUrl":"gs://functions/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())  

我想为这些新功能添加某种配额/限制。这可以是调用次数、每分钟调用次数、在此功能上的花费等。您知道如何添加配额吗?以及如何将其添加到我的 Python 脚本中?

谢谢!

【问题讨论】:

  • 如果您想实现这一点,将不仅仅是几行粘贴到您的代码中。由于 Cloud Functions 调用是无状态的,因此您需要使用另一个 Cloud 产品实现某种持久计数器,以检查每个调用。除了由整个项目中所有产品的总支出触发的通用 GCP 每个项目计费警报之外,您也无法限制总支出。

标签: google-cloud-platform google-cloud-functions quota


【解决方案1】:

I wrote an article 使用 Cloud Endpoint 执行此操作。

即将推出更易于管理的内容,可能会在 3 周后向 Google Cloud Next 发布公告。敬请期待!

【讨论】:

    【解决方案2】:

    我无法为 Google Cloud 功能解决此问题。但是,我找到了 firebase 功能的解决方案。你可以在这里的问题中找到它: Rate limiting for Google/Firebase cloud functions?

    【讨论】:

      猜你喜欢
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 2019-05-14
      • 2020-09-03
      • 2021-10-12
      相关资源
      最近更新 更多