简体   繁体   English

Firebase function 文件下载返回未定义

[英]Firebase function file.download returns undefined

I am trying to parse excel file and store the data into firestore.我正在尝试解析 excel 文件并将数据存储到 Firestore。 But before that I need to download the file.但在此之前我需要下载文件。 I am getting an error when dowloading the file into temp folder in google cloud storage.将文件下载到谷歌云存储中的临时文件夹时出现错误。 Here is the code:这是代码:

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);
  });
});

Here is the error这是错误

12:00:58.525 PM
helloWorld
<id>.appspot.com
12:00:58.525 PM
helloWorld
triggered 
12:00:58.525 PM
helloWorld
/tmp/communal_2019.xlsx
12:00:58.875 PM
helloWorld
Function returned undefined, expected Promise or value
12:00:58.880 PM
helloWorld
Function execution took 555 ms, finished with status: 'ok'

After some hours, I figured out that the file is downloaded and is in the wanted location.几个小时后,我发现文件已下载并位于所需位置。 I just needed to make it sync using await.我只需要使用等待使其同步。

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(async (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??"");
  await file.download({destination: path});
  // I can access my file using its path here
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM