0

I've been stuck on this problem for awhile now and can't seem to figure out why this file isn't being uploaded to firebase correctly. I'm using the code below in firebase functions to generate a document, then I convert that document to a stream, finally I create a write stream declaring the path that I want the file to be written to and pipe my document stream to the firebase storage WriteStream.

Example 1: PDF uploaded from file system through firebase console. (Link works and displays pdf)

Example 2: PDF generated in firebase functions and written to storage using code below

Considerations:

  1. I know the PDF is valid because I can return it from the function and see it in my web browser and everything is as I would expect.
  2. When trying to open the bad file it doesn't display anything and redirects me back to the overview screen.
const pdfGen = require("html-pdf");
pdfGen.create(html, { format: "Letter" }).toStream(function (err, stream) {
    if (err) return res.status(500).send(err);
    var file = storage.bucket()
      .file(job.jobNum + "/quote-doc.pdf")
      .createWriteStream({
        resumable  : false,
        validation : false,
        contentType: "auto",
        metadata   : {
            'Cache-Control': 'public, max-age=31536000'}
    });

    stream.pipe(file)
    .on('error', function(err) {
      return res.status(500).send(err);
    })
    .on('finish', function(data) {
      stream.close();
      file.end();
      res.end();
      console.log('finished upload');
    });
  });
  • Any update, I have the same issue. Cant open uploaded files in firebase storage. – Tim Dirks Jun 17 '20 at 12:15
  • @TimDirks Unfortunately the bug seems to still persist however I have found that when I read the file in my application it appears and after it has been read firebase will enable the link allowing you to read it in storage. Very odd behavior but the file seems to be stored properly just not accessible in firebase until it's been read at least once somewhere else. – Tony Marcinkewciz Mar 27 '21 at 13:40

0 Answers0