我正在开发一个项目,用户将图像上传到firebase存储中,我想要检索所有这些图像并在本地存储。
使用pyrebase我可以设法下载单个图片(如果我知道文件名或下载网址)
storage = firebase.storage()
storage.child("images/example.jpg").download("img")
但在我的情况下,我可能不知道文件的名称或下载URL是什么。 Pyrebase仅支持下载单个文件,文件名也应该是已知的。
是否有其他方法可以列出/下载firebase存储中的所有文件?代码段会有所帮助。
答案 0 :(得分:0)
通常,您将上载文件的路径存储到Realtime Database或其他一些数据存储。如果您想稍后对其进行更改,则可以以编程方式迭代上载的文件。 (云存储中的文件目前没有列表API。)
答案 1 :(得分:0)
Pyrebase Storage支持list_files()
函数。您可以使用它遍历给定文件夹中的所有文件,并通过download_to_filename()
函数在每次通过时下载。
类似这样的东西:
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')
答案 2 :(得分:0)
此处文件名为1,2,3,....格式。在这里,您还必须通过从Firebase设置下载JSON帐户来添加它。
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')