You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cloud Run第二代函数Firestore触发器事件数据解码失败求助

解决第二代Cloud Run Function Firestore触发器Protobuf解码错误

部署第二代Cloud Run Function,使用google.cloud.datastore.entity.v1.written作为Firestore触发事件,采用官方示例代码,依赖配置如下:

functions-framework==3.3.0
google-events==0.13.0
google-api-core==2.6.0
protobuf==5.28.3
cloudevents==1.9.0

但Firestore事件触发时持续报错:

google.protobuf.message.DecodeError: Error parsing message with type 'google.events.cloud.firestore.v1.DocumentEventData'

核心原因

protobuf==5.28.3版本过高,与google-events==0.13.0、google-api-core==2.6.0存在兼容性冲突。Protobuf 5.x系列对API做了大量变更,而当前版本的google-events尚未适配该版本,导致事件数据解码失败。

具体修复步骤

1. 调整依赖版本

修改requirements.txt为以下兼容版本组合:

functions-framework==3.3.0
google-events==0.13.0
google-api-core==2.11.1
protobuf==4.25.3
cloudevents==1.9.0
  • 降级protobuf到4.x稳定版,确保与google-events的proto定义兼容
  • 升级google-api-core到匹配protobuf 4.x的版本,避免额外兼容性问题

2. 验证代码事件解析逻辑

确保代码中正确导入并使用DocumentEventData,示例代码需符合以下结构:

from google.events.cloud.firestore.v1 import DocumentEventData
from cloudevents.http import CloudEvent
import functions_framework

@functions_framework.cloud_event
def firestore_trigger_handler(cloud_event: CloudEvent) -> None:
    # 解析CloudEvent中的Firestore事件数据
    event_data: DocumentEventData = DocumentEventData.from_cloud_event(cloud_event)
    # 处理事件逻辑
    print(f"触发文档路径: {event_data.name}")
    print(f"事件类型: {cloud_event['type']}")

3. 重新部署函数

清理旧依赖缓存后重新部署,确保新依赖被正确安装:

gcloud run deploy YOUR_FUNCTION_NAME \
  --source . \
  --trigger-event="google.cloud.datastore.entity.v1.written" \
  --trigger-resource="projects/YOUR_PROJECT_ID/databases/(default)/documents/YOUR_COLLECTION_NAME/{document}"
  • 替换YOUR_FUNCTION_NAME、YOUR_PROJECT_ID、YOUR_COLLECTION_NAME为实际值

4. 校验触发器配置

确认触发器的事件类型和资源路径无拼写错误:

  • 事件类型必须为google.cloud.datastore.entity.v1.written
  • 资源路径需准确指向目标Firestore集合,格式为projects/[项目ID]/databases/(default)/documents/[集合名]/{document}

内容的提问来源于stack exchange,提问作者Tokyo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 22:45:07