title: Uploading to google cloud storage with Node.js
body: <p>Struggling with Node.js and Google cloud. I am trying to upload a file to a bucket in Google Cloud Storage. Basically I am using the code in this answer: <a href="https://stackoverflow.com/a/45253054/324691">https://stackoverflow.com/a/45253054/324691</a>, but I can't get it to work. Here is my code:</p>
  
  <pre><code>const Storage = require('@google-cloud/storage');
  const storage = new Storage();
  var form = new formidable.IncomingForm();
  
  // form.maxFieldsSize = 20 * 1024 * 1024; // default
  form.maxFieldsSize = 20 * 1024;
  // form.maxFileSize = 200 * 1024 * 1024;
  // 4 MB / minute, 1 hour
  form.maxFileSize = 4 * 60 * 1024 * 1024;
  // Limit to 10 minutes during tests (4 MB/minute)
  form.maxFileSize = 2 * 1024 * 1024;
  form.maxFileSize = 4 * 10 * 1024 * 1024;
  
  form.encoding = 'utf-8';
  form.keepExtensions = true;
  form.type = 'multipart';
  
  return new Promise((resolve, reject) =&gt; {
      form.parse(req, (err, fields, files) =&gt; {
          console.warn("form.parse callback", err, fields, files);
          if (err) {
              reject(new Error(err));
              return;
          }
          var file = files.upload;
          if(!file){
              reject(new Error("no file to upload, please choose a file."));
              return;
          }
          console.info("about to upload file as a json: " + file.type);
          var filePath = file.path;
          console.log('File path: ' + filePath);
          console.log('File name: ' + file.name);
  
          var bucket = storage.bucket(bucketName)
          console.info("typeof bucket.upload", typeof bucket.upload);
          bucket.upload(filePath, {
              destination: file.name
          }).then(() =&gt; {
              resolve(true);  // Whole thing completed successfully.
              return true;
          }).catch((err) =&gt; {
              console.warn("catch bucket.upload, err", err);
              reject(new Error('Failed to upload: ' + JSON.stringify(err)));
          });
          console.warn("After bucket.upload");
      });
      console.warn("After form.parse");
  }).then(() =&gt; {
      console.warn("form.parse then");
      res.status(200).send('Yay!');
      return true;
  }).catch(err =&gt; {
      console.warn("form.parse catch");
      console.error('Error while parsing form: ' + err);
      res.status(500).send('Error while parsing form: ' + err);
  });
  </code></pre>
  
  <p>The output in the console log looks ok (including file.path and file.name), but when it gets to <code>bucket.upload</code> (which is of type <code>function</code>) then it crashes with the error <code>Error while parsing form: Error: Failed to upload: {"message":"Error during request."}</code>. And then it says <code>Execution took 1910 ms, user function completed successfully</code>. And after that finally <code>Function worker killed by signal: SIGTERM</code>.</p>
  
  <p>I have been testing using <code>bucketName</code> with and without "gs://".</p>
  
  <p>And I have been testing different ways for authorization:</p>
  
  <pre><code>const storage = new Storage({ projectId: projectId });
  const storage = new Storage({ keyFileName: 'path-to-keyfile.json'});
  const storage = new Storage();
  </code></pre>
  
  <p>The key file is a key file for the service account (see <a href="https://cloud.google.com/docs/authentication/getting-started" rel="nofollow noreferrer">https://cloud.google.com/docs/authentication/getting-started</a>).</p>
  
  <p>This is a Firebase project and I am testing on the local Firebase server (<code>firebase serve --only functions,hosting</code>).</p>
  
  <p>What could be wrong here?</p>