Python firebase storage emulator
Asked Answered
V

2

5

Hi I was wondering how to connect to my firebase storage emulator, this is how I connect to Firestore. How do I do it with storage

Firestore Example:

if emulator:
    os.environ["FIRESTORE_EMULATOR_HOST"] = "0.0.0.0:8080"
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "myCredentials.json"

# Retrieve the application credentials
cred = credentials.ApplicationDefault()

options = {
    "storageBucket": "my-storage-bucket-name.appspot.com"
}

# Initialise the app
firebase_app = firebase_admin.initialize_app(cred, options)

What is the equivalent for firebase storage?

Virg answered 22/6, 2021 at 17:13 Comment(0)
L
10

Looking at the emulation documentation, it seems that the Python Admin SDK does not currently support the Storage emulator but it is listed as 'future' indicating it is in development but does not have a current release date.

Source: https://firebase.google.com/docs/emulator-suite/install_and_configure#admin_sdk_availability

Lesson answered 23/6, 2021 at 12:35 Comment(1)
The firebase-admin-python github repo has an issue identifying this need, but its been crickets since last summer github.com/firebase/firebase-admin-python/issues/570Sinew
E
0

The environment variable for the Firebase Storage Emulator is FIREBASE_STORAGE_EMULATOR_HOST. Source: https://firebase.google.com/docs/emulator-suite/connect_storage#admin_sdks

But as explained by @DIGI Byte, the Python Firebase Admin SDK does not yet have this implemented.

Hopefully I found a workaround by digging into the Google Storage SDK source code: just set the "STORAGE_EMULATOR_HOST" environment variable.

# for future compatibility (as explained by @digi-byte)
os.environ["FIREBASE_STORAGE_EMULATOR_HOST"] = "127.0.0.1:9199"

# current workaround
os.environ["STORAGE_EMULATOR_HOST"] = "http://127.0.0.1:9199"

Then you can upload files to Firebase Storage Emulator successfully. E.g.:

from firebase_admin import storage

bucket = storage.bucket() # don't forget to set "storageBucket" in "firebase_admin.initialize_app"
blob = bucket.blob('path/to/my/file')
blob.upload_from_file(file)

Some things may not work properly using the emulator. In my tests, blob.make_public() fails.

Eruptive answered 31/1, 2024 at 22:35 Comment(1)
This doesn't work for me. What a pitty from Google.Experimentalism

© 2022 - 2025 — McMap. All rights reserved.