我正在尝试下载存储在 Firebase 中的图像文件,以便稍后将其用作用户个人资料图片。现在我无法通过下载部分。我将图像命名为用户的 UID,以便为每个用户下载正确的图像。我用过:
storage.child("/user_info/user_image/"+user_uid+".png").download("/user files/", user_uid+".png")
下载它。在我看来,它什么也没做。我检查了文件名和路径。这是正确的。
主.py
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import pyrebase
firebaseConfig = {
your config here
}
firebase = pyrebase.initialize_app(firebaseConfig)
auth = firebase.auth()
db=firebase.database()
storage=firebase.storage()
class MainApp(MDApp):
def build(self):
return Builder.load_file('main.kv')
def login(self):
email = self.root.get_screen("test1win").ids.user_mail.text
password = self.root.get_screen("test1win").ids.password.text
try:
login = auth.sign_in_with_email_and_password(email, password)
self.root.current = "test2win"
print("Login Success")
global user_uid
user_uid = login['localId']
print(user_uid)
global user_uinf
user_uinf = []
except:
print("Invalid credentials.")
def database_info(self):
storage.child("/user_info/user_image/"+user_uid+".png").download("/user files/", user_uid+".png")
userinf = db.child('userinfo').child(user_uid).get()
for x in userinf.each():
user_uinf.append(x.val())
print(user_uinf)
self.root.get_screen("test2win").ids.testwin2label.text = user_uinf[1]+" "+user_uinf[4]+", "+user_uinf[0]+", "+str(user_uinf[2])+", "+user_uinf[3]
class WindowManager(ScreenManager):
pass
class TestWindow1(Screen):
pass
class TestWindow2(Screen):
pass
if __name__ == "__main__":
MainApp().run()
主.kv
#: include testwindow1.kv
#: include testwindow2.kv
WindowManager:
TestWindow1:
TestWindow2:
测试窗口1.kv
<TestWindow1>:
name: "test1win"
Screen:
BoxLayout:
orientation: 'vertical'
MDTextField:
id: user_mail
hint_text: "E-Mail"
size_hint_x: 0.8
pos_hint: {"center_x": 0.5}
font_size: 24
mode: "rectangle"
MDTextField:
id: password
hint_text: "Password"
size_hint_x: 0.8
font_size: 24
pos_hint: {"center_x": 0.5}
mode: "rectangle"
MDRaisedButton:
text: "Login"
pos_hint: {"center_x": 0.5}
on_release: app.login()
Widget:
测试窗口2.kv
<TestWindow2>:
name: "test2win"
BoxLayout:
orientation: 'vertical'
Screen:
MDLabel:
id: testwin2label
text: "You're logged in"
MDRaisedButton:
id: button
text: "Press to show database information"
on_release: app.database_info()
Widget:
你可以试试这个
storage.download("/user_info/user_image/"+str(user_uid)+".png", str(user_uid)+".png")
Pyrebase4 的文档似乎已经过时了。查看下载函数的源代码表明我们将 firebase 上文件的位置作为第一个参数传递,并将文件下载到本地系统上的位置作为第二个参数传递。例如这样:
firebase = pyrebase.initialize_app(config)
storage = firebase.storage()
firebase_file_path = "Images/plates/plate.png" #Path to the file on firebase
local_path = "vid_files/plate_d.png" #Local path with new filename
storage.download(path=firebase_file_path, filename=local_path)
希望这有帮助!