Firebase 存储:使用下载 Url 而不是存储参考
Firebase Storage: Using Download Url Instead of Storage Ref
我有一个 iOS
应用程序,它使用 Firebase Storage
来存储图像。上传图像后,我将其 storage reference
保存在我的 Firebase Database
中。当应用程序加载时,它从数据库中获取各种 storage references
并使用 FirebaseUI
方法显示它们相应的图像,如下所示:
let storageRef = Storage.storage().reference(forURL: imageUrl)
imageView.sd_setImage(with: storageRef, placeholderImage: nil)
这很好用...但是速度很慢。
在寻找加速 Firebase Storage
的解决方案时,我发现这个 post 暗示使用图像的 public link,即。它的download url
,而不是它的storage reference
:
我的理解是,这与 public urls
前面有 CDN,而 storage references
没有。
download url
可以在应用程序中使用以下代码检索:
let storageRef = Storage.storage().reference(forURL: imageUrl)
storageRef.downloadURL { (downloadUrl, error) in
if let downloadUrl = downloadUrl {
// do something with downloadUrl
}
}
尽管如此,以这种方式获取它对我来说毫无用处,因为在显示图像之前进行异步调用需要额外的时间...
我一直在考虑编写一个云函数来将每个图像的 download url
与其对应的 storage reference
一起保存在数据库中,然后使用它在应用程序中显示图像.这些图片不是个人图片,也不包含敏感内容,所以我不介意它们是 public。我主要想知道;如果我这样做,有什么我应该担心的吗?这种不常见的做法是有原因的吗?
您不需要“做”任何事情。
对于存储中的 public 个文件(即具有 allow read;
安全规则的文件),获取它们的 URL 是:
https://firebasestorage.googleapis.com/v0/b/(project id).appspot.com/o/(storage path)?alt=media
我在我的应用程序中所做的是存储存储路径,然后使用简单的连接构建 public 下载 URL。
很有魅力。
请注意,存储路径中的某些字符(如空格和斜杠)必须进行转义。在 JavaScript 中,您可以使用 encodeURIComponent()
。
另请注意,为存储强制执行 App Check 会破坏这一点。
我有一个 iOS
应用程序,它使用 Firebase Storage
来存储图像。上传图像后,我将其 storage reference
保存在我的 Firebase Database
中。当应用程序加载时,它从数据库中获取各种 storage references
并使用 FirebaseUI
方法显示它们相应的图像,如下所示:
let storageRef = Storage.storage().reference(forURL: imageUrl)
imageView.sd_setImage(with: storageRef, placeholderImage: nil)
这很好用...但是速度很慢。
在寻找加速 Firebase Storage
的解决方案时,我发现这个 post 暗示使用图像的 public link,即。它的download url
,而不是它的storage reference
:
我的理解是,这与 public urls
前面有 CDN,而 storage references
没有。
download url
可以在应用程序中使用以下代码检索:
let storageRef = Storage.storage().reference(forURL: imageUrl)
storageRef.downloadURL { (downloadUrl, error) in
if let downloadUrl = downloadUrl {
// do something with downloadUrl
}
}
尽管如此,以这种方式获取它对我来说毫无用处,因为在显示图像之前进行异步调用需要额外的时间...
我一直在考虑编写一个云函数来将每个图像的 download url
与其对应的 storage reference
一起保存在数据库中,然后使用它在应用程序中显示图像.这些图片不是个人图片,也不包含敏感内容,所以我不介意它们是 public。我主要想知道;如果我这样做,有什么我应该担心的吗?这种不常见的做法是有原因的吗?
您不需要“做”任何事情。
对于存储中的 public 个文件(即具有 allow read;
安全规则的文件),获取它们的 URL 是:
https://firebasestorage.googleapis.com/v0/b/(project id).appspot.com/o/(storage path)?alt=media
我在我的应用程序中所做的是存储存储路径,然后使用简单的连接构建 public 下载 URL。
很有魅力。
请注意,存储路径中的某些字符(如空格和斜杠)必须进行转义。在 JavaScript 中,您可以使用 encodeURIComponent()
。
另请注意,为存储强制执行 App Check 会破坏这一点。