title: Firebase function storage POST image issue
body: <p>I have encountered a problem when following a maximilian schwarzmüller course, which has otherwise been great: <a href="https://www.youtube.com/watch?v=qZ1EFnFOGvE" rel="nofollow noreferrer">https://www.youtube.com/watch?v=qZ1EFnFOGvE</a></p>
  
  <p>The image logs in the Firebase console as uploaded, recognises the type of file/size etc. But continually loads and never displays the image. I use a post request in POSTMAN to upload the image.
  When I upload manually to firebase on their UI, everything works fine.</p>
  
  <p>My code:</p>
  
  <pre><code>const functions = require("firebase-functions");
  const os = require("os");
  const path = require("path");
  const spawn = require("child-process-promise").spawn;
  const cors = require("cors")({ origin: true });
  const Busboy = require("busboy");
  const fs = require("fs");
  
  
  
  const gcconfig = {
    projectId: "REDACTED",
    keyFilename: "REDACTED"
  };
  
  const gcs = require("@google-cloud/storage")(gcconfig);
  
  //
  exports.onFileChange = functions.storage.object().onFinalize(event =&gt; {
    const object = event.data;
    const bucket = object.bucket;
    const contentType = object.contentType;
    const filePath = object.name;
    console.log("File change detected, function execution started");
  
    if (object.resourceState === "not_exists") {
      console.log("We deleted a file, exit...");
      return;
    }
  
    if (path.basename(filePath).startsWith("resized-")) {
      console.log("We already renamed that file!");
      return;
    }
  
    const destBucket = gcs.bucket(bucket);
    const tmpFilePath = path.join(os.tmpdir(), path.basename(filePath));
    const metadata = { contentType: contentType };
    return destBucket
      .file(filePath)
      .download({
        destination: tmpFilePath
      })
      .then(() =&gt; {
        return spawn("convert", [tmpFilePath, "-resize", "500x500", tmpFilePath]);
      })
      .then(() =&gt; {
        return destBucket.upload(tmpFilePath, {
          destination: "resized-" + path.basename(filePath),
          metadata: metadata
        });
      });
  });
  
  exports.uploadFile = functions.https.onRequest((req, res) =&gt; {
    cors(req, res, () =&gt; {
      if (req.method !== "POST") {
        return res.status(500).json({
          message: "Not allowed"
        });
      }
      const busboy = new Busboy({ headers: req.headers });
      let uploadData = null;
  
      busboy.on("file", (fieldname, file, filename, encoding, mimetype) =&gt; {
        const filepath = path.join(os.tmpdir(), filename);
        uploadData = { file: filepath, type: mimetype };
        file.pipe(fs.createWriteStream(filepath));
      });
  
      busboy.on("finish", () =&gt; {
        const bucket = gcs.bucket("REDACTED");
        bucket
          .upload(uploadData.file, {
            uploadType: "media",
            metadata: {
              metadata: {
                contentType: uploadData.type
              }
            }
          })
          .then(() =&gt; {
            res.status(200).json({
              message: "It worked!"
            });
          })
          .catch(err =&gt; {
            res.status(500).json({
              error: err
            });
          });
      });
      busboy.end(req.rawBody);
    });
  });
  </code></pre>
  
  <p>My security rules:</p>
  
  <pre><code>rules_version = '2'; 
  service firebase.storage { 
    match /b/{bucket}/o { 
      match /{allPaths=**} { 
        allow read, write:if true; 
      } 
    } 
  }
  </code></pre>