Firebase 函数 file.download 返回 undefined

问题描述

我正在尝试解析 excel 文件并将数据存储到 firestore 中。但在此之前,我需要下载文件。 将文件下载到谷歌云存储中的临时文件夹时出现错误代码如下:

scikit-image

这里是错误

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import * as os from "os";
import {join} from "path";
import {existsSync,mkdirsync} from "fs";

const fh = functions.storage.object();
export const helloWorld = fh.onFinalize((object) => {
  const path = join(os.tmpdir(),object.name??"");
  if (!existsSync(os.tmpdir())) {
    mkdirsync(os.tmpdir());
  }
  console.log(object.bucket);
  functions.logger.log("triggered");
  functions.logger.log(path);
  if (!admin.apps.length) {
    admin.initializeApp();
  }
  const bkt = admin.storage().bucket(object.bucket);
  const file = bkt.file(object.name??"");
  const prom = file.download({destination: path});
  prom.then((p) => {
    functions.logger.log(p.length);
  });
});

解决方法

几个小时后,我发现文件已下载并位于所需位置。 我只需要使用 await 使其同步。

MLPlots