【问题标题】:Firebase Cloud Functions raise time limitFirebase Cloud Functions 提高时间限制
【发布时间】:2021-06-09 17:46:56
【问题描述】:

我为 Firebase 编写了一个可调用 https 的云函数,应该删除文件夹中的所有文件。看起来是这样的:

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.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 {
      // eslint-disable-next-line max-len
      console.log(`All the Firebase Storage files in users/${data.folderName}/ have been deleted`);
    }
  });
});

效果很好。但是我的文件夹有时会包含很多文件,因此我在部署此功能时获得的标准时间限制 60 秒有时不足以删除文件夹内的所有文件。所以我想将计算时间限制提高到 540 秒,女巫是根据 Cloud Functions 我可以获得的最大计算时间。但我找不到这样做的可能性。

如何提高单个 https 可调用函数的时间限制?

【问题讨论】:

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


    【解决方案1】:

    doc中所述,您需要执行以下操作:

    const runtimeOpts = {
            timeoutSeconds: 540
          }
    
    
    exports.deleteFolder = functions
                            .runWith(runtimeOpts)
                            .https
                            .onCall(async (data, context) => {...});
    

    请注意,您也可以通过 Google Cloud Console(不是 Firebase 控制台)配置此值。

    在 Google Cloud Console 中,选择您的 Firebase 项目,选择“Cloud Functions”垂直菜单项,点击函数名称,然后点击“编辑”按钮。然后点击“VARIABLEs,....”部分标题如下图,调整对应字段的Timeout值。

    【讨论】:

      猜你喜欢
      • 2018-09-22
      • 2020-10-22
      • 2018-01-03
      • 2019-05-06
      • 1970-01-01
      • 2019-01-14
      • 1970-01-01
      • 2018-04-19
      • 2017-08-26
      相关资源
      最近更新 更多