How can i change Firebase storage image file name which is already uploaded?

允我心安 提交于 2021-01-28 06:47:54

问题


I need to change the file name which is already uploaded in firebase storage. Because, after uploading a image in firebase storage, i save the url in firebase database under a specific child(folder). But when i move the image to another child(folder), i need to change the image file name in storage according to new name. How can i do this? Firebase metadata has a name property, but it is readonly property.


回答1:


You cannot change the filename of the image that is already uploaded to firebase storage. What you can do is

Step 1: download the given image from the storage

Step 2: Convert the UIImage to data by using UIImagePNGRepresentation of UIImageJPEGRepresentation methods.

Step 3: Create a reference using the desired name Storage.storage().reference().child("DesiredName")

Step 4: Upload the data to firebase storage .putData

Step 5: Delete the previously uploaded data from firebase.

// Create a reference to the file to delete
let desertRef = storageRef.child("desert.jpg")
// Delete the file
desertRef.delete { error in
   if let error = error {
   // Uh-oh, an error occurred!
   } else {
   // File deleted successfully
   }
}


来源:https://stackoverflow.com/questions/47008537/how-can-i-change-firebase-storage-image-file-name-which-is-already-uploaded

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