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

Learning Firebase(1):Create Your First Project

Learning Firebase(2):CRUD Our Database with Python

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

Once you get the idea of how firebase works, you now have the right to copy and paste my code.

2021/05/18 Update:

Firebase Storage — error with uploading png image via Python google-cloud-storage lib

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

1.Rules:

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

2. Find your GOOGLE_APPLICATION_CREDENTIALS

Refer to the following image to save your CREDENTIALS json file

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

Machine Learning | Deep Learning | https://linktr.ee/yanwei