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

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

โปรดดูเอกสาร Cloud Functions สำหรับข้อมูลทั่วไปในการใช้ Cloud Functions

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

Firebase รองรับทั้ง Cloud Functions รุ่นที่ 1 และ 2 อย่างไรก็ตาม Firebase ปัจจุบันส่วนขยายมีข้อจำกัดเกี่ยวกับระบบคลาวด์รุ่นใด ที่คุณสามารถใช้กับทริกเกอร์บางประเภท ด้วยเหตุนี้ ผู้คนจำนวนมาก ส่วนขยายจะมีทั้งฟังก์ชันรุ่นที่ 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 เขียนด้วย TypeScript คุณสามารถตรวจสอบส่วนขยายเหล่านั้น แนวทางปฏิบัติในการใช้ TypeScript สำหรับส่วนขยายของคุณ

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

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

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

  2. ในไฟล์ package.json ให้กำหนดฟิลด์ main ไปที่ตำแหน่ง JavaScript ที่สร้างขึ้น

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

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

ทริกเกอร์ HTTP

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

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

คำจำกัดความของฟังก์ชัน (รุ่นที่ 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 แต่จะใช้ ที่อำนวยความสะดวกในการเรียกจากโค้ดฝั่งไคลเอ็นต์ของคุณ

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

คำจำกัดความของฟังก์ชัน (รุ่นที่ 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() ของ 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

การตรวจสอบสิทธิ์

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

ดูทริกเกอร์การตรวจสอบสิทธิ์ 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: ...

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

ทริกเกอร์เหตุการณ์ 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

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

ดูทริกเกอร์ฐานข้อมูลเรียลไทม์ในเอกสารประกอบเรื่อง 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

การกำหนดค่าระยะไกล

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

โปรดดูทริกเกอร์การกำหนดค่าระยะไกลในเอกสารประกอบของ 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 สำหรับ ข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์พื้นที่เก็บข้อมูล

คำจำกัดความของฟังก์ชัน (รุ่นที่ 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

ทริกเกอร์การแจ้งเตือนการตรวจสอบประสิทธิภาพ

ฟังก์ชันที่ทริกเกอร์การตรวจสอบประสิทธิภาพจะทำงานเมื่อการตรวจสอบประสิทธิภาพเผยแพร่การแจ้งเตือน

ดูทริกเกอร์การแจ้งเตือนของ 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 จะทำงานเมื่อมีการเผยแพร่ประเภทเหตุการณ์ที่เฉพาะเจาะจงไปยัง แชแนลใดแชแนลหนึ่งโดยเฉพาะ

โปรดดูสร้างและจัดการทริกเกอร์เหตุการณ์ที่กำหนดเองในฟังก์ชันระบบคลาวด์ เอกสารประกอบเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์ 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 ช่องในภูมิภาค 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}