skip to Main Content

I’m trying to get the default bucket in Firebase from a function. It appears there was recently a change in the SDK and I can’t find information on retrieving it anymore. I’ve tried a million things my current code looks like this and returns an empty string.

import * as admin from "firebase-admin";
import * as functions from "firebase-functions";

admin.initializeApp({
  credential: admin.credential.applicationDefault(),
});

export const processVideo = functions.https.onCall(async (data, context) => {
  const bucket = admin.storage().bucket.name;

  functions.logger.log("***************Bucket Name****************", bucket);
});

2

Answers


  1. Chosen as BEST ANSWER

    It turns out that the information is stored in an environment variable but not imported into the admin tooling directly. So the best way I have found to prepare your environment to serve the default bucket name is as follows:

    const firebaseConfig = JSON.parse(process.env.FIREBASE_CONFIG!);
    
    admin.initializeApp({
      credential: admin.credential.applicationDefault(),
      storageBucket: firebaseConfig.storageBucket,
    });
    

  2. The documentation suggests that you’re supposed to provide that value to the SDK when you initialize it. The SDK doesn’t just know it without being told.

    You can specify a default bucket name when initializing the Admin SDK. Then you can retrieve an authenticated reference to this bucket. The bucket name must not contain gs:// or any other protocol prefixes. For example, if the bucket URL displayed in the Firebase console is gs://bucket-name.appspot.com, pass the string bucket-name.appspot.com to the Admin SDK.

    initializeApp({
        credential: cert(serviceAccount),
        storageBucket: '<BUCKET_NAME>.appspot.com'
    });
    

    So, you have to get the value from the Firebase console and use that to initialize the SDK.

    Also, you need to use a method called bucket(), not a property. The API documentation says it returns a Bucket object.

    admin.storage().bucket()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search