Firebase Storage: Using Download Url Instead of Storage Ref

谁说我不能喝 提交于 2020-05-15 04:59:20

问题


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.

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

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.

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...

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?


回答1:


You don't need to "do" anything.

For public files in google storage, the URL to get them is:

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

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

Works like a charm.



来源:https://stackoverflow.com/questions/58443123/firebase-storage-using-download-url-instead-of-storage-ref

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!