Home > OS >  firebase admin storage buckets don't recognize the initialized app
firebase admin storage buckets don't recognize the initialized app

Time:05-24

So we have an incredibly confusing situation. We initialize our firebase app and then try to access a storage bucket. Here is our code:

from firebase_admin import credentials, initialize_app
cred = credentials.Certificate(app.config.get('GOOGLE_APPLICATION_CREDENTIALS'))
initialize_app(cred, {'storageBucket': app.config['STORAGE_BUCKET']}, 'high_sec')
from firebase_admin import storage
bucket = storage.bucket()

Doing the above throws an error. Here is the Error:

 The default Firebase app does not exist. Make sure to initialize the SDK by calling initialize_app().

This makes no sense. We initialize the app immediately prior to calling the function complaining we didn't. The worst part is this used to work. We aren't sure what broke it.

Anyone have any idea what is happening here?

CodePudding user response:

According to this post, you should just call your initialize_app() at the global scope of your code. Don't pass any parameters to accept the default service account, which should have permission to read and write your Cloud Firestore database.

I've replicated your error, and by removing the 'high_sec', it works fine. For the code: initialize_app(cred, {'storageBucket': app.config['STORAGE_BUCKET']})

CodePudding user response:

We resolved this problem by passing the application object directly to the bucket. We figured this out by reverse engineering the sdk step by step. No idea if this is best practice. It definitely isn't intuitive.

app = initialize_app(cred, {'storageBucket': app.config['STORAGE_BUCKET']}, 'high_sec')
from firebase_admin import storage
bucket = storage.bucket(app=app)
  • Related