【发布时间】: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