Cloud Functions 和 Firebase Functions 有什么区别?

What is the difference between Cloud Functions and Firebase Functions?

Cloud Functions and Firebase Functions (or "Cloud Functions for Firebase") 两者看起来一样。请描述每个的用例。

两者都使用 HTTP 函数。

云函数中:

exports.helloHttp = function helloHttp (req, res) {
  res.send(`Hello ${req.body.name || 'World'}!`);
};

并且在 Firebase 函数中:

exports.helloWorld = functions.https.onRequest((request, response) => {
  response.send("Hello from Firebase!");
});

这些有什么区别?

没有名为 Firebase Functions 的产品。

三个独立的东西:

  1. Google Cloud Functions,它允许您在 Google 的基础结构中 运行 代码片段以响应事件。
  2. Cloud Functions for Firebase,它会根据 Firebase 中的事件(例如数据库或文件写入、用户创建等)触发 Google Cloud Functions
  3. Firebase SDK for Cloud Functions,其中包含一个库(容易混淆地称为 firebase-functions),您可以在 Functions 代码中使用它来访问 Firebase 数据(例如写入数据库的数据快照)

因此 Firebase 围绕 Google Cloud Functions 提供了一个(相对较薄的)包装器,以使后者产品更易于使用并将其与 Firebase 集成。从这个意义上说,它类似于 Firebase 如何将 Google Cloud Storage 集成到 "Cloud Storage for Firebase"(以前称为 Firebase Storage)中。

如果您使用 Google 不带 Firebase 的云平台,那么您应该使用纯 Google Cloud Functions。如果您使用的是 Firebase 或者您是对 Cloud Functions 感兴趣的移动开发人员,则应该使用 Cloud Functions for Firebase.

Google 云平台 GCP 有一篇文章解决了这个问题,Google Cloud Functions and Firebase

Google Cloud Functions and Firebase

Google Cloud Functions is Google's serverless compute solution for creating event-driven applications. It is a joint product between the Google Cloud Platform team and the Firebase team.

For Google Cloud Platform developers, Cloud Functions serve as a connective layer allowing you to weave logic between Google Cloud Platform (GCP) services by listening for and responding to events.

For Firebase developers, Cloud Functions for Firebase provides a way to extend the behavior of Firebase and integrate Firebase features through the addition of server-side code.

Both solutions provide fast and reliable execution of functions in a fully managed environment where there's no need for you to worry about managing any servers or provisioning any infrastructure.

...

Cloud Functions for Firebase is optimized for Firebase developers:

  • Firebase SDK to configure your functions through code
  • Integrated with Firebase Console and Firebase CLI
  • The same triggers as Google Cloud Functions, plus Firebase Realtime Database, Firebase Authentication, and Firebase Analytics triggers

还有一个区别:Firebase Functions 只能在 JS 或 Node.JS 中实现,而 Cloud Functions 也允许使用 Python 和 Go。

如果您使用的是 Spark 计划,则它们的定价方式也略有不同。检查一下 https://firebase.google.com/pricing vs. https://cloud.google.com/functions/pricing 如果您使用的是 Blaze 计划,则价格相同。

我碰巧在我的 Firebase 项目中使用了两者。

官方Google视频描述差异:GCP vs. Firebase - Functions & Firestore

  1. Firebase 将为您提供将函数包装到可调用函数中的功能,这些函数可通过 firebase SDK 调用
  2. 语言支持,GCP也支持Go,Python和java
  3. GCP 可以通过控制台或 CLI 部署,但 firebase 只能通过 CLI 运行