Learning Firebase(3):Upload Image to Firebase with Python

Before you start to copy and paste the code, please make sure you have read my previous articles

2021/05/18 Update:

Image seem to be uploaded successfully, but when manually viewing it in Firebase Storage, it doesn’t load.

Install Package

pip install google-cloud-storage
pip install --upgrade gcloud
pip install firebase

Setting up Firebase Storage

Firebase -> Storage -> Rules# Set your rule as following:rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}

Let’s coding

from google.cloud import storage
from firebase import firebase
import os
# You just get your CREDENTIALS on previous step
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="filePathTo.json"
db_url='https://test-XXXXX.firebaseio.com' # Your project url
firebase = firebase.FirebaseApplication(db_url,None)
client = storage.Client()
bucket = client.get_bucket('test-XXXXX.appspot.com')
imageBlob = bucket.blob("/")
imagePath = "path/to/dir/" + fileName # Replace with your own path
imageBlob = bucket.blob(fileName)
imageBlob.upload_from_filename(imagePath) # Upload your image

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store