无法使用 google 云存储和 Cloud Functions for Firebase 下载文件

Can't download file using google cloud storage and Cloud Functions for Firebase

我的问题与此相反:

(郑重声明,我已经尝试了那里建议的所有方法)。

基本上我有一个已知的文件路径,然后是一个由数据库事件触发的云函数。

我可以初始化一个存储桶,获取一个文件及其名称,但是当我尝试下载它时,我得到 API 错误:未找到。

这是我的代码:

module.exports = (orgID, reportID) => {
  const bucket = gcs.bucket("MY_PROJECT.appspot.com");

  const filePath = `/safety_hotline/${orgID}/${reportID}`;
  const file = bucket.file(filePath);

  // the name is shown correctly in the console
  console.log(file.name);

  const tempLocalFile = path.join(os.tmpdir(), filePath);
  const tempLocalDir = path.dirname(tempLocalFile);

  return mkdirp(tempLocalDir)
    .then(() => {
      // Download file from bucket.
      return file.download({ destination: tempLocalFile });
    })
    .then(() => {
      console.log("file downloaded succesfully");
    })
    .catch(err => {
      console.log(err);
    });
}

你可以看到我得到了文件名的控制台日志,所以我不明白为什么我不能下载它? 任何建议都会很棒,谢谢!

编辑:为清楚起见对代码进行了一些编辑

You Try This Follow functions-samples ?

我正在尝试完成 下载文件成功

我看到你有这条线:

  const filePath = `/safety_hotline/${orgID}/${reportID}`;

我猜您可能使用 safety_hotline/org/report 模式命名了您的对象,但正如上面所写,对象名称的第一个字符是斜线。这也是一个合法的对象名称,但通常是无意的。尝试删除斜杠?