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 进行处理,而不是在用户设备上运行,从而提高应用的响应速度。例如,您可以编写一个函数来监听将图像上传到 Cloud Storage 的事件,将图像下载到运行该函数的实例,进行修改并将其上传回 Cloud Storage 中。修改操作可以包括使用 sharp 或 Pillow 等工具调整图像大小、剪裁或转换图像。

- 函数在图像文件上传到 Cloud Storage 时触发。
- 由函数下载图像并创建其缩略图版本。
- 函数将缩略图位置写入数据库,便于客户端应用查找和使用。
- 函数将缩略图上传回 Cloud Storage 中的新位置。
- 由应用下载缩略图链接。
如需查看图像处理示例的演示,请参阅处理 Cloud Storage 事件指南。
其他 Firebase 云端批量作业示例
与第三方服务和 API 集成
Cloud Functions 可以通过调用和提供网络 API 来帮助您的应用更好地与其他服务配合工作。例如,一款开发协作应用可以将 GitHub 提交发布到工作组聊天室。

- 一个用户向一个 GitHub 代码库推送提交。
- HTTPS 函数通过 GitHub webhook API 触发。
- 函数向团队 Slack 频道发送提交通知。
其他与第三方服务和 API 集成的方式
- 使用 Google Cloud Vision API 分析和标记上传的图像。
- 使用 Google 翻译来翻译消息。
- 使用自定义身份验证让用户登录。
- 在写入 Realtime Database 时向 webhook 发送请求。
- 启用对 Realtime Database 元素的全文搜索。
- 处理用户付款。
- 创建电话和短信自动回复。
- 使用 Google 助理创建聊天机器人。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-17。
[null,null,["最后更新时间 (UTC):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."]]