2
votes

I am having the opposite as this issue: issues deleting an image using Cloud Functions for Firebase and @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.

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.

Here is my code:

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

enter image description here

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!

Edit: edited code a bit for clarity

2
Great Question!!ISS

2 Answers

3
votes

I see you have this line:

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

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?

0
votes

You Try This Follow functions-samples ?

i'am try this follow done Download file successful