Upload files to Firebase Storage using Node.js
<p>I'm trying to understand how to upload files in Firebase Storage, using Node.js. My first try was to use the Firebase library:</p>
<pre><code>&quot;use strict&quot;;

var firebase = require('firebase');

var config = {
    apiKey: &quot;AIz...kBY&quot;,
    authDomain: &quot;em....firebaseapp.com&quot;,
    databaseURL: &quot;https://em....firebaseio.com&quot;,
    storageBucket: &quot;em....appspot.com&quot;,
    messagingSenderId: &quot;95...6&quot;
};

firebase.initializeApp(config);

// Error: firebase.storage is undefined, so not a function
var storageRef = firebase.storage().ref();

var uploadTask = storageRef.child('images/octofez.png').put(file);

// Register three observers:
// 1. 'state_changed' observer, called any time the state changes
// 2. Error observer, called on failure
// 3. Completion observer, called on successful completion
uploadTask.on('state_changed', function(snapshot){
    ...
}, function(error) {
    console.error(&quot;Something nasty happened&quot;, error);
}, function() {
  var downloadURL = uploadTask.snapshot.downloadURL;
  console.log(&quot;Done. Enjoy.&quot;, downloadURL);
});
</code></pre>
<p>But it turns out that Firebase cannot upload files from the server side, as it clearly states in the docs:</p>
<blockquote>
<p>Firebase Storage is not included in the server side Firebase npm module. Instead, you can use the gcloud Node.js client.</p>
<p><code>$ npm install --save gcloud</code></p>
<p>In your code, you can access your Storage bucket using:</p>
<pre><code>var gcloud = require('gcloud')({ ... }); var gcs = gcloud.storage();
var bucket = gcs.bucket('&lt;your-firebase-storage-bucket&gt;');
</code></pre>
</blockquote>
<ul>
<li><p>Can we use <code>gcloud</code> without having an account on Google Cloud Platform? How?</p>
</li>
<li><p>If not, how come that uploading files to Firebase Storage from the client side is possible?</p>
</li>
<li><p>Can't we just create a library that makes the same requests from the server side?</p>
</li>
<li><p>How is Firebase Storage connected with Google Cloud Platform at all? Why Firebase allows us to upload images only from the client side?</p>
</li>
</ul>
<hr />
<p>My second try was to use the <code>gcloud</code> library, like mentioned in the docs:</p>
<pre><code>var gcloud = require(&quot;gcloud&quot;);

// The following environment variables are set by app.yaml when running on GAE,
// but will need to be manually set when running locally.
// The storage client is used to communicate with Google Cloud Storage
var storage = gcloud.storage({
  projectId: &quot;em...&quot;,
  keyFilename: 'auth.json'
});

storage.createBucket('octocats', function(err, bucket) {

    // Error: 403, accountDisabled
    // The account for the specified project has been disabled.

    // Create a new blob in the bucket and upload the file data.
    var blob = bucket.file(&quot;octofez.png&quot;);
    var blobStream = blob.createWriteStream();

    blobStream.on('error', function (err) {
        console.error(err);
    });

    blobStream.on('finish', function () {
        var publicUrl = `https://storage.googleapis.com/${bucket.name}/${blob.name}`;
        console.log(publicUrl);
    });

    fs.createReadStream(&quot;octofez.png&quot;).pipe(blobStream);
});
</code></pre>