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

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

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

ฟังก์ชั่นคลาวด์รุ่นที่ 1 และ 2

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

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

ข้อควรพิจารณาเป็นพิเศษ

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

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

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

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

การใช้ TypeScript

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

ในความเป็นจริง ส่วนขยาย 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

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

ทริกเกอร์ฟังก์ชันตามกำหนดเวลา

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

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

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

ดู ทริกเกอร์ของ Google Analytics ในเอกสารประกอบของฟังก์ชันคลาวด์สำหรับข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่เรียกใช้ของ 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 Authentication ในเอกสารประกอบของ 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 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-triggered จะทำงานเมื่อมีการเผยแพร่ข้อความในหัวข้อเฉพาะ

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

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

ฐานข้อมูลเรียลไทม์

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

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

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

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

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

คำจำกัดความของฟังก์ชัน (รุ่นที่ 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 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 จะทำงานเมื่อเมทริกซ์ทดสอบเสร็จสิ้นการทดสอบ

ดู ทริกเกอร์ Firebase Test Lab ในเอกสารประกอบฟังก์ชันคลาวด์สำหรับข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่เรียกใช้ 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 Alerts ในเอกสารประกอบของ 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 Alerts ในเอกสารประกอบของ 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 เผยแพร่การแจ้งเตือน

ดูท ริกเกอร์ Firebase Alerts ในเอกสารประกอบของ 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}