เขียน Cloud Functions สำหรับส่วนขยาย

เมื่อสร้างส่วนขยาย คุณจะเขียนตรรกะของส่วนขยายโดยใช้ Cloud Functions ในลักษณะเดียวกับที่คุณเขียนฟังก์ชันที่จะใช้ในโปรเจ็กต์ของคุณเองเท่านั้น คุณประกาศฟังก์ชันในไฟล์ extension.yaml และเมื่อผู้ใช้ติดตั้งส่วนขยาย ระบบจะ นำฟังก์ชันเหล่านี้ไปใช้ในโปรเจ็กต์ของผู้ใช้

ดูข้อมูลทั่วไปเกี่ยวกับการใช้ Cloud Functionsได้ในเอกสารประกอบของ Cloud Functions

รุ่นที่ 1 และ 2 Cloud Functions

Firebase รองรับทั้งรุ่นที่ 1 และรุ่นที่ 2Cloud Functions อย่างไรก็ตาม ปัจจุบัน Firebase Extensions มีข้อจำกัดบางอย่างเกี่ยวกับ Cloud Functions รุ่นที่คุณใช้กับทริกเกอร์บางประเภทได้ ด้วยเหตุนี้ ส่วนขยายจำนวนมากจึงมีฟังก์ชันทั้งรุ่นที่ 1 และรุ่นที่ 2

การรองรับการสร้างฟังก์ชันจะระบุไว้สำหรับทริกเกอร์แต่ละประเภทด้านล่าง

สิ่งที่ควรพิจารณาเป็นพิเศษ

  • คำจำกัดความของฟังก์ชันบางอย่างกำหนดให้คุณต้องระบุข้อมูลที่ระบุไว้ในไฟล์ extension.yaml ด้วย เช่น Cloud Firestore มี document()เมธอดที่ระบุรูปแบบเอกสารที่จะดู และการประกาศที่เกี่ยวข้องใน extension.yaml มีฟิลด์ resource ที่ระบุรูปแบบเดียวกัน

    ในกรณีเหล่านี้ ระบบจะใช้การกำหนดค่าที่ระบุในไฟล์ extension.yaml และจะละเว้นการกำหนดค่าที่ระบุในคำจำกัดความของฟังก์ชัน

    โดยทั่วไปแล้ว เราจะระบุค่าที่กำหนดไว้ในคำจำกัดความของฟังก์ชัน เพื่อเป็นเอกสารประกอบ ตัวอย่างในหน้านี้เป็นไปตามรูปแบบนี้

  • Cloud Functions SDK รุ่นที่ 1 มีเมธอด functions.config()และ functions:config:setคำสั่ง CLI ที่คุณใช้เพื่อทำงานกับ ค่าที่กำหนดพารามิเตอร์ในฟังก์ชันรุ่นที่ 1 ได้ เราเลิกใช้งานเทคนิคนี้ใน Cloud Functions และจะใช้งานไม่ได้เลยในส่วนขยาย แต่ให้ใช้โมดูล functions.params (แนะนำ) หรือ process.env แทน

การใช้ TypeScript

เอกสารประกอบส่วนใหญ่สำหรับการพัฒนาส่วนขยายของคุณเองจะอธิบายเวิร์กโฟลว์ โดยใช้ JavaScript สำหรับ Cloud Functions for Firebase แต่คุณสามารถเขียนฟังก์ชันโดยใช้ TypeScript แทนได้

โดยส่วนขยายFirebase อย่างเป็นทางการFirebaseทั้งหมด เขียนด้วย TypeScript คุณสามารถตรวจสอบส่วนขยายเหล่านั้นเพื่อดูแนวทางปฏิบัติแนะนำบางส่วนในการใช้ TypeScript สำหรับส่วนขยายของคุณ

หากเขียนฟังก์ชันของส่วนขยายใน TypeScript คุณต้องทำสิ่งต่อไปนี้ก่อนติดตั้งส่วนขยาย

  1. คอมไพล์ซอร์สโค้ดฟังก์ชันของส่วนขยายเป็น JavaScript

    คำสั่ง firebase ext:dev:init ช่วยให้คุณเลือก TypeScript เพื่อเขียนฟังก์ชันได้ คำสั่ง จะให้ส่วนขยายที่ติดตั้งได้สมบูรณ์แก่คุณรวมถึงสคริปต์ บิลด์ที่คุณเรียกใช้ด้วย npm run build ได้

  2. ในไฟล์ package.json ให้ตรวจสอบว่าฟิลด์ main ชี้ไปที่ JavaScript ที่สร้างขึ้น

  3. หากคุณกำลังติดตั้งหรืออัปโหลดส่วนขยายจากแหล่งที่มาในเครื่อง ให้คอมไพล์ไฟล์ TypeScript ก่อน

ทริกเกอร์ฟังก์ชันที่รองรับ

ทริกเกอร์ HTTP

ฟังก์ชันที่ทริกเกอร์ด้วย HTTP จะได้รับการติดตั้งใช้งานในhttpsปลายทางสาธารณะและทำงาน เมื่อมีการเข้าถึงปลายทาง

ดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์โดย HTTP ได้ที่เรียกใช้ฟังก์ชันผ่านคำขอ HTTP ในCloud Functions เอกสารประกอบ

คำจำกัดความของฟังก์ชัน (รุ่นที่ 1 เท่านั้น)

import { https } from "firebase-functions/v1";

export const yourFunctionName = https.onRequest(async (req, resp) => {
  // ...
});

การประกาศทรัพยากร (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      runtime: nodejs16
      httpsTrigger: {}
  - name: anotherFunction
    type: ...

ฟังก์ชันที่เรียกใช้ได้

ฟังก์ชันที่เรียกใช้ได้จะคล้ายกับฟังก์ชันที่ทริกเกอร์โดย HTTP แต่จะใช้ โปรโตคอลที่ทำให้เรียกใช้จากโค้ดฝั่งไคลเอ็นต์ได้อย่างสะดวก

ดูข้อมูลเกี่ยวกับการใช้ฟังก์ชันที่เรียกใช้ได้ในเรียกใช้ฟังก์ชันจากแอปในเอกสารประกอบของ Cloud Functions

คำจำกัดความของฟังก์ชัน (รุ่นที่ 1 เท่านั้น)

import { https } from "firebase-functions/v1";

export const yourFunctionName = https.onCall(async (data, context) => {
  // ...
});

การประกาศทรัพยากร (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      runtime: nodejs16
      httpsTrigger: {}
  - name: anotherFunction
    type: ...

ทริกเกอร์ฟังก์ชันที่กำหนดเวลาแล้ว

ฟังก์ชันที่กำหนดเวลาไว้จะทำงานซ้ำๆ ตามกำหนดการที่ปรับแต่งได้

ดูฟังก์ชันกำหนดเวลาในเอกสารประกอบของ Cloud Functions เพื่อดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่กำหนดเวลา

คำจำกัดความของฟังก์ชัน (รุ่นที่ 1 เท่านั้น)

import { pubsub } from "firebase-functions/v1";

export const yourFunctionName = pubsub.schedule("every 6 hours").onRun((context) => {
  // ...
});

การประกาศทรัพยากร (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      scheduleTrigger:
        schedule: 'every 5 minutes'
  - name: anotherFunction
    type: ...

ฟิลด์ย่อยที่ใช้ได้สำหรับ scheduleTrigger มีดังนี้

ฟิลด์ คำอธิบาย
schedule
(ต้องระบุ)

ความถี่ที่คุณต้องการให้ฟังก์ชันทำงาน

ฟิลด์นี้ยอมรับสตริงที่ใช้ไวยากรณ์ใดไวยากรณ์หนึ่ง (ต้องใส่ในเครื่องหมายคำพูดเดี่ยว)

timeZone
(ไม่บังคับ)

เขตเวลาที่กำหนดการจะทำงาน

หากต้องการให้ผู้ใช้กำหนดค่ากำหนดเวลาได้เมื่อติดตั้งส่วนขยาย ให้เพิ่มพารามิเตอร์ใหม่ลงในไฟล์ extension.yaml และอ้างอิงพารามิเตอร์ในประกาศ resource ของฟังก์ชัน

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      scheduleTrigger:
        schedule: ${SCHEDULE_FREQUENCY}
  - name: anotherFunction
    type: ...

params:
  - param: SCHEDULE_FREQUENCY
    label: Schedule
    description: How often do you want to run yourFunctionName()?
    type: string
    default: 'every 5 minutes'  # Specifying a default is optional.
    required: true

ทริกเกอร์คิวงาน

ฟังก์ชันคิวของงานจะทริกเกอร์ในเหตุการณ์วงจร ของส่วนขยาย หรือเมื่อเพิ่มลงในคิวของงานของส่วนขยายด้วยตนเองโดยใช้เมธอด TaskQueue.enqueue() ของ Admin SDK

ดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่จัดการเหตุการณ์วงจรของส่วนขยายได้ที่จัดการเหตุการณ์วงจรของส่วนขยาย

ดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันคิวของงานได้ที่ฟังก์ชันการจัดคิวด้วย Cloud Tasks ในเอกสารประกอบของ Cloud Functions

คำจำกัดความของฟังก์ชัน (รุ่นที่ 1 เท่านั้น)

import { tasks } from "firebase-functions/v1";

export const yourFunctionName = tasks.taskQueue().onDispatch(async (data, context) => {
  // ...
});

การประกาศทรัพยากร (extension.yaml)

resources:
  - name: myTaskFunction
    type: firebaseextensions.v1beta.function
    description: >-
      Perform a task when triggered by a lifecycle event
    properties:
      taskQueueTrigger: {}

ตั้งค่าพร็อพเพอร์ตี้ taskQueueTrigger เป็น {} หรือแผนที่ของตัวเลือกที่ ปรับขีดจำกัดอัตราและลักษณะการลองใหม่ของคิวงาน (ดูการปรับคิวงาน)

หากต้องการเรียกใช้ฟังก์ชันในเหตุการณ์วงจรของส่วนขยาย ให้เพิ่ม ระเบียน lifecycleEvents ที่มีชื่อฟังก์ชันและข้อความการประมวลผล ที่ไม่บังคับ ซึ่งจะแสดงในคอนโซล Firebase เมื่อเริ่ม การประมวลผล

lifecycleEvents:
  onInstall:
    function: myTaskFunction
    processingMessage: Resizing your existing images
  onUpdate:
    function: myOtherTaskFunction
    processingMessage: Setting up your extension
  onConfigure:
    function: myOtherTaskFunction
    processingMessage: Setting up your extension

Analytics

ฟังก์ชันที่ทริกเกอร์โดย Analytics จะทํางานเมื่อระบบบันทึกเหตุการณ์ Analytics ที่ระบุ

ดูทริกเกอร์ Google Analytics ในเอกสารประกอบของ Cloud Functions เพื่อดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์โดย Analytics

คำจำกัดความของฟังก์ชัน (รุ่นที่ 1 เท่านั้น)

import { analytics } from "firebase-functions/v1";

export const yourFunctionName = analytics.event("event_name").onLog((event, context) => {
  // ...
});

การประกาศทรัพยากร (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/google.firebase.analytics/eventTypes/event.log
        resource: projects/${PROJECT_ID}/events/ga_event
  - name: anotherFunction
    type: ...

หากต้องการให้ผู้ใช้กำหนดค่าAnalyticsเหตุการณ์เพื่อรอรับฟังได้เมื่อติดตั้งส่วนขยาย ให้เพิ่มพารามิเตอร์ใหม่ลงในไฟล์ extension.yaml และอ้างอิงพารามิเตอร์ในการประกาศ resource ของฟังก์ชัน

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/google.firebase.analytics/eventTypes/event.log
        resource: projects/${PROJECT_ID}/events/${EVENT_NAME}
  - name: anotherFunction
    type: ...

params:
  - param: EVENT_NAME
    label: Analytics event
    description: What event do you want to respond to?
    type: string
    default: ga_event  # Specifying a default is optional.
    required: true

Authentication

ฟังก์ชันที่ทริกเกอร์การตรวจสอบสิทธิ์จะทำงานเมื่อมีการสร้างหรือลบผู้ใช้

ดูทริกเกอร์การตรวจสอบสิทธิ์ Firebase ในเอกสารCloud Functions สำหรับข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์โดยการตรวจสอบสิทธิ์

คำจำกัดความของฟังก์ชัน (รุ่นที่ 1 เท่านั้น)

import { auth } from "firebase-functions/v1";

export const yourFunctionName = auth.user().onCreate((user, context) => {
  // ...
});

export const yourFunctionName2 = auth.user().onDelete((user, context) => {
  // ...
});

การประกาศทรัพยากร (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/firebase.auth/eventTypes/user.create
        resource: projects/${PROJECT_ID}
  - name: anotherFunction
    type: ...

ตารางต่อไปนี้แสดงวิธีระบุAuthenticationเหตุการณ์ แต่ละประเภทที่รองรับ

Cloud Functions ทริกเกอร์เหตุการณ์ eventType คำอธิบาย
onCreate() providers/firebase.auth/eventTypes/user.create สร้างผู้ใช้ใหม่แล้ว
onDelete() providers/firebase.auth/eventTypes/user.delete มีการลบผู้ใช้

Cloud Firestore

ฟังก์ชันที่ทริกเกอร์โดย Cloud Firestore จะทำงานเมื่อมีการสร้าง อัปเดต หรือลบเอกสาร

ดูทริกเกอร์ Cloud Firestore ในเอกสารประกอบของ Cloud Functions เพื่อดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์โดย Firestore

คำจำกัดความของฟังก์ชัน (รุ่นที่ 1 เท่านั้น)

import { firestore } from "firebase-functions/v1";

export const yourFunctionName = firestore.document("collection/{doc_id}")
  .onCreate((snapshot, context) => {
    // ...
  });

export const yourFunctionName2 = firestore.document("collection/{doc_id}")
  .onUpdate((change, context) => {
    // ...
  });

export const yourFunctionName3 = firestore.document("collection/{doc_id}")
  .onDelete((snapshot, context) => {
    // ...
  });

export const yourFunctionName4 = firestore.document("collection/{doc_id}")
  .onWrite((change, context) => {
    // onWrite triggers on creation, update, and deletion.
    // ...
  });

การประกาศทรัพยากร (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/cloud.firestore/eventTypes/document.write
        resource: projects/${PROJECT_ID}/databases/(default)/documents/collection/{documentID}
  - name: anotherFunction
    type: ...

ตารางต่อไปนี้แสดงวิธีระบุCloud Firestore ประเภทเหตุการณ์ที่รองรับแต่ละประเภท

Cloud Functions ทริกเกอร์เหตุการณ์ eventType คำอธิบาย
onCreate() providers/cloud.firestore/eventTypes/document.create สร้างเอกสารใหม่แล้ว
onDelete() providers/cloud.firestore/eventTypes/document.delete ลบเอกสารแล้ว
onUpdate() providers/cloud.firestore/eventTypes/document.update อัปเดตเอกสารแล้ว
onWrite() providers/cloud.firestore/eventTypes/document.write สร้าง ลบ หรืออัปเดตเอกสาร

หากต้องการให้ผู้ใช้กำหนดค่าเส้นทางเอกสารได้เมื่อติดตั้งส่วนขยายของคุณ ให้เพิ่มพารามิเตอร์ใหม่ลงในไฟล์ extension.yaml และอ้างอิงพารามิเตอร์ในการประกาศ resource ของฟังก์ชัน

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/cloud.firestore/eventTypes/document.write
        resource: projects/${PROJECT_ID}/databases/(default)/documents/${YOUR_DOCUMENT_PATH}
  - name: anotherFunction
    type: ...

params:
  - param: YOUR_DOCUMENT_PATH
    label: Cloud Firestore path
    description: Where do you want to watch for changes?
    type: string
    default: path/to/{documentID}  # Specifying a default is optional.
    required: true

Pub/Sub

ฟังก์ชันที่ทริกเกอร์โดย Pub/Sub จะทำงานเมื่อมีการเผยแพร่ข้อความไปยังหัวข้อที่เฉพาะเจาะจง

ดูทริกเกอร์ Pub/Sub ในเอกสารประกอบของ Cloud Functions เพื่อดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์โดย Pub/Sub

คำจำกัดความของฟังก์ชัน (รุ่นที่ 1 เท่านั้น)

import { pubsub } from "firebase-functions/v1";

export const yourFunctionName = pubsub.topic("topic_name").onPublish((message, context) => {
  // ...
});

การประกาศทรัพยากร (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.pubsub.topic.publish
        resource: projects/${PROJECT_ID}/topics/topic-name
  - name: anotherFunction
    type: ...

หากต้องการให้ผู้ใช้กำหนดค่าPub/Sub หัวข้อได้เมื่อ ติดตั้งส่วนขยาย ให้เพิ่มพารามิเตอร์ใหม่ลงในไฟล์ extension.yaml และอ้างอิงพารามิเตอร์ในการประกาศ resource ของฟังก์ชัน

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.pubsub.topic.publish
        resource: projects/${PROJECT_ID}/topics/${PUBSUB_TOPIC}
  - name: anotherFunction
    type: ...

params:
  - param: PUBSUB_TOPIC
    label: Pub/Sub topic
    description: Which Pub/Sub topic do you want to watch for messages?
    type: string
    default: topic-name  # Specifying a default is optional.
    required: true

Realtime Database

ฟังก์ชันที่ทริกเกอร์โดย Realtime Database จะทำงานเมื่อมีการสร้าง อัปเดต หรือลบเส้นทางที่ตรงกับรูปแบบที่ระบุ

ดูทริกเกอร์ของ Realtime Database ในเอกสารประกอบของ Cloud Functions เพื่อดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์โดย RTDB

คำจำกัดความของฟังก์ชัน (รุ่นที่ 1 เท่านั้น)

import { database } from "firebase-functions/v1";

export const yourFunctionName = database.ref("path/to/{item}")
  .onCreate((snapshot, context) => {
    // ...
  });

export const yourFunctionName2 = database.ref("path/to/{item}")
  .onUpdate((change, context) => {
    // ...
  });

export const yourFunctionName3 = database.ref("path/to/{item}")
  .onDelete((snapshot, context) => {
    // ...
  });

export const yourFunctionName4 = database.ref("path/to/{item}")
  .onWrite((change, context) => {
    // onWrite triggers on creation, update, and deletion.
    // ...
  });

การประกาศทรัพยากร (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/google.firebase.database/eventTypes/ref.create
        # DATABASE_INSTANCE (project's default instance) is an auto-populated
        # parameter value. You can also specify an instance.
        resource: projects/_/instances/${DATABASE_INSTANCE}/refs/path/to/{itemId}
  - name: anotherFunction
    type: ...

ตารางต่อไปนี้แสดงวิธีระบุCloud Firestore ประเภทเหตุการณ์ที่รองรับแต่ละประเภท

Cloud Functions ทริกเกอร์เหตุการณ์ eventType คำอธิบาย
onCreate() providers/google.firebase.database/eventTypes/ref.create สร้างข้อมูลแล้ว
onDelete() providers/google.firebase.database/eventTypes/ref.delete ลบข้อมูลแล้ว
onUpdate() providers/google.firebase.database/eventTypes/ref.update อัปเดตข้อมูลแล้ว
onWrite() providers/google.firebase.database/eventTypes/ref.write ข้อมูลที่สร้าง ลบ หรืออัปเดต

หากต้องการให้ผู้ใช้กำหนดค่าเส้นทางที่จะดูได้เมื่อติดตั้งส่วนขยายของคุณ ให้เพิ่มพารามิเตอร์ใหม่ลงในไฟล์ extension.yaml และอ้างอิงพารามิเตอร์ในการประกาศ resource ของฟังก์ชัน

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/google.firebase.database/eventTypes/ref.create
        # DATABASE_INSTANCE (project's default instance) is an auto-populated
        # parameter value. You can also specify an instance.
        resource: projects/_/instances/${DATABASE_INSTANCE}/refs/${DB_PATH}
  - name: anotherFunction
    type: ...

params:
  - param: DB_PATH
    label: Realtime Database path
    description: Where do you want to watch for changes?
    type: string
    default: path/to/{itemId}  # Specifying a default is optional.
    required: true

Remote Config

ฟังก์ชันที่ทริกเกอร์โดยการกำหนดค่าระยะไกลจะทำงานเมื่อมีการอัปเดตเทมเพลตพารามิเตอร์ของโปรเจ็กต์

ดูทริกเกอร์การกำหนดค่าระยะไกลในเอกสารประกอบของ Cloud Functions เพื่อดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์การกำหนดค่าระยะไกล

คำจำกัดความของฟังก์ชัน (รุ่นที่ 1 เท่านั้น)

import { remoteConfig } from "firebase-functions/v1";

export const yourFunctionName = remoteConfig.onUpdate((version, context) => {
  // ...
});

การประกาศทรัพยากร (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.firebase.remoteconfig.update
        resource: projects/${PROJECT_ID}
  - name: anotherFunction
    type: ...

Cloud Storage

ฟังก์ชันที่ทริกเกอร์โดย Cloud Storage จะทำงานเมื่อมีการสร้าง เก็บถาวร หรือ ลบออบเจ็กต์ หรือเมื่อมีการเปลี่ยนแปลงข้อมูลเมตาของออบเจ็กต์

ดูทริกเกอร์ Cloud Storage ในเอกสารประกอบของ Cloud Functions เพื่อดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์โดย Storage

คำจำกัดความของฟังก์ชัน (รุ่นที่ 1 เท่านั้น)

import { storage } from "firebase-functions/v1";

export const yourFunctionName = storage.object().onFinalize((object, context) => {
  // ...
});

export const yourFunctionName2 = storage.object().onMetadataUpdate((object, context) => {
  // ...
});

export const yourFunctionName3 = storage.object().onArchive((object, context) => {
  // ...
});

export const yourFunctionName4 = storage.object().onDelete((object, context) => {
  // ...
});

การประกาศทรัพยากร (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.storage.object.finalize
        # STORAGE_BUCKET (project's default bucket) is an auto-populated
        # parameter. You can also specify a bucket.
        resource: projects/_/buckets/${STORAGE_BUCKET}
  - name: anotherFunction
    type: ...

ตารางต่อไปนี้แสดงวิธีระบุCloud Storage ประเภทเหตุการณ์ที่รองรับแต่ละประเภท

Cloud Functions ทริกเกอร์เหตุการณ์ eventType คำอธิบาย
onFinalize() google.storage.object.finalize สร้างออบเจ็กต์แล้ว
onMetadataUpdate() google.storage.object.metadataUpdate อัปเดตข้อมูลเมตาของออบเจ็กต์แล้ว
onArchive() google.storage.object.archive เก็บออบเจ็กต์แล้ว
onDelete() google.storage.object.delete ลบออบเจ็กต์แล้ว

หากต้องการให้ผู้ใช้กำหนดค่าที่เก็บข้อมูลได้เมื่อติดตั้งส่วนขยาย ของคุณ ให้เพิ่มพารามิเตอร์ใหม่ลงในไฟล์ extension.yaml และอ้างอิง พารามิเตอร์ในการประกาศ resource ของฟังก์ชัน

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.storage.object.finalize
        resource: projects/_/buckets/${YOUR_BUCKET}
  - name: anotherFunction
    type: ...

params:
  - param: YOUR_BUCKET
    label: Cloud Storage bucket
    description: Which bucket do you want to watch for changes?
    type: selectResource
    resourceType: storage.googleapis.com/Bucket
    default: ${STORAGE_BUCKET}  # Specifying a default is optional.
    required: true

Test Lab

ฟังก์ชันที่ทริกเกอร์โดย Test Lab จะทํางานเมื่อเมทริกซ์การทดสอบทําการทดสอบเสร็จสิ้น

ดูทริกเกอร์ Firebase Test Lab ในเอกสารประกอบของ Cloud Functions เพื่อดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์โดย Test Lab

คำจำกัดความของฟังก์ชัน (รุ่นที่ 1 เท่านั้น)

import { testLab } from "firebase-functions/v1";

export const yourFunctionName = testLab.testMatrix().onComplete((matrix, context) => {
  // ...
});

การประกาศทรัพยากร (extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.testing.testMatrix.complete
        resource: projects/${PROJECT_ID}/testMatrices/{matrixId}
  - name: anotherFunction
    type: ...

ทริกเกอร์การแจ้งเตือน Crashlytics รายการ

ฟังก์ชันที่ทริกเกอร์โดย Crashlytics จะทำงานเมื่อ Crashlytics เผยแพร่การแจ้งเตือน

ดูทริกเกอร์การแจ้งเตือนของ Firebase ในเอกสารประกอบของ Cloud Functions เพื่อดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์การแจ้งเตือน

คำจำกัดความของฟังก์ชัน (รุ่นที่ 2 เท่านั้น)

import {
  onNewFatalIssuePublished,
  onNewNonfatalIssuePublished,
  onNewAnrIssuePublished,
  onRegressionAlertPublished,
  onVelocityAlertPublished,
  onStabilityDigestPublished,
} from "firebase-functions/v2/alerts/crashlytics";

export const yourFunctionName = onNewFatalIssuePublished((event) => {
  // ...
});

export const yourFunctionName2 = onNewNonfatalIssuePublished((event) => {
  // ...
});

export const yourFunctionName3 = onNewAnrIssuePublished((event) => {
  // ...
});

export const yourFunctionName4 = onRegressionAlertPublished((event) => {
  // ...
});

export const yourFunctionName5 = onVelocityAlertPublished((event) => {
  // ...
});

export const yourFunctionName6 = onStabilityDigestPublished((event) => {
  // ...
});

การประกาศทรัพยากร (extension.yaml)

apis:
  - apiName: eventarc.googleapis.com
    reason: Powers all events and triggers
  - apiName: run.googleapis.com
    reason: Powers 2nd-gen functions

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      buildConfig:
        runtime: nodejs16
      serviceConfig:
        availableMemory: 512M
      eventTrigger:
        eventType: google.firebase.firebasealerts.alerts.v1.published
        triggerRegion: global
        eventFilters:
          - attribute: alerttype
            value: crashlytics.newFatalIssue
  - name: anotherFunction
    type: ...

คุณใช้ค่าต่อไปนี้สำหรับ alerttype ได้

  • crashlytics.newFatalIssue
  • crashlytics.newNonfatalIssue
  • crashlytics.regression
  • crashlytics.stabilityDigest
  • crashlytics.velocity
  • crashlytics.newAnrIssue

ทริกเกอร์การแจ้งเตือน Performance Monitoring รายการ

ฟังก์ชันที่ทริกเกอร์โดย Performance Monitoring จะทำงานเมื่อ Performance Monitoring เผยแพร่การแจ้งเตือน

ดูทริกเกอร์การแจ้งเตือนของ Firebase ในเอกสารประกอบของ Cloud Functions เพื่อดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์การแจ้งเตือน

คำจำกัดความของฟังก์ชัน (รุ่นที่ 2 เท่านั้น)

import { onThresholdAlertPublished } from "firebase-functions/v2/alerts/performance";

export const yourFunctionName = onThresholdAlertPublished((event) => {
  // ...
});

การประกาศทรัพยากร (extension.yaml)

apis:
  - apiName: eventarc.googleapis.com
    reason: Powers all events and triggers
  - apiName: run.googleapis.com
    reason: Powers 2nd-gen functions

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      buildConfig:
        runtime: nodejs16
      serviceConfig:
        availableMemory: 512M
      eventTrigger:
        eventType: google.firebase.firebasealerts.alerts.v1.published
        triggerRegion: global
        eventFilters:
          - attribute: alerttype
            value: performance.threshold
  - name: anotherFunction
    type: ...

ทริกเกอร์การแจ้งเตือน App Distribution รายการ

ฟังก์ชันที่ทริกเกอร์โดย App Distribution จะทำงานเมื่อ App Distribution เผยแพร่การแจ้งเตือน

ดูทริกเกอร์การแจ้งเตือนของ Firebase ในเอกสารประกอบของ Cloud Functions เพื่อดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์การแจ้งเตือน

คำจำกัดความของฟังก์ชัน (รุ่นที่ 2 เท่านั้น)

import {
  onNewTesterIosDevicePublished,
  onInAppFeedbackPublished
} from "firebase-functions/v2/alerts/appDistribution";

export const yourFunctionName = onNewTesterIosDevicePublished((event) => {
  // ...
});

export const yourFunctionName2 = onInAppFeedbackPublished((event) => {
  // ...
});

การประกาศทรัพยากร (extension.yaml)

apis:
  - apiName: eventarc.googleapis.com
    reason: Powers all events and triggers
  - apiName: run.googleapis.com
    reason: Powers 2nd-gen functions

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      buildConfig:
        runtime: nodejs16
      serviceConfig:
        availableMemory: 512M
      eventTrigger:
        eventType: google.firebase.firebasealerts.alerts.v1.published
        triggerRegion: global
        eventFilters:
          - attribute: alerttype
            value: appDistribution.inAppFeedback
  - name: anotherFunction
    type: ...

คุณใช้ค่าต่อไปนี้สำหรับ alerttype ได้

  • appDistribution.newTesterIosDevice
  • appDistribution.inAppFeedback

ทริกเกอร์เหตุการณ์ที่กำหนดเอง (Eventarc)

ฟังก์ชันที่ทริกเกอร์โดย Eventarc จะทํางานเมื่อมีการเผยแพร่เหตุการณ์ประเภทหนึ่งๆ ไปยังช่องหนึ่งๆ

ดูสร้างและจัดการทริกเกอร์เหตุการณ์ที่กำหนดเองในCloud Functions เอกสารประกอบเพื่อดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์โดย Eventarc

นอกจากนี้ คุณยังเผยแพร่เหตุการณ์จากส่วนขยายเพื่อให้ผู้ใช้มีวิธีแทรกตรรกะที่กำหนดเองลงในส่วนขยายได้ด้วย ดูใช้ตรรกะที่กำหนดเองซึ่งนักพัฒนาซอฟต์แวร์ระบุไว้ในส่วนขยาย

คำจำกัดความของฟังก์ชัน (รุ่นที่ 2 เท่านั้น)

import { onCustomEventPublished } from "firebase-functions/v2/eventarc";

export const yourFunctionName = onCustomEventPublished((event) => {
  // ...
});

การประกาศทรัพยากร (extension.yaml)

apis:
  - apiName: eventarc.googleapis.com
    reason: Powers all events and triggers
  - apiName: run.googleapis.com
    reason: Powers 2nd-gen functions

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      # LOCATION is a user-configured parameter value specified by the user
      # during installation.
      location: ${param:LOCATION}
      buildConfig:
        runtime: nodejs16
      serviceConfig:
        availableMemory: 512M
        timeoutSeconds: 60
      eventTrigger:
        eventType: firebase.extensions.storage-resize-images.v1.complete
        channel: projects/${param:PROJECT_ID}/locations/us-central1/channels/firebase
  - name: anotherFunction
    type: ...

ช่องต้องมีอยู่แล้วเมื่อติดตั้งส่วนขยาย เช่น หากคุณต้องใช้เหตุการณ์ที่กําหนดเองจากส่วนขยายอื่นที่สร้างแชแนล ให้ผู้ใช้ติดตั้งส่วนขยายนั้นก่อน

ตัวอย่างข้างต้นจะสร้างทริกเกอร์เหตุการณ์ที่กําหนดเองสําหรับแชแนล Firebase "default" ในภูมิภาค us-central1 คุณปรับแต่งชื่อและภูมิภาคของช่องได้โดยใช้พารามิเตอร์ เช่น


params:
  - param: EVENTARC_CHANNEL_NAME
    label: Eventarc channel name
    description: What is the name of the Eventarc channel.
    default: firebase
    type: string
    required: true

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      location: ${param:LOCATION}
      eventTrigger:
        eventType: firebase.extensions.storage-resize-images.v1.complete
        channel: projects/${param:PROJECT_ID}/locations/${param:LOCATION}/channels/${param:EVENTARC_CHANNEL_NAME}