3
votes

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.

3

3 Answers

1
votes

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. (There is currently no list API for files in Cloud Storage.)

0
votes

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')    
-1
votes

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')