无法使用Google Cloud Storage和Cloud Functions for Firebase下载文件

无法使用Google Cloud Storage和Cloud Functions for Firebase下载文件

问题描述:

与这个问题相反: 发出使用Cloud删除图像的问题Firebase和@ google-cloud/storage的功能

(为记录起见,我已经尝试了所有建议的方法).

(for the record, I have tried all things suggested there).

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

Basically I have a known file path, then a cloud function triggered by a database event.

我可以初始化存储桶,获取文件及其名称,但是当我尝试下载它时,出现API错误:找不到.

I can initialise a bucket, get a file as well as its name, but then when I try and download it I get API Error: not found.

这是我的代码:

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 can see I get the console log of the file name, so I don't understand why I can't then download it? Any advice would be amazing, thanks!

为清晰起见,对代码进行了一些修改

edited code a bit for clarity

我看到你有这行:

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

我猜您可能已经用模式safety_hotline/org/report命名了对象,但是如对象名称的第一个字符上方所示,将是一个斜线.这也是一个合法的对象名称,但这通常是无意的.尝试删除斜线吗?

I am guessing that you may have named your objects with the pattern safety_hotline/org/report, but as written above the first character of the object name would be a slash. That's also a legal object name but it's usually unintentional. Try removing the slash?