2

I'm using multer with nodejs to handle multipart form data . I don't want to save the req.file which i get from client . I want to directly upload the file buffer in memory to google cloud storage ...

But the storage bucket's (firebase storage) upload method takes only a file path as argument.Is there any way i can achieve this directly without saving the file and upload the file buffer in memory to firebase storage directly ?

If so , how to do that ?

Natesh bhat
  • 12,274
  • 10
  • 84
  • 125

1 Answers1

0

The solution is right there in the cloud storage getting started guide for nodejs.

function sendUploadToGCS (req, res, next) {
  if (!req.file) {
    return next();
  }

  const gcsname = Date.now() + req.file.originalname;
  const file = bucket.file(gcsname);

  const stream = file.createWriteStream({
    metadata: {
      contentType: req.file.mimetype
    },
    resumable: false
  });

  stream.on('error', (err) => {
    req.file.cloudStorageError = err;
    next(err);
  });

  stream.on('finish', () => {
    req.file.cloudStorageObject = gcsname;
    file.makePublic().then(() => {
      req.file.cloudStoragePublicUrl = getPublicUrl(gcsname);
      next();
    });
  });

  stream.end(req.file.buffer);
}

Reference: https://cloud.google.com/nodejs/getting-started/using-cloud-storage

Gaurav Sharma
  • 1,983
  • 18
  • 18
  • 2
    this doesn't work... and doesn't show how to pipe the multer memory buffer object. – Colin May 26 '20 at 18:25
  • 1
    @Gaurav thanks for sharing the code snippet. this worked for me. can you please getPublicUrl() function. where to find this. – Qadir Hussain Jul 24 '20 at 10:53
  • @QadirHussain You can keep the storage URL by appending the bucket name. All the objects are relative to your bucket directory structure. This is how you can generate your public URL – Gaurav Sharma Aug 07 '20 at 22:43
  • @Colin I am sorry if it didn’t work for you. The question didn’t ask anything specific about Multer. However, the file object is indeed parsed in this snippet using Multer itself. And the amswer is regarding pushing the uploaded file directly to GCS without saving locally. – Gaurav Sharma Aug 07 '20 at 22:49
  • @Colin Please check the question description and the link I’ve shared. If you still have trouble then I can help you if you can share the snippet regarding the issue you are facing. Or you can create a new question with Multer specific implementation issue. – Gaurav Sharma Aug 07 '20 at 22:50