node.jsgoogle-cloud-storagegcloud-nodefirebase-storage

Using Firebase parameters with Google Cloud Storage under node.js


There is no node.js Firebase Storage client at the moment (too bad...), so I'm turning to gcloud-node with the parameters found in Firebase's console.

I'm trying :

var firebase = require('firebase');
var gcloud = require('gcloud')({
    keyFilename: process.env.FB_JSON_PATH,
    projectId: process.env.FB_PROJECT_ID
});

firebase.initializeApp({
    serviceAccount: process.env.FB_JSON_PATH,
    databaseURL: process.env.FB_DATABASE_URL
});

var fb = firebase.database().ref();

var gcs = gcloud.storage();

var bucket = gcs.bucket(process.env.FB_PROJECT_ID);

bucket.exists(function(err, exists) {
    console.log('err', err);
    console.log('exists', exists);
});

Where :

The id of the bucket is FB_PROJECT_ID (in Firebase's console the storage tab displays gs://app-a36e5.appspot.com)

When I run this code I get :

err null
exists false

But no other errors.

I'm expecting exists true at least.

Some additional info : I can query the database (so I imagine the JSON file is correct), and I have set the storage rules as follow :

service firebase.storage {
  match /b/app-a36e5.appspot.com/o {
    match /{allPaths=**} {
      allow read: if true;
      allow write: if request.auth != null;
    }
  }
}

So that everything on the storage is readable.

Any ideas how to get it to work ? Thank you.


Solution

  • The issue here is that you aren't naming your storage bucket correctly. The bucket initialization should be:

    var bucket = gcs.bucket('app-a36e5.appspot.com'); // full name of the bucket includes the .appspot.com
    

    I would assume that process.env.FB_PROJECT_ID is just the your-bucket part, and you'd need to get the full bucket name, not just the project id (though the bucket name may be process.env.FB_PROJECT_ID + '.appspot.com').

    Also, sorry about not providing Storage integrated with Firebase--GCS has a high quality library that you've already found (gcloud-node), and we figured that this provides the best story for developers (Firebase for mobile, Google Cloud Platform for server side development), and didn't want to muddy the waters further.