เมื่อสร้างส่วนขยาย คุณจะเขียนตรรกะของส่วนขยายโดยใช้ Cloud Functions ใน
ลักษณะเดียวกับที่คุณเขียนฟังก์ชันที่จะใช้ใน
โปรเจ็กต์ของคุณเองเท่านั้น คุณประกาศฟังก์ชันในไฟล์ extension.yaml และเมื่อผู้ใช้ติดตั้งส่วนขยาย ฟังก์ชันเหล่านี้จะได้รับการติดตั้งใช้งานในโปรเจ็กต์ของผู้ใช้
ดูข้อมูลทั่วไปเกี่ยวกับการใช้ Cloud Functions ได้ในเอกสารประกอบของCloud Functions
รุ่นที่ 1 และรุ่นที่ 2 Cloud Functions
Firebase รองรับทั้ง รุ่นที่ 1 และรุ่นที่ 2 Cloud Functions อย่างไรก็ตาม ปัจจุบัน Firebase Extensions มีข้อจำกัดบางอย่างเกี่ยวกับ Cloud Functions รุ่นที่คุณใช้ได้กับทริกเกอร์บางประเภท ด้วยเหตุนี้ ส่วนขยายหลายรายการจึงมีฟังก์ชันทั้งรุ่นที่ 1 และรุ่นที่ 2
ระบบจะระบุการรองรับฟังก์ชันรุ่นต่างๆ ไว้สำหรับทริกเกอร์แต่ละประเภทด้านล่าง
สิ่งที่ควรพิจารณาเป็นพิเศษ
คำจำกัดความของฟังก์ชันบางอย่างกำหนดให้คุณต้องระบุข้อมูลที่ระบุไว้ในไฟล์
extension.yamlด้วย ตัวอย่างเช่น Cloud Firestore มีเมธอดdocument()ที่ระบุรูปแบบเอกสารที่จะตรวจสอบ และการประกาศที่เกี่ยวข้องในextension.yamlมีช่องresourceที่ระบุรูปแบบเดียวกันในกรณีเหล่านี้ ระบบจะใช้การกำหนดค่าที่ระบุไว้ในไฟล์
extension.yamlและจะละเว้นการกำหนดค่าที่ระบุไว้ในคำจำกัดความของฟังก์ชันโดยทั่วไปแล้ว เราจะระบุค่าที่กำหนดค่าไว้ในคำจำกัดความของฟังก์ชันเสมอเพื่อวัตถุประสงค์ในการจัดทำเอกสาร ตัวอย่างในหน้านี้เป็นไปตามรูปแบบนี้
1st gen SDK มีเมธอด
functions.config()และfunctions:config:setคำสั่งใน CLI ที่คุณใช้เพื่อทำงานกับ ค่าที่มีพารามิเตอร์ในฟังก์ชันรุ่นที่ 1 ได้Cloud Functions เทคนิคนี้เลิกใช้งานแล้วใน Cloud Functions และจะ ไม่ทำงานเลย ในส่วนขยาย แต่ให้ใช้โมดูลfunctions.params(แนะนำ) หรือprocess.envแทน
การใช้ TypeScript
เอกสารประกอบส่วนใหญ่สำหรับการพัฒนาส่วนขยายของคุณเองจะอธิบายเวิร์กโฟลว์ โดยใช้ JavaScript สำหรับ Cloud Functions for Firebase อย่างไรก็ตาม คุณสามารถเขียนฟังก์ชันโดยใช้ TypeScript แทนได้
อันที่จริงแล้ว ส่วนขยาย อย่างเป็นทางการ Firebaseทั้งหมด เขียนด้วย TypeScript คุณสามารถดูส่วนขยายเหล่านั้นเพื่อดูแนวทางปฏิบัติแนะนำบางอย่างในการใช้ TypeScript สำหรับส่วนขยายของคุณ
หากคุณเขียนฟังก์ชันของส่วนขยายใน TypeScript คุณต้องทำ ดังต่อไปนี้ก่อนติดตั้งส่วนขยาย:
คอมไพล์ซอร์สโค้ดของฟังก์ชันของส่วนขยายเป็น JavaScript
คำสั่ง
firebase ext:dev:initช่วยให้คุณเลือก TypeScript สำหรับเขียนฟังก์ชันได้ คำสั่งนี้ จะให้ส่วนขยายที่สมบูรณ์และติดตั้งได้ รวมถึงสคริปต์บิลด์ ที่คุณเรียกใช้ได้ด้วยnpm run buildในไฟล์
package.jsonให้ตรวจสอบว่าได้ชี้ช่องmainไปที่ JavaScript ที่สร้างขึ้นหากคุณกำลังติดตั้งหรืออัปโหลดส่วนขยายจากซอร์สในเครื่อง ให้คอมไพล์ไฟล์ 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(required) |
ความถี่ที่ต้องการให้ฟังก์ชันทำงาน ฟิลด์นี้ยอมรับสตริงที่ใช้ไวยากรณ์ใดไวยากรณ์หนึ่งต่อไปนี้ (ต้องใส่ใน เครื่องหมายคำพูดเดี่ยว)
|
timeZone(optional) |
เขตเวลาที่จะใช้กำหนดเวลา
|
หากต้องการให้ผู้ใช้กำหนดค่ากำหนดเวลาได้เมื่อติดตั้งส่วนขยาย ให้เพิ่มพารามิเตอร์ใหม่ลงในไฟล์ 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 FunctionsCloud 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 ที่ระบุ
ดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์ด้วย Analytics ได้ที่หัวข้อทริกเกอร์ Google Analytics ในเอกสารประกอบของCloud Functionsสำหรับ
คำจำกัดความของฟังก์ชัน (รุ่นที่ 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 จะทำงานเมื่อมีการสร้าง อัปเดต หรือลบเอกสาร
ดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์ด้วย Firestore ได้ที่หัวข้อทริกเกอร์ Cloud Firestore ในเอกสารประกอบCloud Functionsสำหรับ
คำจำกัดความของฟังก์ชัน (รุ่นที่ 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 ได้ที่หัวข้อทริกเกอร์ Pub/Sub ในเอกสารประกอบCloud Functionsสำหรับ
คำจำกัดความของฟังก์ชัน (รุ่นที่ 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 จะทำงานเมื่อมีการสร้าง อัปเดต หรือลบเส้นทางที่ตรงกับรูปแบบที่ระบุ
ดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์ด้วย RTDB ได้ที่หัวข้อทริกเกอร์ Realtime Database ในเอกสารประกอบCloud Functions
คำจำกัดความของฟังก์ชัน (รุ่นที่ 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
ฟังก์ชันที่ทริกเกอร์ด้วย Remote Config จะทำงานเมื่อมีการอัปเดตเทมเพลตพารามิเตอร์ของโปรเจ็กต์
ดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์ด้วย Remote Config ได้ที่หัวข้อทริกเกอร์ 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 จะทำงานเมื่อมีการสร้าง เก็บถาวร หรือลบออบเจ็กต์ หรือเมื่อข้อมูลเมตาของออบเจ็กต์มีการเปลี่ยนแปลง
ดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์ด้วย 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 |
ลบออบเจ็กต์แล้ว |
หากต้องการให้ผู้ใช้กำหนดค่า Bucket ของ Storage ได้เมื่อติดตั้งส่วนขยาย ให้เพิ่มพารามิเตอร์ใหม่ลงในไฟล์ 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 จะทำงานเมื่อเมทริกซ์การทดสอบทำการทดสอบเสร็จสิ้น
ดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์ด้วย Test Lab ได้ที่หัวข้อทริกเกอร์ Firebase Test LabในเอกสารประกอบCloud Functionsสำหรับ
คำจำกัดความของฟังก์ชัน (รุ่นที่ 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)
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.newFatalIssuecrashlytics.newNonfatalIssuecrashlytics.regressioncrashlytics.stabilityDigestcrashlytics.velocitycrashlytics.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)
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)
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.newTesterIosDeviceappDistribution.inAppFeedback
ทริกเกอร์เหตุการณ์ที่กำหนดเอง (Eventarc)
ฟังก์ชันที่ทริกเกอร์ด้วย Eventarc จะทำงานเมื่อมีการเผยแพร่เหตุการณ์ประเภทที่เฉพาะเจาะจงไปยังช่องที่เฉพาะเจาะจง
ดูข้อมูลเกี่ยวกับการเขียนฟังก์ชันที่ทริกเกอร์ด้วย Eventarc ได้ที่หัวข้อสร้างและจัดการทริกเกอร์เหตุการณ์ที่กำหนดเองในเอกสารประกอบCloud Functions
นอกจากนี้ คุณยัง เผยแพร่ เหตุการณ์จากส่วนขยายเพื่อให้ผู้ใช้มีวิธีแทรกตรรกะที่กำหนดเองลงในส่วนขยายได้ด้วย ดูหัวข้อใช้ตรรกะที่กำหนดเองซึ่งนักพัฒนาซอฟต์แวร์เป็นผู้ระบุในส่วนขยาย
คำจำกัดความของฟังก์ชัน (รุ่นที่ 2 เท่านั้น)
import { onCustomEventPublished } from "firebase-functions/v2/eventarc";
export const yourFunctionName = onCustomEventPublished((event) => {
// ...
});
การประกาศทรัพยากร (extension.yaml)
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}