0
votes

I have created some functions that upload and download files from firebase storage with cloud functions using the Firebase SDK and they work. I would like for the functions to be executed as admin so that they don't need to abide by the storage rules.

I have replaced the firebase SDK with the admin SDK but I found out that my firebase.storage().ref reference doesn't work anymore and by reading around some docs I have realized I now need to use the google cloud services system instead.

So my question is, is there a way to have a cloud function have administrator powers on the entire firebase project without having to switch to google cloud functions and if not, is there a work around to do that so that I can somehow authorize my cloud function to have full read/write powers on the entire storage? I am puzzled!

Here is a snippet of my code:

const firebase = require('firebase-admin');

const functions = require('firebase-functions');

require("firebase-admin")
require("firebase")
require("firebase/storage");

var serviceAccount = require("serviceAccount.json");

var config = {
    [...]
    credential: firebase.credential.cert(serviceAccount)
  };
  firebase.initializeApp(config);

  var storage = firebase.storage();
  var storageRef = storage.ref(); //This returns .ref() is not a function
1

1 Answers

0
votes

The Firebase client libraries are not intended to work on backend server environments. The Firebase Admin SDK is meant for backends, but the API to access Cloud Storage is different than the client SDK. The Admin SDK just wraps the Cloud Storage server SDKs. So for node environments, you are actually just going to use the Cloud Storage Node.js client.

When you call:

const admin = require('firebase-admin')
const storage = admin.storage()

you are getting a Storage object from the node SDK. It doesn't have a ref() method. You will need to get a Bucket object and use that instead:

const bucket = storage.bucket()

From here, you should continue to use the API docs I'm linking to.