Firebase Storage 如何存储和检索图像 [英] Firebase Storage How to store and Retrieve images

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

问题描述

如何在 firebase 上存储和查看图像?

How to store and view images on firebase?

推荐答案

更新 (20160519):Firebase 刚刚发布了一项名为 Firebase 存储.这允许您将图像和其他非 JSON 数据上传到专用存储服务.我们强烈建议您使用它来存储图像,而不是将它们作为 base64 编码数据存储在 JSON 数据库中.

Update (20160519): Firebase just released a new feature called Firebase Storage. This allows you to upload images and other non-JSON data to a dedicated storage service. We highly recommend that you use this for storing images, instead of storing them as base64 encoded data in the JSON database.

你当然可以!根据您的图片有多大,您有几个选择:

You certainly can! Depending on how big your images are, you have a couple options:

1.对于较小的图片(小于 10mb)

我们在这里有一个示例项目:https://github.com/firebase/firepano

We have an example project that does that here: https://github.com/firebase/firepano

一般的方法是在本地加载文件(使用 FileReader),这样您就可以像处理任何其他数据一样将其存储在 Firebase 中.由于图像是二进制文件,因此您需要获取 base64 编码的内容,以便将其存储为字符串.或者更方便的是,您可以将其存储为 data: url 然后准备好插入作为 img 标签的 src(这就是示例所做的)!

The general approach is to load the file locally (using FileReader) so you can then store it in Firebase just as you would any other data. Since images are binary files, you'll want to get the base64-encoded contents so you can store it as a string. Or even more convenient, you can store it as a data: url which is then ready to plop in as the src of an img tag (this is what the example does)!

2.对于更大的图像

Firebase 确实有 10mb(utf8 编码的字符串数据)限制.如果您的图像更大,则必须将其分成 10 mb 的块.您是对的,尽管 Firebase 更针对频繁更改的小字符串而不是多兆字节字符串进行了优化.如果您有大量大型静态数据,我肯定会推荐 S3 或 CDN.

Firebase does have a 10mb (of utf8-encoded string data) limit. If your image is bigger, you'll have to break it into 10mb chunks. You're right though that Firebase is more optimized for small strings that change frequently rather than multi-megabyte strings. If you have lots of large static data, I'd definitely recommend S3 or a CDN instead.

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

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