使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Cloud Functions for Firebase
plat_ios
plat_android
plat_web
plat_flutter
plat_cpp
plat_unity
Cloud Functions for Firebase 是一种无服务器框架,您可以利用它自动运行后端代码来响应后台事件、HTTPS 请求、
Admin SDK 或
Cloud Scheduler 作业触发的事件。您的 JavaScript、TypeScript 或 Python 代码存储在 Google Cloud 基础架构中,并在托管式环境中运行。您无需管理和扩缩自己的服务器。
已经在 Google Cloud 中使用 Cloud Functions?详细了解如何搭配 Firebase 使用 Cloud Functions。
使用入门
使用场景
主要功能
集成 Firebase 功能并将 Firebase 与 Google Cloud 相关联 |
您编写的函数可以响应各种 Firebase 和 Google Cloud 功能(从 Firebase Authentication 触发器到 Cloud Storage 触发器)生成的事件。
将 Admin SDK 与 Cloud Functions 配合使用以与各种 Firebase 功能进行集成,并通过编写自己的 Webhook 来与第三方服务集成。
Cloud Functions 能最大限度减少样板代码,让您更轻松地在函数中使用 Firebase 和 Google Cloud。 |
零维护 |
只需在命令行中输入一个命令即可将您的 JavaScript、TypeScript 或 Python 代码部署到我们的服务器。此后,Firebase 会根据您用户的使用模式自动纵向扩容计算资源。您永远不必担心以下这些问题:凭据、服务器配置、预配新服务器或停用旧服务器。 |
确保逻辑的私密性和安全性 |
在很多情况下,开发者喜欢在服务器上控制应用逻辑,以避免客户端出现篡改的情况。此外,有时候您可能并不希望代码被逆向工程。
Cloud Functions 与客户端完全隔离,因此您可以确信它是私密且安全的,且始终按照您的要求执行操作。 |
工作原理
在您编写并部署函数后,Google 的服务器会立即开始管理函数。您可以使用 HTTP 请求、Admin SDK 或预定作业直接触发函数,或者对于后台函数,Google 的服务器会监听事件并在函数被触发时运行函数。
Google 会随着负载的增加或减少而做出响应,迅速调节运行函数所需的虚拟服务器实例的数量。每个函数都拥有自己的配置,会在自己的环境中单独运行。
后台函数的生命周期
- 您需要为新函数编写代码,选择事件提供程序(如 Cloud Firestore),并定义在哪些条件下执行函数。
- 当您部署函数时:
- Firebase CLI 会创建函数代码的
.zip
归档文件,然后将其上传到 Cloud Storage 存储桶(以 gcf-sources
为前缀),接着 Cloud Functions 会在您的项目中创建一个 Artifact Registry 代码库(名为 gcf-artifacts
)。
- Cloud Build 会检索函数代码并构建函数源。您可以在 Google Cloud 控制台中查看 Cloud Build 日志。
- 所构建函数代码的容器映像会上传到项目中的私有 Artifact Registry 代码库(名为
gcf-artifacts
),新的函数将会发布。
- 当事件提供程序生成与函数条件匹配的事件时,系统就会调用代码。 该函数附加了一个服务账号,可在 Firebase Admin SDK 的帮助下用于访问其他 Firebase 服务。
- 如果函数忙于处理许多事件,Google 会创建更多实例以更快地处理工作。如果函数空闲,则实例会被清除。
- 当您通过部署更新后的代码来更新函数时,较旧版本的实例以及 Artifact Registry 中的构建工件都会被清除并替换为新实例。
- 当您删除该函数时,所有实例和 zip 归档文件以及 Artifact Registry 中的相关构建工件都会被清除。函数与事件提供程序之间的关联也将移除。
除了使用后台函数侦听事件之外,您还可以使用 HTTP 请求或客户端调用直接调用函数。 您还可以通过 Admin SDK 按固定时间表触发函数或将任务函数加入队列。
实现流程
|
设置 Cloud Functions |
安装 Firebase CLI 并在 Firebase 项目中初始化 Cloud Functions。 |
|
编写函数 |
编写 JavaScript 代码、TypeScript 代码或 Python 代码以处理来自 Firebase 服务、Google Cloud 服务或其他事件提供程序的事件。 |
|
测试函数 |
使用本地模拟器测试您的函数。 |
|
部署和监控 |
将您的项目升级到随用随付 Blaze 定价方案,并使用 Firebase CLI 部署您的函数。然后,您可以使用 Google Cloud 控制台查看和搜索日志。 |
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-17。
[null,null,["最后更新时间 (UTC):2025-08-17。"],[],[],null,["Cloud Functions for Firebase \nplat_ios plat_android plat_web plat_flutter plat_cpp plat_unity \nCloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by background events, HTTPS requests, the Admin SDK, or Cloud Scheduler jobs. Your JavaScript, TypeScript or Python code is stored on Google Cloud infrastructure and runs in a managed environment. There's no need to manage and scale your own servers.\n\n\u003cbr /\u003e\n\nAlready using Cloud Functions in Google Cloud?\n[Learn more](/docs/functions/functions-and-firebase) about how Firebase fits\ninto the picture.\n\n[Get started](/docs/functions/get-started)\n[Use cases](/docs/functions/use-cases)\n\nKey capabilities\n\n|----------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Integrates Firebase features and connects Firebase with Google Cloud | The functions you write can respond to events generated by various Firebase and Google Cloud features, from [Firebase Authentication triggers](/docs/functions/auth-events) to [Cloud Storage Triggers](/docs/functions/gcp-storage-events). Integrate across Firebase features using the [Admin SDK](/docs/admin/setup) together with Cloud Functions, and integrate with third-party services by writing your own webhooks. Cloud Functions minimizes boilerplate code, making it easier to use Firebase and Google Cloud inside your function. |\n| Zero maintenance | Deploy your JavaScript, TypeScript, or Python code to our servers with one command from the command line. After that, Firebase automatically scales up computing resources to match the usage patterns of your users. You never worry about credentials, server configuration, provisioning new servers, or decommissioning old ones. |\n| Keeps your logic private and secure | In many cases, developers prefer to control application logic on the server to avoid tampering on the client side. Also, sometimes it's not desirable to allow that code to be reverse engineered. Cloud Functions is fully insulated from the client, so you can be sure it is private and always does exactly what you want. |\n\nHow does it work?\n\nAfter you write and deploy a function, Google's servers begin to manage the\nfunction immediately. You can fire the function directly with an HTTP request,\nthe Admin SDK, or a scheduled job, or, in the case of background functions,\nGoogle's servers listen for events and run the function when it is\ntriggered.\n\nAs the load increases or\ndecreases, Google responds by rapidly scaling the number of virtual server\ninstances needed to run your function. Each function runs in isolation, in its\nown environment with its own configuration.\n\nLifecycle of a background function\n\n1. You write code for a new function, selecting an event provider (such as Cloud Firestore), and defining the conditions under which the function should execute.\n2. When you deploy your function:\n 1. The Firebase CLI creates a `.zip` archive of the function code, which is then uploaded to a Cloud Storage bucket (prefixed with `gcf-sources`) before Cloud Functions creates an Artifact Registry repository (named `gcf-artifacts`) in your project.\n 2. Cloud Build retrieves the function code and builds the function source. You can view Cloud Build logs in the [Google Cloud console](//console.cloud.google.com/logs/query;query%3Dresource.type%253D%2522build%2522&sa=D&ust=1597179510979000&usg=AFQjCNG2BmFrgWjGoP83WiMrB2TLLXpBEQ).\n 3. The container image for the built functions code is uploaded to a private Artifact Registry repository in your project (named `gcf-artifacts`), and your new function is rolled out.\n3. When the event provider generates an event that matches the function's conditions, the code is invoked. The function has a service account attached to it that can be used to access other Firebase services with the help of the Firebase Admin SDK.\n4. If the function is busy handling many events, Google creates more instances to handle work faster. If the function is idle, instances are cleaned up.\n5. When you update the function by deploying updated code, instances for older versions are cleaned up along with build artifacts in Artifact Registry, and replaced by new instances.\n6. When you delete the function, all instances and zip archives are cleaned up, along with related build artifacts in Artifact Registry. The connection between the function and the event provider is removed.\n\nIn addition to listening for events with a background function, you can call\nfunctions directly with an HTTP request or a\n[call from the client](/docs/functions/callable). You can also trigger functions\non a fixed [schedule](/docs/functions/schedule-functions) or\n[enqueue task functions](/docs/functions/task-functions) via the Admin SDK.\n\nImplementation path\n\n|---|------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| | Set up Cloud Functions | Install the Firebase CLI and initialize Cloud Functions in your Firebase project. |\n| | Write functions | Write JavaScript code, TypeScript code, or Python code to handle events from Firebase services, Google Cloud services, or other event providers. |\n| | Test functions | Use the [local emulator](/docs/functions/local-emulator) to test your functions. |\n| | Deploy and monitor | Upgrade your project to the [pay-as-you-go Blaze pricing plan](/pricing) and deploy your functions using the Firebase CLI. You can then use the [Google Cloud console](//console.cloud.google.com/functions/list) to view and search through your logs. |\n\nNext steps\n\n- [Get started](/docs/functions/get-started) setting up, creating, and deploying functions.\n- Learn more about [what you can do with functions](/docs/functions/use-cases).\n- Try the [Cloud Functions codelab](https://codelabs.developers.google.com/codelabs/firebase-cloud-functions/#0)."]]