File and Image Uploads with Express and Firebase Cloud Functions

Published over 4 years ago.

I recently spent longer than I'd care to admit trying to figure out how to properly do file or image uploads with Express and Firebase Cloud Functions.

TL;DR do not try and use Multer for this. As it turns out, Cloud Functions introduced a breaking middleware for Multer which parses the body of the request. The result is that Multer will always return a blank req.body, req.file and req.files.

So what are your options? Google documents them both here, but let me save you a click:

  1. Use busboy to directly parse multipart form-data and do something with it (sample code).
  2. Upload directly Google Cloud Storage directly using Signed URLs (sample code)

My solution was to create and use a middleware based off the code samples Google had that make things feel more Multer-y.

// middleware.js

exports.filesUpload = function (req, res, next) {
  // See
  const busboy = new Busboy({
    headers: req.headers,
    limits: {
      // Cloud functions impose this restriction anyway
      fileSize: 10 * 1024 * 1024,

  const fields = {};
  const files = [];
  const fileWrites = [];
  // Note: os.tmpdir() points to an in-memory file system on GCF
  // Thus, any files in it must fit in the instance's memory.
  const tmpdir = os.tmpdir();

  busboy.on("field", (key, value) => {
    // You could do additional deserialization logic here, values will just be
    // strings
    fields[key] = value;

  busboy.on("file", (fieldname, file, filename, encoding, mimetype) => {
    const filepath = path.join(tmpdir, filename);
      `Handling file upload field ${fieldname}: ${filename} (${filepath})`
    const writeStream = fs.createWriteStream(filepath);

      new Promise((resolve, reject) => {
        file.on("end", () => writeStream.end());
        writeStream.on("finish", () => {
          fs.readFile(filepath, (err, buffer) => {
            const size = Buffer.byteLength(buffer);
            console.log(`${filename} is ${size} bytes`);
            if (err) {
              return reject(err);

              originalname: filename,

            try {
            } catch (error) {
              return reject(error);

        writeStream.on("error", reject);

  busboy.on("finish", () => {
      .then(() => {
        req.body = fields;
        req.files = files;


Similar to Multer, this middleware will ensure req.body are any text fields in form-data and req.files is an array of uploaded files.

const express = require("express");

const { filesUpload } = require("./middleware");

app = express();"/upload", filesUpload, function (req, res) {
  // will contain all text fields
  // will contain an array of file objects
      fieldname: 'image',       String - name of the field used in the form
      originalname,             String - original filename of the uploaded image
      encoding,                 String - encoding of the image (e.g. "7bit")
      mimetype,                 String - MIME type of the file (e.g. "image/jpeg")
      buffer,                   Buffer - buffer containing binary data
      size,                     Number - size of buffer in bytes

exports = app;

Hope this helps.

Mike Sukmanowsky

๐Ÿ‘‹๐Ÿป Hi! I'm Mike, the author of this post and others.

If you'd like to get in touch, you can reach me on Twitter or LinkedIn.