Skip to main content
 首页 » 编程设计

python之如何使用 Python 从 Firebase 存储中检索图像

2024年12月31日11shihaiming

我已经将我的图像存储到 Firebase 存储中,我需要使用 Python 代码将其取出。我可以使用任何 URL 检索图像吗?或者有什么方法可以取回吗?

这是我如何将其存储在 Firebase 存储中的图像:

请您参考如下方法:

这是我用的。希望对您有所帮助。

import firebase_admin 
from firebase_admin import credentials 
from firebase_admin import storage 
 
# Fetch the service account key JSON file contents 
cred = credentials.Certificate("credentials.json") 
 
# Initialize the app with a service account, granting admin privileges 
app = firebase_admin.initialize_app(cred, { 
    'storageBucket': '<BUCKET_NAME>.appspot.com', 
}, name='storage') 
 
bucket = storage.bucket(app=app) 
blob = bucket.blob("<your_blob_path>") 
 
print(blob.generate_signed_url(datetime.timedelta(seconds=300), method='GET')) 

它会生成一个公共(public) URL(持续 300 秒)供您下载文件。

例如,在我的例子中,我使用该 URL 来显示我的 django 网站中存储的图片 <img>标签。

Here是更多有用功能的文档。