'Download all files from firebase storage using python
I am working on a project where the user uploads images into firebase storage and i want to retrieve all those images and store locally.
Using pyrebase i can manage to download a single image(if i know the filename or download URL)
storage = firebase.storage()
storage.child("images/example.jpg").download("img")
But in my situation i may not know what is the name or download URL of file is. Pyrebase only supports to download single file and the filename should also be known.
Is there an alternative way to list / download all the files from firebase storage? Code snippet would be helpful.
Solution 1:[1]
Pyrebase Storage supports a list_files() function. You can use this to loop through all files in a given folder, downloading on each pass with the download_to_filename() function.
Something like this:
storage = firebase.storage()
datadir = 'path/to/local/directory/'
all_files = storage.child("myfirebasefolder").list_files()
for file in all_files:
try:
file.download_to_filename(datadir + file.name)
except:
print('Download Failed')
Solution 2:[2]
Here the file names are in 1,2,3,.... format. Here you have to add the json account also, by downloading it from firebase settings.
config={
"serviceAccount":"abc.json"
}
firebase=pyrebase.initialize_app(config)
storage = firebase.storage()
ab=str(1)
all_files = storage.child("images").list_files() //Enter the name of the folder
for file in all_files:
try:
print(file.name)
z=storage.child(file.name).get_url(None)
storage.child(file.name).download(""+path+"/"+ab+".mp3")
x=int(ab)
ab=str(x+1)
except:
print('Download Failed')
Solution 3:[3]
Typically you store paths to uploaded files to Realtime Database or some other data store. This lets you iterate uploaded files programmatically if you want to make changes to them later.
Alternatively, you can use the list_blobs() API as shown in the documentation for the Cloud Storage python SDK.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | muzz_83 |
| Solution 2 | Pavani Jayalath |
| Solution 3 |
