如何使用Python从Firebase存储中检索图像? [英] How to retrieve image from Firebase Storage using Python?

查看:84
本文介绍了如何使用Python从Firebase存储中检索图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将图像存储到Firebase Storage中,需要使用Python代码将其取出.我可以使用任何URL检索图像吗?还是有什么办法可以找回它?

I have already store my image to Firebase Storage, and I need to take it out by using Python code. Can I retrieve the image by using any URL? Or is there any way to retrieve it out?

以下是我将其存储在Firebase Storage中的图像:

Here are the image of how I store it in Firebase Storage:

推荐答案

这就是我所使用的.希望对您有所帮助.

This is what I use. Hope it helps.

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'))

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

It generates a public URL (for 300 secs) for you to download your files.

例如,在我的情况下,我使用该URL在django网站中使用<img>标签显示存储的图片.

For example, in my case, I use that URL to display stored pictures in my django website with <img> tag.

此处是更有用的功能的文档.

Here is the doc for more usefull functions.

这篇关于如何使用Python从Firebase存储中检索图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆