firebase部署到自定义区域(eu-central1)

LOs*_*KeY 27 firebase google-cloud-functions

有没有办法指定将部署我的firebase功能的区域/区域.

实际上我没有在文档中找到任何关于它的内容,我的功能总是部署到我们 - central1但我希望在eu-central1上有...

是否可以在Firebase - Config - File中设置它?

{
  "database": {
    "rules": "database.rules.json"
  },
  "hosting": {
    "public": "public",
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我也看了一下cli选项,但我没有找到任何东西.

Firebase项目本身已正确设置为欧洲区域oO

提前致谢

Fra*_*len 26

这里有一个firebaser

更新(2018-07-25):

现在,您可以在Firebase中指定云功能的区域,在代码中指定该区域并部署更改.例如:

exports.myStorageFunction = functions
    .region('europe-west1')
    .storage
    .object()
    .onFinalize((object) => {
      // ...
    });
Run Code Online (Sandbox Code Playgroud)

有关完整详细信息,请参阅有关云功能位置Firebase文档(从中获取上述代码段)并修改已部署功能的区域.

  • @FrankvanPuffelen对于那个甜蜜的区域间延迟.在使用Google云服务构建复杂设置时,如果数据存储和计算位于同一位置,事情会更快.所以重申一下这个问题,将来有可能吗? (4认同)
  • 任何消息,当我们可以期望具有更改firebase功能区域的功能时? (2认同)
  • 有没有关于这个问题的更新?我看到官方文档提到 Google Cloud Function 确实支持选择位置 https://cloud.google.com/functions/docs/locations 。但是,我仍然没有弄清楚如何通过 CLI 或 firebase.json 选择区域 (2认同)

Moe*_*Moe 21

V2 Firebase 功能的解决方案:

更新于 2024 年 2 月

使用新的 V2 语法,添加 .region 不起作用,并且在开头添加 {region: ''} 对象也不适用于 onDocument 事件。

更新 V2 firebase 函数区域的解决方法是更新全局选项以更改区域,这也会自动将任何函数默认为您指定的区域:

const { setGlobalOptions } = require("firebase-functions/v2");

setGlobalOptions({ region: 'europe-west1' });
Run Code Online (Sandbox Code Playgroud)

此外,如果您想编辑任何其他选项,它将始终默认为您在 setGlobalOptions() 中定义的内容

const { setGlobalOptions } = require("firebase-functions/v2");

setGlobalOptions({
    maxInstances: 10,
    region: "europe-west1",
    timeoutSeconds: 60,
    memory: "2GiB",
});
Run Code Online (Sandbox Code Playgroud)

  • 这是最干净的解决方案!谢谢。 (2认同)

Jac*_*ckl 11

来自docs:https://firebase.google.com/docs/functions/locations

现在可在以下地区使用:

  • us-central1(爱荷华州)
  • us-east1(南卡罗来纳州)
  • europe-west1(比利时)
  • asia-northeast1(东京)

改变地区的最佳实践

// before
const functions = require('firebase-functions');

exports.webhook = functions
    .https.onRequest((req, res) => {
            res.send("Hello");
    });

// after
const functions = require('firebase-functions');

exports.webhookEurope = functions
    .region('europe-west1')
    .https.onRequest((req, res) => {
            res.send("Hello");
    });
Run Code Online (Sandbox Code Playgroud)

  • 顺便说一句 因此理想的设置是:比利时(EU1)中的云功能和法兰克福(EU3)中的数据中心?我想知道为什么它们不在同一数据中心中提供功能和数据库? (2认同)

han*_*luc 6

要为您的所有功能使用相同的自定义区域,您可以执行以下操作。

import * as functions from 'firebase-functions';

const regionalFunctions = functions.region('europe-west1');

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

export const helloWorld2 = regionalFunctions.https.onRequest((request, response) => {
 response.send("Hello from Firebase 2!");
});
Run Code Online (Sandbox Code Playgroud)