You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Firebase第二代函数部署到多个区域?

Firebase v2 函数部署到多个区域的解决方法

在Firebase第一代函数中,部署到多个区域操作简单,但在第二代函数中,直接沿用旧写法会报错。以下是正确的实现方式:

原单区域部署代码

setGlobalOptions({ region: "northamerica-northeast1" });
exports.myFunction = onRequest({
    minInstances: 2,
    memory: "1GiB",
    concurrency: 80,
    cors: true
},
    async (req, res) => {
        // 函数逻辑
    });

错误尝试及报错

尝试直接在setGlobalOptions中添加多个区域字符串,写法如下:

setGlobalOptions({ region: "northamerica-northeast1", "northamerica-northeast2" }); 

部署时触发如下错误:

HTTP Error: 403, Permission denied on 'locations/northamerica-northeast1, northamerica-northeast2' (or it may not exist).

正确实现方式

Firebase v2 函数中,多区域部署需要将region设置为数组格式,而非多个独立参数。

  • 全局设置多区域(所有函数共用)
    修改setGlobalOptions的配置,把region值改成区域字符串数组:
setGlobalOptions({ 
  region: ["northamerica-northeast1", "northamerica-northeast2"] 
});

exports.myFunction = onRequest({
    minInstances: 2,
    memory: "1GiB",
    concurrency: 80,
    cors: true
},
    async (req, res) => {
        // 函数逻辑
    });
  • 单个函数单独设置多区域(覆盖全局配置)
    如果仅需给特定函数设置多区域,可在函数配置中直接指定:
// 全局默认单区域
setGlobalOptions({ region: "us-central1" });

// 该函数单独部署到两个区域
exports.myFunction = onRequest({
    region: ["northamerica-northeast1", "northamerica-northeast2"],
    minInstances: 2,
    memory: "1GiB",
    concurrency: 80,
    cors: true
},
    async (req, res) => {
        // 函数逻辑
    });

内容的提问来源于stack exchange,提问作者FlatDog

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 09:05:19