Search code examples
node.jsfirebaseexpressfirebase-storage

Express server can't fully interact with Firebase Storage


So, storage.ref() works and storage.child("some.txt") works and returns a file refrence, i can get its name by .name. But I cant either get download link nor upload a file. My config is as follows:

|-firebase
  |-firebase.js
  |-storage.js
|-index.js
firebase.js:
    const firebase = require('firebase/app');
    global.XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
    let firebaseStorage = require('firebase/storage');
    // Your web app's Firebase configuration
    var firebaseConfig = {
      apiKey: "abc", //real credentials are provided
      authDomain: "def", //real credentials are provided
      projectId: "xyz", //real credentials are provided
      storageBucket: "hi", //real credentials are provided
      messagingSenderId: "idk", //real credentials are provided
      appId: "some" //real credentials are provided
    };
    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);
    console.log('[FIREBASE] Firebase Init')


    module.exports = firebase;

And storage is just exporting firebase.storage(); nothing too much. the problem here is: this doesnt return anything and the whole code is stuck in this bit:

  let fileRef = storageRef.child('myfile.txt');

  fileRef.getDownloadURL().then((downloadURL) => {
    console.log('File available at: ', downloadURL);
    res.send(downloadURL);
  });

I spent alot of time trying to find simillar questions and using different libs for that but i cant find anything.


Solution

  • The Firebase javascript modules were designed to work within a browser environment, for node.js it is suggested to use the admin sdk with limited credentials or GCloud Node. You can use this with your existing storage bucket and the typical Firebase setup.

    To quote a Firebase Dev:

    Since Google Cloud Storage already provides a number of high-quality client libraries, we didn't want to reimplement another

    Client environment: GCP integration

    Server environment: Admin-sdk Get Started