简体   繁体   English

无法从Firebase删除图片?

[英]Unable to delete image from firebase?

I want to delete an image from firebase with the download url of image. 我想使用图像的下载URL从Firebase删除图像。 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. 但是,当我通过下面的函数在firebase中传递它时,它不会被删除。

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! 它会导致404错误,提示“找不到对象”,但是它与我访问图片的网址完全相同!

As far as I know removing image from Firebase Storage is not possible by providing image download URL to delete function. 据我所知,通过提供图像下载URL进行删除功能无法从Firebase存储中删除图像。

The URL that should be used as a parameter is the storage path to the file, not the actual file URL. 应该用作参数的URL是文件的存储路径,而不是实际的文件URL。 So don't use downloadURL function to get file url, instead use the file path. 因此,请勿使用downloadURL函数获取文件url,而应使用文件路径。 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: 无论如何,以下是该URL实际含义的示例:

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 要查看更多示例,请参阅Firebase文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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