Firebase存储:使用下载URL代替存储参考 [英] Firebase Storage: Using Download Url Instead of Storage Ref

本文介绍了Firebase存储:使用下载URL代替存储参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iOS应用程序,该应用程序使用Firebase Storage来存储图像.上载图像后,将其storage reference保存在我的Firebase Database中.应用加载时,它会从数据库中获取各种storage references并使用FirebaseUI方法来显示其相应的图像,如下所示:

I have an iOS app that uses Firebase Storage for storing images. After an image is uploaded I save its storage reference in my Firebase Database. When the app loads, it fetches various storage references from the database and uses the FirebaseUI method to display their corresponding images like so:

let storageRef = Storage.storage().reference(forURL: imageUrl)
imageView.sd_setImage(with: storageRef, placeholderImage: nil)

这很好...但是非常慢.

This works great... but its very slow.

在寻找加快速度的解决方案时,我发现这篇文章暗示使用图像的公共链接,即.其download url而不是其storage reference: https://stackoverflow.com/a/44362350/5225013

While looking for solutions to speeding up Firebase Storage I found this post that hints at using an image's public link, ie. its download url, instead of its storage reference: https://stackoverflow.com/a/44362350/5225013

我的理解是,这与public urls前面带有CDN的内容有关,而storage references则没有.

My understanding is that this has something to do with the public urls having a CDN in front of them, whereas the storage references don't.

可以使用以下代码在应用程序中检索download url:

The download url can be retrieved in the app with the following code:

let storageRef = Storage.storage().reference(forURL: imageUrl)
storageRef.downloadURL { (downloadUrl, error) in

    if let downloadUrl = downloadUrl {
        // do something with downloadUrl
    }
}

以这种方式获取它对于我的目的来说是毫无用处的,因为在显示图像之前,要花费额外的时间进行异步调用操作...

Getting it this way is pretty useless for my purpose, though, because making the asynchronous call tacks on extra time before the image can be displayed...

我一直想编写一个云函数来将每个图像的download url与相应的storage reference一起保存在数据库中,然后使用它在应用程序中显示图像.这些图像不是私人的,也不包含敏感内容,因此我不介意它们是否公开.我主要是想知道如果这样做我有什么需要担心的吗?这是不常见的做法吗?

I've been toying with the idea of writing a cloud function to save each image's download url alongside its corresponding storage reference in the database, and then using that for displaying the images in the app. The images aren't personal, nor do they contain sensitive content, so I don't mind if they're public. I'm mainly wondering; is there anything I should be worried about if I do this? Is it uncommon practise for a reason?

推荐答案

您无需执行"操作,任何东西.

You don't need to "do" anything.

对于存储中的公共文件(即具有allow read;安全规则的文件),获取它们的URL为:

For public files in storage (i.e. files with the allow read; security rule), the URL to get them is:

https://firebasestorage.googleapis.com/v0/b/{project id} .appspot.com/o/{storage path}?alt = media

https://firebasestorage.googleapis.com/v0/b/{project id}.appspot.com/o/{storage path}?alt=media

我在应用程序中要做的是存储存储路径,然后使用简单的串联构造公共下载URL.

What I do in my apps is store the storage path and then construct the public download URL using simple concatenation.

像魅力一样工作.

这篇关于Firebase存储:使用下载URL代替存储参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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