【问题标题】:Switch Firebase Function to Gen-2将 Firebase 功能切换到 Gen-2
【发布时间】:2023-01-04 12:18:42
【问题描述】:

我刚刚看到我们有第二代 Cloud Functions,它看起来很棒:https://cloud.google.com/functions/docs/2nd-gen/overview

但是如何将我的第一代功能切换为第二代功能?我看到我可以create a new function as 2nd gen like this

const functions = require('@google-cloud/functions-framework');

functions.http('helloHttp', (req, res) => {
 res.send(`Hello ${req.query.name || req.body.name || 'World'}!`);
});

但是旧功能呢?有没有办法,或者我将不得不一个一个地删除并重新创建它们?

【问题讨论】:

  • 我不清楚你在问什么。执行源代码级转换的一些工具或指令集?此外,您在标题中提到了 Firebase,但您链接到的所有内容都是针对 Cloud 的。 Firebase 有一组不同的工具来处理相同的底层产品(云功能)。
  • @DougStevenson 啊你的意思是我们无法从 firebase 访问第二代函数?
  • 您在文档中看到的就是可用的。
  • 今天,我将相同的功能部署到两个不同的 Firebase 项目中,其中一个项目获得了“第一代”徽标,而另一个获得了“第二代”徽标,当天晚些时候“第二代”又变回了“第一代”。我指的是如果您转到 console.firebase.google.com > 单击项目 > 功能 > 单击 3 个垂直点 > 详细使用情况统计信息(带您到 Cloud Functions UI)时看到的蓝色药丸形徽标。
  • 我注意到 URL 上有一个名为 env=gen1 的参数,当我手动更改为 env=gen2 时,它会将蓝色药丸形徽标从“第一代”更改为“第二代”。有人知道发生了什么事吗?

标签: node.js firebase google-cloud-functions


【解决方案1】:

Cloud Function Gen2 仅适用于产品 Cloud Functions,不适用于 Firebase 功能。
Firebase 函数使用库firebase-functions,而 Cloud Functions 使用@google-cloud/functions-framework
在 Google Cloud 控制台中,这两个函数看起来是一样的,但是如果您尝试在 Cloud Function gen1 上部署 Cloud Function gen2,您将收到以下错误:

Failed to create function, function projects/[PROJECT_NUMBER]/locations/[LOCATION]/functions/[FUNCTION_NAME] already exists under 'Gen 1' environment. Please delete conflicting function first or deploy the function with a different name.

您需要从 Firebase 函数完全迁移到全新创建的 Cloud Functions gen2。

【讨论】:

  • 你知道有没有计划改变这个?我们真的可以使用第 2 代功能。
  • @NickJ,不确定它何时切换,但看起来 Gen2 现在支持(预览中)Firebase 函数:firebase.google.com/docs/functions/beta
【解决方案2】:

截至 2022 年 8 月 9 日 Cloud Functions 2nd Gen become Generally Available,情况可能发生了变化,因此我将记录在 TypeScript 项目中对我有用的内容。

第一代

客户

“火力基地”:“9.9.3”

import { httpsCallable } from "firebase/functions";
import { AddTwoNumbersInputParams, AddTwoNumbersInputResult } from "./util/shared/my-types";

// ...

const addTwoNumbersFuncCallable = httpsCallable<AddTwoNumbersInputParams, AddTwoNumbersInputResult>(
    firebaseFunctions,
    "addTwoNumbersFunc",
);

const result = await addTwoNumbersFuncCallable({
    firstNum: 3,
    secondNum: 5
});

console.log("Result", result.data);

服务器

“firebase 函数”:“^3.21.0”

import * as functions from "firebase-functions";
import { AddTwoNumbersInputParams, AddTwoNumbersInputResult } from "./util/shared/my-types";

// ...

export const addTwoNumbersFunc = functions.https.runWith({ memory: "1GB" }).onCall((params: AddTwoNumbersInputParams, ctx): AddTwoNumbersInputResult => {
    
    if (!ctx.auth) {
        throw new functions.https.HttpsError("unauthenticated", "You must be logged in to call server-side functions");
    }


    return { result: params.firstNum + params.secondNum };
}

共享

为了在我的客户端和服务器代码之间共享 TypeScript 接口 AddTwoNumbersInputParams 和 AddTwoNumbersInputResult,我创建了一个指向目录 util/shared 的符号链接,该目录在名为 my-types.ts 的文件中包含以下定义:

export interface AddTwoNumbersInputParams {
    firstNum: number
    secondNum: number
}

export interface AddTwoNumbersInputResult {
    result: number
}

第二代

客户

“火力基地”:“9.9.3”

import { httpsCallableFromURL } from "firebase/functions"; // see note1, note2, note3 below 

// ...

const addTwoNumbersFuncCallable = httpsCallableFromURL<AddTwoNumbersInputParams, AddTwoNumbersInputResult>(
    firebaseFunctions,
    "https://addtwonumbersfunc-fri67ycgta-uc.a.run.app",  // see note1, note2, note3 below 
);

const result = await addTwoNumbersFuncCallable({
    firstNum: 3,
    secondNum: 5
});

console.log("Result", result.data);

注意1:callable documentationhttp-events documentation都说firebase deploy命令应该输出URL,但我没有看到。我通过访问这里获得了它:

  1. Firebase Console
  2. 单击您的项目
  3. 单击“功能”(在左侧的目录中,如果没有看到,请单击“所有产品”,然后单击“功能”)
  4. 在触发器列中复制函数的 URL;它应该是https://&lt;lowercase_func_name&gt;-&lt;random-hash&gt;-&lt;region&gt;.a.run.app的形式

    注 2:起初我担心第二代函数会在我的持续集成管道中引入手动步骤,因为它们现在输出一个 URL(或者 it said)。有几个 Firebase 项目代表不同的阶段以促进生产 (Google's recommendation),我想我会遇到新的麻烦来复制和粘贴第二代功能的每个部署的 URL。幸运的是,它并没有我想象的那么糟糕,因为"the URL remains stable after deployment"。因此,我只需部署一次即可获取 URL,将其插入我的客户端代码,此后每次部署都保持不变。也就是说,它仍然是我的每个 Firebase 项目的不同 URL。所以我将不得不做更多的工作来促进生产。但也许他们会解决这个问题,因为他们说了"In a future release, 2nd gen function URLs will be updated to be both stable and deterministic."

    注 3:我发现 URL 的东西太麻烦了,所以我尝试不使用它并在 Firebase Functions 模拟器上取得了成功,但在真正的 Firebase 项目上却失败了。使用模拟器,我几乎可以继续使用接受函数名称的 httpsCallable() 函数,而不是需要 URL 的 httpsCallableFromURL() 函数。它适用于模拟器,但不适用于真正的 Firebase 项目。

    服务器

    “firebase 函数”:“^3.21.0”

    import * as functionsV2 from "firebase-functions/v2";
    import {CallableRequest} from "firebase-functions/lib/common/providers/https";
    import {HttpsOptions} from "firebase-functions/lib/v2/providers/https";
    
    // ... 
    
    const httpsOptions: HttpsOptions = {
        memory: "16GiB"  // see note4 below
    };
    
    
    // see note5 below
    export const addtwonumbersfunc = functionsV2.https.onCall(httpsOptions, (request: CallableRequest<AddTwoNumbersInputParams>): AddTwoNumbersInputResult => {
        if (!request.auth) {
            throw new functionsV2.https.HttpsError("unauthenticated", "You must be logged in to call server-side functions");
        }
    
        return { result: request.data.firstNum + request.data.secondNum };
    });
    
    
    

    注意4:用于设置内存(等)的runWith() 语法似乎已更改为接受HttpsOptionsHttpsOptions object that you pass to the onCall(),例如memory。第 2 代令人兴奋的事情之一是它提供了 higher memory allocations than 1st Gen,因此我已经在此处进行了演示,但从“1GB”增加到“16GiB”(另请注意从“GB”到“GiB”的变化)。

    注5:"Function names are restricted to lowercase letters, numbers, and dashes."但希望很快就会出现"Support for using capital letters in function names."

    共享

    无需更改

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-03
    • 2017-11-12
    • 2021-11-15
    • 1970-01-01
    相关资源
    最近更新 更多