0

I am using the NPM Package called Busboy to handle Multipart. When a file is sent by the Client to the Server, I wish to immediately send the incoming file directly to Firebase Storage without first storing it on my server.

Currently, I'm storing the file on my server before sending it to firebase storage, I don't want that behaviour anymore. I want the file to be sent directly to Firebase as it is coming in.

Here is my current file upload code:

const http = require('http');
const { v4: uuidv4 } = require('uuid');
const firebase = require('./firebase_service').firebase;
const busboy = require('busboy');

http.createServer((req, res) => {

    if (req.method === 'POST') {

    const bb = busboy({ headers: req.headers });

    bb.on('file', async (name, file, filename, info) => {
      
      //Currently I'm first storing the file on disk as below which is what I don't want
      let writeStream = fs.createWriteStream(filename); //I don't want to do this
      file.pipe(writeStream);

      //After storing the file above, below is how I am sending it to firebase storage
      let uuid = uuidv4();
      let bucket = firebase.storage().bucket();
      await bucket.upload(filename, { //So instead of this, is there a way to send the file directly to firebase as it is coming from the client?
        resumable: false,
        gzip: true,
        metadata: {
            cacheControl: 'public, max-age=31536000',
            firebaseStorageDownloadTokens: uuid,
        }
    });

    });
    bb.on('field', (name, val, info) => {
       //Omitted
    });
    bb.on('close', () => {
      res.writeHead(303, { Connection: 'close', Location: '/' });
      res.end();
    });
    req.pipe(bb);
  }
}).listen(8000, () => {
  console.log('Listening for requests');
});

What's a better work around to this?

Thank you

user15155716
  • 571
  • 6
  • 16
  • "Firebase Storage" is actually just Google Cloud Storage, and the Firebase Admin SDK just wraps the Google Cloud SDK. The duplicate explains how to do it by creating a write stream. The product documentation for streaming transfers is [here](https://cloud.google.com/storage/docs/streaming). – Doug Stevenson May 27 '22 at 03:22

0 Answers0