Firebase Storage Overwriting Files

此生再无相见时 提交于 2020-12-26 07:47:25

问题


I have small bug in my program.

I have firebase Storage in my app. I wanna let the user to upload image to the storage.

the thing is when the user uploads image and then tries to upload another image. what the program does it overwrites the image was uploaded before.

what I want is to avoid "overwrite" when the user wants to upload another image. and keep both images in different file

let currentUser = Auth.auth().currentUser
    let StorageRefrenece = Storage.storage().reference()
    let posterImageRef =
        StorageRefrenece.child("posters").child(currentUser!.uid).child("posterOne.jpg")

Thnx


回答1:


That is happening because you're storing them with the same name. In Firebase Storage you're the one in charge to decide the name of the files, there is no ".childByAutoId()".

If you want to have different files, you can create random values to name them, that is well explained here: How does one generate a random number in Apple's Swift language?

Your final code should look like this:

StorageRefrenece.child("posters").child(currentUser!.uid).child(<#Any Random Value#>)


来源:https://stackoverflow.com/questions/54450700/firebase-storage-overwriting-files

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