简体   繁体   English

如何使用 python 从 firebase 下载文件夹?

[英]How to download a folder from firebase with python?

i want to download all images in a firebase storage folder.我想下载 firebase 存储文件夹中的所有图像。 I tried to follow the instructions of this post How to download an entire folder in firebase storge using python?我尝试按照这篇文章的说明如何使用 python 在 Firebase 存储中下载整个文件夹? . . 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' .这是我得到的错误AttributeError: 'Storage' object has no attribute 'bucket' this is the folder in my firebase storage:这是我的 Firebase 存储中的文件夹: 在此处输入图像描述

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')
  • list_files() requires a service account as well, please add it to your config. list_files()也需要一个服务帐户,请将其添加到您的配置中。

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)

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.我有很多文件夹,list_file() 会全部获取。

file.name is " your folder name/your image name.jpg ". file.name 是“您的文件夹名称/您的图像名称.jpg”。 I think the problem is if you use file.name as the image name while downloading, it might not work.我认为问题是如果您在下载时使用file.name作为图像名称,它可能无法正常工作。

if you want keep the origin name , maybe use split() to get and use it while downloading.如果您想保留原始名称,可以在下载时使用 split() 获取和使用它。

and if you want to choose the place to save them, just add the path in download_to_filename() like this:如果您想选择保存它们的位置,只需在 download_to_filename() 中添加路径,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM