Cloud Functions 提供哪些功能?
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Cloud Functions可讓開發人員存取 Firebase 和Google Cloud事件,以及可擴充的運算能力,以便因應這些事件執行程式碼。雖然 Firebase 應用程式預期會以獨特方式使用 Cloud Functions,以滿足獨特需求,但一般用途可能屬於下列領域:
請先查看您感興趣的類別,瞭解相關的使用案例和範例,然後前往「開始使用」教學課程,或參閱驗證事件、數據分析事件等特定操作指南。
在發生有趣事件時通知使用者
開發人員可以運用 Cloud Functions 讓使用者持續參與,並隨時掌握應用程式的相關資訊。舉例來說,假設應用程式允許使用者追蹤彼此的活動,每當使用者追蹤其他使用者時,Realtime Database 中就會發生寫入作業。接著,這項寫入事件可能會觸發函式,建立 Firebase Cloud Messaging (FCM) 通知,讓適當的使用者知道自己獲得了新追蹤者。

- 函式會在寫入 Realtime Database 路徑時觸發,該路徑用於儲存追蹤者。
- 這個函式會撰寫訊息,並透過 FCM 傳送。
- FCM 會將通知訊息傳送至使用者的裝置。
如要查看可運作的程式碼,請參閱 GitHub 中的程式碼範例:
其他有趣的通知用途
- 傳送確認電子郵件給訂閱電子報的使用者。
- 使用者完成註冊時,傳送歡迎電子郵件。
- 使用者建立新帳戶時,傳送簡訊確認訊息。
執行資料庫清除和維護作業
透過 Cloud Functions 資料庫事件處理,您可以根據使用者行為修改 Realtime Database 或 Cloud Firestore,讓系統維持在所需狀態。舉例來說,您可以監控寫入事件,並變更使用者訊息中特定字串的格式 (例如全部改為大寫)。運作方式如下:

- 函式的資料庫事件處理常式會監聽特定路徑的寫入事件,並擷取包含訊息文字的事件資料。
- 這項函式會處理文字,將字串變更為大寫。
- 函式會將更新後的文字寫回資料庫。
如要查看可運作的程式碼,請參閱 GitHub 中的程式碼範例:
其他資料庫清除和維護用途
- 從 Realtime Database 中清除已刪除使用者的內容。
- 限制 Firebase 資料庫中的子節點數量。
- 追蹤 Realtime Database 清單中的元素數量。
- 將資料從 Realtime Database 複製到 Google Cloud BigQuery。
- 將文字轉換成表情符號。
- 管理資料庫記錄的計算中繼資料。
在雲端執行耗用大量資源的工作,而非在應用程式中執行
您可以利用 Cloud Functions 將耗用大量資源的工作 (大量 CPU 或網路) 卸載至 Google 雲端,不必在使用者裝置上執行,進而提升應用程式的回應速度。舉例來說,您可以編寫函式,監聽上傳至 Cloud Storage 的圖片、將圖片下載至執行函式的執行個體、修改圖片,然後將圖片上傳回 Cloud Storage。修改內容包括使用 sharp 或 Pillow 等工具調整大小、裁剪或轉換圖片。

- 圖片檔案上傳至 Cloud Storage 時,系統會觸發函式。
- 這個函式會下載圖片,並建立縮圖版本。
- 這個函式會將縮圖位置寫入資料庫,以便用戶端應用程式尋找及使用。
- 這個函式會將縮圖上傳回 Cloud Storage 的新位置。
- 應用程式會下載縮圖連結。
如需圖片處理範例的逐步說明,請參閱處理 Cloud Storage 事件指南。
Firebase 雲端中的其他批次工作範例
與第三方服務和 API 整合
Cloud Functions 可呼叫及公開 Web API,協助應用程式與其他服務搭配運作。舉例來說,用於開發協作的應用程式可以將 GitHub 提交內容發布至工作群組聊天室。

- 使用者將修訂版本推送至 GitHub 存放區。
- HTTPS 函式會透過 GitHub Webhook API 觸發。
- 這個函式會將提交通知傳送至團隊的 Slack 頻道。
與第三方服務和 API 整合的其他方式
- 使用 Google Cloud Vision API 分析及標記上傳的圖片。
- 使用 Google 翻譯服務翻譯訊息。
- 使用自訂驗證登入使用者。
- 在 Realtime Database 寫入時,向 Webhook 發送要求。
- 啟用 Realtime Database 元素上的全文搜尋功能。
- 處理使用者的款項。
- 建立電話和簡訊的自動回覆。
- 使用 Google 助理建立聊天機器人。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-17 (世界標準時間)。
[null,null,["上次更新時間:2025-08-17 (世界標準時間)。"],[],[],null,["\u003cbr /\u003e\n\nCloud Functions gives developers access to Firebase and Google Cloud\nevents, along with\nscalable computing power to run code in response to those events. While it's\nexpected that Firebase apps will use Cloud Functions in unique ways to meet\ntheir unique requirements, typical use cases might fall into these areas:\n\n- [Notify users when something interesting happens](#notify_users_when_something_interesting_happens).\n- [Perform database sanitization and maintenance](#perform_database_sanitization_and_maintenance).\n- [Execute intensive tasks in the cloud instead of in your app](#execute_intensive_tasks_in_the_cloud_instead_of_in_your_app).\n- [Integrate with third-party services and APIs](#integrate_with_third-party_services_and_apis).\n\nReview the use cases and examples for each category that interests\nyou, and then proceed to our [Get Started](/docs/functions/get-started) tutorial\nor to specific how-to guides\nfor [authentication events](/docs/functions/auth-events),\n[analytics events](/docs/functions/analytics-events), and more.\n\nNotify users when something interesting happens\n\nDevelopers can use Cloud Functions to keep users engaged and up to date\nwith relevant information about an app. Consider, for example, an app that\nallows users to follow one another's activities in the app. Each time a user\nadds themselves as a follower of another user, a write occurs in the\nRealtime Database. Then this write event could trigger a function\nto create Firebase Cloud Messaging (FCM) notifications to let the appropriate\nusers know that they have gained new followers.\n\n1. The function triggers on writes to the Realtime Database path where followers are stored.\n2. The function composes a message to send via .\n3. sends the notification message to the user's device.\n\nTo review working code, see the sample code in GitHub:\n\n- Node.js: [fcm-notifications](https://github.com/firebase/functions-samples/tree/main/Node/fcm-notifications)\n- Python: [fcm-notifications](https://github.com/firebase/functions-samples/tree/main/Python/fcm-notifications)\n\nOther interesting notification use cases\n\n- Send confirmation emails to users subscribing to a newsletter.\n- Send a welcome email when a user completes signup.\n- Send an SMS confirmation when a user creates a new account.\n\nPerform database sanitization and maintenance\n\nWith Cloud Functions database event handling, you can modify Realtime Database or\nCloud Firestore in response to user behavior, keeping the system in your desired\nstate. For example, you could monitor write events and change the format\n(for example, change to all uppercase) of certain strings in users' messages.\nHere's how that could work:\n\n1. The function's database event handler listens for write events on a specific path, and retrieves event data containing the text of a messages.\n2. The function processes the text to change strings to uppercase.\n3. The function writes the updated text back to the database.\n\nTo review working code, see the sample code in GitHub:\n\n- Node.js: [uppercase-rtdb](https://github.com/firebase/functions-samples/tree/main/Node/quickstarts/uppercase-rtdb)\n- Python: [uppercase-rtdb](https://github.com/firebase/functions-samples/tree/main/Python/quickstarts/uppercase-rtdb)\n\nOther database sanitization and maintenance use cases\n\n- Purge a deleted user's content from Realtime Database.\n- Limit the number of child nodes in a Firebase database.\n- Track the number of elements in a Realtime Database list.\n- Copy data from Realtime Database to Google Cloud BigQuery.\n- Convert text to emoji.\n- Manage computed metadata for database records.\n\nExecute intensive tasks in the cloud instead of in your app\n\nYou can take advantage of Cloud Functions to offload to the Google\ncloud resource-intensive work (heavy CPU or networking) instead of running it\non a user's device, improving the responsiveness of your app.\nFor instance, you could write a function\nto listen for image uploads to Cloud Storage, download the image to the instance\nrunning the function, modify it, and upload it back to Cloud Storage. Your\nmodifications could include resizing, cropping, or converting images with tools\nlike [sharp](https://www.npmjs.com/package/sharp) or\n[Pillow](https://pillow.readthedocs.io/en/stable/).\n\n1. A function triggers when an image file is uploaded to Cloud Storage.\n2. The function downloads the image and creates a thumbnail version of it.\n3. The function writes that thumbnail location to the database, so a client app can find and use it.\n4. The function uploads the thumbnail back to Cloud Storage in a new location.\n5. The app downloads the thumbnail link.\n\nFor a walkthrough of an image processing example, see the guide to\n[handling Cloud Storage events](https://firebase.google.com/docs/functions/gcp-storage-events?gen=2nd).\n\nOther examples of batch jobs in the Firebase cloud\n\n- Periodically delete unused Firebase accounts [Node.js](https://github.com/firebase/functions-samples/tree/main/Node/delete-unused-accounts-cron) \\| [Python](https://github.com/firebase/functions-samples/tree/main/Python/delete-unused-accounts-cron).\n- Automatically back up uploaded images [Node.js](https://github.com/firebase/functions-samples/tree/main/Node/taskqueues-backup-images) \\| [Python](https://github.com/firebase/functions-samples/tree/main/Python/taskqueues-backup-images).\n- Send bulk email to users.\n- Aggregate and summarize data periodically.\n- Process a queue of pending work.\n\nIntegrate with third-party services and APIs\n\nCloud Functions can help your app work better with other services by\ncalling and exposing web APIs. For instance, an app used for collaboration on\ndevelopment could post GitHub commits to a workgroup chat room.\n\n1. A user pushes commits to a GitHub repo.\n2. An HTTPS function triggers via the [GitHub webhook API](https://developer.github.com/webhooks/).\n3. The function sends a notification of the commit to a team Slack channel.\n\nOther ways to integrate with third-party services and APIs\n\n- Use Google [Cloud Vision API](https://cloud.google.com/vision/) to analyze and tag uploaded images.\n- Translate messages using Google Translate.\n- Use [custom auth](https://firebase.google.com/docs/auth/where-to-start#custom-auth) to sign in users.\n- Send a request to a webhook on Realtime Database writes.\n- Enable full-text search on Realtime Database elements.\n- Process payments from users.\n- Create auto-responses to phone calls and SMS messages.\n- Create a chatbot using Google Assistant."]]