Firebase单个HTTPS Callable云函数如何提升超时时间至540s上限
单HTTPS Callable云函数的超时配置方式
这个配置不需要在Firebase控制台手动找入口,直接在代码的函数导出阶段,通过runWith方法传入独立的运行时参数,就能单独给指定函数设置540秒的超时上限,不会影响同项目下其他云函数的默认配置。
配置操作
- 定位到
deleteFolder函数的导出代码段,在https.onCall的前序链路接入runWith方法 - 在
runWith的入参对象中设置timeoutSeconds: 540,这个参数的合法取值范围为1秒到Cloud Functions允许的最大上限540秒,刚好匹配你需要的超时值 - 配置仅绑定在当前导出的函数上,不需要修改
firebase.json或者全局的云函数配置文件
修改后的完整代码片段
import * as functions from "firebase-functions"; const admin = require('firebase-admin'); admin.initializeApp(); const {Storage} = require("@google-cloud/storage"); const storage = new Storage({keyFilename: "myserviceaccount.json"}); const fileBucket = "myfilebucket"; // 新增单函数运行时超时配置 exports.deleteFolder = functions .runWith({ timeoutSeconds: 540 }) .https .onCall(async (data, context) => { if (!context.auth) { throw new functions.https.HttpsError( "unauthenticated", "only authenticated users can add requests" ); } const bucket = storage.bucket(fileBucket); return bucket.deleteFiles({ prefix: `${data.folderName}`, }, function(err:any) { if (err) { console.log(err); } else { console.log(`All the Firebase Storage files in users/${data.folderName}/ have been deleted`); } }); });
落地注意点
- 修改完成后重新执行云函数部署流程,新的超时配置才会在云端实例生效
- 所有运行时参数都可以在同一个
runWith配置块中统一设置,比如后续如果需要给这个删文件的函数调高内存配额,直接在入参对象里追加memory: "2GiB"这类参数即可 - 不要把
runWith的调用提到全局functions实例上,否则同项目下所有导出的云函数都会继承该配置,达不到单函数定向调整的目的
内容的提问来源于stack exchange,提问作者aedolfi
相关产品推荐
相关产品推荐

