Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve image from Firebase Storage using Python?

I have already store my image to Firebase Storage, and I need to take it out by using Python code. Can I retrieve the image by using any URL? Or is there any way to retrieve it out?

Here are the image of how I store it in Firebase Storage:

Here are the image of how I store it in Firebase Storage

like image 755
Kai Avatar asked Mar 06 '23 01:03

Kai


1 Answers

This is what I use. Hope it helps.

import firebase_admin
from firebase_admin import credentials
from firebase_admin import storage

# Fetch the service account key JSON file contents
cred = credentials.Certificate("credentials.json")

# Initialize the app with a service account, granting admin privileges
app = firebase_admin.initialize_app(cred, {
    'storageBucket': '<BUCKET_NAME>.appspot.com',
}, name='storage')

bucket = storage.bucket(app=app)
blob = bucket.blob("<your_blob_path>")

print(blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET'))

It generates a public URL (for 300 secs) for you to download your files.

For example, in my case, I use that URL to display stored pictures in my django website with <img> tag.

Here is the doc for more usefull functions.

like image 54
Cheche Avatar answered Mar 09 '23 00:03

Cheche