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 series:

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.

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

Written by

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

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