使用gcloud通过Eventarc部署带Firestore触发器的Cloud Function报错
环境信息
- Google Cloud Function(第二代)
- 运行时:Python 3.12
- Firestore数据库
- gcloud CLI版本:465.0.0
需求目标
让Cloud Function在以下嵌套路径的Firestore文档发生写入操作(创建、更新、删除)时触发:MyCollection/{userId}/MySubCollection/{personId}/MyNestedCollection/{sessionId}
问题详情
执行部署命令时,--trigger-event-filters-path-pattern参数触发语法错误,提示路径模式的字典参数格式不正确。
使用的部署命令
gcloud beta functions deploy trigger_generate_insights \ --gen2 \ --runtime python312 \ --entry-point trigger_generate_insights \ --trigger-event-filters="type=google.cloud.firestore.document.v1.written" \ --trigger-event-filters-path-pattern="projects/my-project/databases/(default)/documents/MyCollection/{userId}/MySubCollection/{personId}/MyNestedCollection/{sessionId}" \ --trigger-location="us-central1" \ --region us-central1 \ --memory 128MB \ --allow-unauthenticated
错误信息
ERROR: (gcloud.beta.functions.deploy) argument
--trigger-event-filters-path-pattern: Bad syntax for dict arg: [projects/my-project/databases/(default)/documents/MyCollection/{userId}/MySubCollection/{personId}/MyNestedCollection/{sessionId}].
Please seegcloud topic flags-fileorgcloud topic escapingfor
information on providing list or dictionary flag values with special
characters.
已尝试的解决方法
- 转义路径中的大括号
{}并添加引号 - 查阅
gcloud topic flags-file和gcloud topic escaping文档,但未找到有效解决方案
解决方案
问题出在--trigger-event-filters-path-pattern的参数格式,该参数要求是键值对形式(字典类型),需要明确指定键为document,值为路径模式。修正后的部署命令如下:
gcloud beta functions deploy trigger_generate_insights \ --gen2 \ --runtime python312 \ --entry-point trigger_generate_insights \ --trigger-event-filters="type=google.cloud.firestore.document.v1.written" \ --trigger-event-filters-path-pattern="document=projects/my-project/databases/(default)/documents/MyCollection/{userId}/MySubCollection/{personId}/MyNestedCollection/{sessionId}" \ --trigger-location="us-central1" \ --region us-central1 \ --memory 128MB \ --allow-unauthenticated
核心修改点:在路径前添加document=,满足该参数的字典格式要求,不需要额外转义大括号。
内容的提问来源于stack exchange,提问作者wellsinsideout

