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.
像魅力一样工作.