【问题标题】:Google cloud functions realtime database trigger setup in Python谷歌云函数在 Python 中设置实时数据库触发器
【发布时间】:2020-08-17 16:16:05
【问题描述】:

我正在尝试使用 Firebase 实时数据库触发器设置 Google Cloud 函数,但当我将文档添加到数据库时无法触发该函数。

我想要发生的是,当项目 xxx 下的 Firebase 数据库集合 yy​​y 有新条目时,我希望触发 Cloud Function funtion-1。 Function-1 是默认值(如下所示),测试运行良好。

我正在使用主控制台并创建了一个名为 function-1 的函数。我可以在 firebase 控制台中看到函数本身:

https://console.firebase.google.com/u/0/project/xxx/functions/list

我设置的collection在项目xxx下,命名为yyy,可以在下访问

https://console.firebase.google.com/u/0/project/xxx/database/firestore/data~2Fyyy

我在函数控制台中:

https://console.cloud.google.com/functions/edit/us-central1/function-1?project=xxx

设置如下:

  • 触发器:Firebase 实时数据库(测试版)
  • 事件类型:创建
  • 数据库:xxx
  • 路径:/data/yyy

运行时是 Python 3.7

代码是默认的 Google Cloud Functions 代码:

def hello_rtdb(event, context):
    """Triggered by a change to a Firebase RTDB reference.
    Args:
         event (dict): Event payload.
         context (google.cloud.functions.Context): Metadata for the event.
    """
    resource_string = context.resource
    # print out the resource string that triggered the function
    print(f"Function triggered by change to: {resource_string}.")
    # now print out the entire event object
    print(str(event))

requirements.txt 为空

我已在其他 Google Cloud Functions 中成功使用其他触发器(HTTP 或 PubSub),但我无法让该函数由数据库事件触发。我已经为路径变量尝试了多种选项,但无法使其工作。

我为路径变量尝试的选项是:

  • /xxx/database/firestore/data/yyy
  • /database/firestore/data/yyy
  • /data/yyy
  • /yyy
  • yyy 等等……

我确定我犯了一个基本错误,但遗憾的是文档没有帮助(可能是因为这是一个基本的事情)。如何以正确的方式进行设置?

【问题讨论】:

    标签: python firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    您使用的是 Google Firestore 还是 Firebase Firestore?我知道它们在技术上是一样的 产品在幕后,但我相信它们会引发不同的事件。这可能取决于是否 您从 Google Cloud Platform 或 Firebase 创建了数据库。

    $ gcloud functions event-types list
    
    EVENT_PROVIDER                   EVENT_TYPE                                                EVENT_TYPE_DEFAULT  RESOURCE_TYPE       RESOURCE_OPTIONAL
    google.firebase.database.ref     providers/google.firebase.database/eventTypes/ref.write   Yes                 firebase database   No
    google.firestore.document        providers/cloud.firestore/eventTypes/document.write       Yes                 firestore document  No
    

    我正在使用 event_provider=google.firestore.document 并且它有效。以下是我将 Python 函数部署到 Google Cloud 上的方法。假设main.py 与您的上述代码存在,但函数名称称为hello_firestore。

    $ gcloud functions deploy hello_firestore --entry-point hello_firestore --runtime python37 --trigger-event providers/cloud.firestore/eventTypes/document.write --trigger-resource "projects/$GCP_PROJECT/databases/(default)/documents/$DOC_PATH"
    

    对于 Firebase Firestore,它应该是这样的,但这没有经过测试,因为我是从 GCP 而不是 Firebase 创建我的 Firestore。

    $ gcloud functions deploy hello_rtdb --entry-point hello_rtdb --runtime python37 --trigger-event providers/google.firebase.database/eventTypes/ref.write --trigger-resource "projects/_/instances/$GCP_PROJECT/refs/$DOC_PATH"
    

    另外需要注意的是,只有 Firestore 原生模式支持触发事件,如 限制和保证部分中提到的 here。

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2020-04-19
      • 1970-01-01
      • 2019-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多