I'm running web app based on Firebase Realtime Database and Firebase Storage.
I need to upload new images to Firebase google bucket every hour via Python google-cloud-storage lib. Here are the docs.
My code for image upload (img_src path is correct):
bucket = storage.bucket()
blob = bucket.blob(img_src)
blob.upload_from_filename(filename=img_path, content_type='image/png')
Image seem to be uploaded successfully, but when manually viewing it in Firebase Storage, it doesn't load. All the image's specs seem to be correct. Please compare specs of manually uploaded image (loads fine) with corrupted one.
Thanks for help!
Cloud Storage for Firebase stores your files in a Google Cloud Storage bucket, making them accessible through both Firebase and Google Cloud. This allows you the flexibility to upload and download files from mobile clients via the Firebase SDKs for Cloud Storage.
Image URL is obtained by uploading an image to firebase bucket and then that can return back a URL that URL is a permanent URL which can be open anywhere. Then a user can use this URL for any purpose in its application.
Whenever you upload an image using Firebase Console, an access token will be automatically generated. However, if you upload an image using any Admin SDK or gsutil you will need to manually generate this access token yourself.
Here is an example on how to generate and set an access token for an image using the Admin Python SDK.
import firebase_admin
from firebase_admin import credentials
from firebase_admin import storage
# Import UUID4 to create token
from uuid import uuid4
cred = credentials.Certificate("path/to/your/service_account.json")
default_app = firebase_admin.initialize_app(cred, {
'storageBucket': '<BUCKET_NAME>.appspot.com'
})
bucket = storage.bucket()
blob = bucket.blob(img_src)
# Create new token
new_token = uuid4()
# Create new dictionary with the metadata
metadata = {"firebaseStorageDownloadTokens": new_token}
# Set metadata to blob
blob.metadata = metadata
# Upload file
blob.upload_from_filename(filename=img_path, content_type='image/png')
Here is quick explanation:
from uuid import uuid4
new_token = uuid4()
metadata = {"firebaseStorageDownloadTokens": new_token}
blob.metadata = metadata
blob.upload_from_filename(...)
This solution can be implemented for any Admin SDK.
Firebase Support says that this is being fixed, but I think anyone having this problem should go this way instead of waiting for Firebase to fix this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With