title: Upload base64 encoded jpeg to Firebase Storage (Admin SDK)
body: <p>I am trying to send a picture from my mobile hybrid app (Ionic 3) to my Heroku backend (Node.js) and have the backend upload the picture to Firebase Storage and return the newly uploaded fil download url to the mobile app.</p>
  
  <p>Keep in mind that I am using the Firebase Admin SDK for Node.js.</p>
  
  <p>So I send the base64 encoded image to Heroku (I check the encoded string with an online base64 decoder and it is alright) which is handle by the following function:</p>
  
  <pre><code>const uploadPicture = function(base64, postId, uid) {
    return new Promise((resolve, reject) =&gt; {
      if (!base64 || !postId) {
        reject("news.provider#uploadPicture - Could not upload picture because at least one param is missing.");
      }
  
      let bufferStream = new stream.PassThrough();
      bufferStream.end(new Buffer.from(base64, 'base64'));
  
      // Retrieve default storage bucket
      let bucket = firebase.storage().bucket();
  
      // Create a reference to the new image file
      let file = bucket.file(`/news/${uid}_${postId}.jpg`);
  
      bufferStream.pipe(file.createWriteStream({
        metadata: {
          contentType: 'image/jpeg'
        }
      }))
      .on('error', error =&gt; {
        reject(`news.provider#uploadPicture - Error while uploading picture ${JSON.stringify(error)}`);
      })
      .on('finish', (file) =&gt; {
        // The file upload is complete.
        console.log("news.provider#uploadPicture - Image successfully uploaded: ", JSON.stringify(file));
      });
    })
  };
  </code></pre>
  
  <p>I have 2 major issues:</p>
  
  <ol>
  <li>Upload succeeds but I when I go to Firebase Storage console, there is an error when I try to display the preview of the picture and I cannot open it from my computer when I download it. I guess it is an encoding thing....?</li>
  <li>How can I retrieve the newly uploaded file download url ? I was expecting an object to be returned in the <code>.on('finish)</code>, like in the <code>upload()</code> function, but none is returned (file is undefined). How could I retrieve this url to send it back in the server response?</li>
  </ol>
  
  <p>I want to avoid using the <code>upload()</code> function because I don't want to host files on the backend as it is not a dedicated server.</p>