Kiên Lư
Kiên Lư

Reputation: 5

How to download a folder from firebase with python?

i want to download all images in a firebase storage folder. I tried to follow the instructions of this post How to download an entire folder in firebase storge using python? . But no success. I am getting error like below, can someone help me, Thanks for reading this post. this is the error i got AttributeError: 'Storage' object has no attribute 'bucket'. this is the folder in my firebase storage: enter image description here

import pyrebase

config = {

  "apiKey": "",
  "authDomain": "",
  "projectId": "",
    "databaseURL": "",
  "storageBucket": "",
  "messagingSenderId": "",
  "appId": "",

}
firebase=pyrebase.initialize_app(config)
storage = firebase.storage()

path = "data"

ab=str(1)    
all_files = storage.child("images").list_files()
for file in all_files:             
    try:
        print(file.name)
        z=storage.child(file.name).get_url(None)
        storage.child(file.name).download(""+path+"/"+ab+".png")
        x=int(ab)
        ab=str(x+1)


    except:
        print('Download Failed')

Upvotes: 0

Views: 705

Answers (2)

ilok
ilok

Reputation: 1

I met the same problem today.

and it took me literally a whole night. :(

I google every keyword which might help but nothing help

and I found a way to download them!

I'm not good at this, so maybe something wrong. but it works for me. hope it work for you too!

I tried to download them with my own name like this:

all_files = storage.child("images").list_files() # get all file
cnt = 0
for file in all_files:
  print(file.name)
  file.download_to_filename(str(cnt)+".jpg")
  cnt = cnt + 1

and it works. I have a lots folders , and list_file() will get them all.

file.name is "your folder name/your image name.jpg". I think the problem is if you use file.name as the image name while downloading, it might not work.

if you want keep the origin name , maybe use split() to get and use it while downloading.

and if you want to choose the place to save them, just add the path in download_to_filename() like this:

path = 'C:/foldername'
file.download_to_filename(path+str(cnt)+".jpg")

Upvotes: 0

ItzMaxTV
ItzMaxTV

Reputation: 86

  • list_files() requires a service account as well, please add it to your config.

Try this instead if you would like to download the images:

import pyrebase

config = {
  "apiKey": "",
  "authDomain": "",
  "projectId": "",
  "databaseURL": "",
  "storageBucket": "",
  "messagingSenderId": "",
  "appId": "",
  "serviceAccount": "path/to/serviceAccountCredentials.json"
}
firebase=pyrebase.initialize_app(config)
storage = firebase.storage()

path = "data"

ab=str(1)    
all_files = storage.child("images").list_files()

for file in all_files:
    storage.child("images").child(file.name).download(path)

Upvotes: 1

Related Questions