Skip to content

Instantly share code, notes, and snippets.

@juliooa
Last active July 29, 2023 21:47
Show Gist options
  • Save juliooa/60fa6385c85aa30137dc2c75fe98ffed to your computer and use it in GitHub Desktop.
Save juliooa/60fa6385c85aa30137dc2c75fe98ffed to your computer and use it in GitHub Desktop.
Python script to upload an image to Firebase storage through his url
#!/usr/bin/env python
import sys
import requests
import firebase_admin
from firebase_admin import credentials
from firebase_admin import storage
image_url = sys.argv[1] #we pass the url as an argument
cred = credentials.Certificate('path/to/certificate.json')
firebase_admin.initialize_app(cred, {
'storageBucket': '<mysuperstorage>.appspot.com'
})
bucket = storage.bucket()
image_data = requests.get(image_url).content
blob = bucket.blob('new_cool_image.jpg')
blob.upload_from_string(
image_data,
content_type='image/jpg'
)
print(blob.public_url)
@CatsuKe
Copy link

CatsuKe commented Aug 23, 2018

Hello Julio,

Unfortunately, I'm a newbie in python, and I don't really know how to give the path to the image and to the certificate. Could you give me a more specific example?

@mihkels
Copy link

mihkels commented Sep 3, 2018

path/to/certificate.json is the relative path to file. For example, if you have upload_image.py in directory /projects and Firebase credentials file in /projects/credentials/firebase-key.json then the path to file would be credentials/firebase-key.json.

@ShashankPatel1997
Copy link

I am getting Error during execution of program

Traceback (most recent call last):
File "E:/tutorials/eCommerce/firebase test/demo4.py", line 8, in
image_url = sys.argv[1]
IndexError: list index out of range

@ShashankPatel1997
Copy link

line 8 : image_url = sys.argv[1] #we pass the url as an argument
how to get the URL ?

@fmarzocca
Copy link

line 8 : image_url = sys.argv[1] #we pass the url as an argument
how to get the URL ?

you should know the url of your image!

@Subbu0117
Copy link

code works well, Thank you!

@arunsriram1155
Copy link

is there any code to download files from firebase storage using python?

@tolgaijin
Copy link

Does the image url have to be a local path?
Can we also use http endpoint instead?

@danial459
Copy link

how to get storage bucket in firebase

@tkejr
Copy link

tkejr commented Jul 30, 2020

Hi how can I enter my url
I am really confused pls help

@tkejr
Copy link

tkejr commented Jul 30, 2020

@tkejr
Copy link

tkejr commented Jul 30, 2020

image_url = sys.argv[1] #we pass the url as an argument what do I pass in this ? my url or my path or what

@rmaran6
Copy link

rmaran6 commented Sep 17, 2020

Is there a way I can add a timeout to the upload? I am using this on a 4G connection and want to timeout if the connection is poor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment