简体   繁体   中英

Unable to delete image from firebase?

I want to delete an image from firebase with the download url of image. When I copy & paste the url into a browser I am able to view the file. However, when I pass it in firebase via the function below, then it does not get deleted.

func removeMediaFromFirebase(url:String) {        
    let imageRef = FIRStorage.storage().reference(forURL: url)
    imageRef.delete { (error) in
        if error != nil {
            DILog.print(items: "Unable to delete \(error.debugDescription)")
        }
    }    
}

It causes a 404 error stating "Object not found", but it's exactly the same as URL as where I accessed the image!

As far as I know removing image from Firebase Storage is not possible by providing image download URL to delete function.

The URL that should be used as a parameter is the storage path to the file, not the actual file URL. So don't use downloadURL function to get file url, instead use the file path. There is a great documentation on their website, I think you should read it through to get familiar with it.

Anyway here is an example on what that URL actually means:

let imageURL = "/path/to/image.jpg"
let imageReference = FIRStorage.storage().reference(forURL: imageURL)

Or this way:

let imageReference = FIRStorage.storage().reference().child("/path/to/image.jpg")

After getting image reference, you can perform a delete on it:

imageReference.delete { error in
  if let error = error {
    // Uh-oh, an error occurred!
  } else {
    // File deleted successfully
  }
}

This way it should work. To see more examples, check Firebase documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM