如何从命令行调用Google Cloud Function?调用2代函数遇404错误
问题描述
- 已创建Hello World版本的2代Google Cloud Function,通过
curl调用可正常运行 - 执行
gcloud functions list确认函数存在:
NAME STATE TRIGGER REGION ENVIRONMENT hello-world ACTIVE HTTP Trigger us-central1 2nd gen
- 执行
gcloud functions call hello-world时返回404错误:
ERROR: (gcloud.functions.call) ResponseError: status=[404], code=[Ok], message=[Function hello-world in region us-central1 in project test-project does not exist]
- 已尝试设置默认区域和可用区,但问题未解决:
gcloud compute project-info add-metadata \ --metadata google-compute-default-region=us-central1,google-compute-default-zone=us-central1-a
解决方案
核心原因是gcloud functions call默认针对1代Cloud Function,不会自动识别2代环境,必须明确指定参数:
- 直接带参数调用2代函数:
gcloud functions call hello-world --gen2 --region=us-central1
- 若想永久默认调用2代函数,可设置gcloud配置:
gcloud config set functions/gen2 true
之后执行gcloud functions call hello-world --region=us-central1即可正常调用。
注:你之前输入的命令中出现了gcp前缀,正确的Google Cloud命令行工具前缀是gcloud,请修正后使用。
内容的提问来源于stack exchange,提问作者yambo
相关产品推荐
相关产品推荐

