如何删除Firebase存储文件?

时间:2017-01-09 16:49:05

标签: ios swift firebase swift3 firebase-storage

我错过了什么吗?网络上的答案与我做同样的事情,但....下载网址不再有扩展名,所以我的错误说:

"error": {
    "code": 404,
    "message": "Not Found.  Could not delete object"
}

代码:

let url = demo[indexPath.row].downloadUrl
print(url) // shows the url

DataService.ds.REF_RECEIPTS.child(url).delete { (error) in
  if error != nil {
    print(error)
  }
}

这不起作用。其他人拥有和工作的是url =“demo.jpg”,我的只是一个带有令牌但没有文件扩展名的字符串。如果我去网址,我会看到图片,那是什么问题?

编辑:

这是我上传图片的方式:

let receiptImageData = UIImageJPEGRepresentation(imagePlaceholder.image!, 0.2)
let metadata = FIRStorageMetadata()
metadata.contentType = "image/jpeg"

let receiptUID = NSUUID().uuidString

DataService.ds.REF_RECEIPTS.child(CURRENT_USER!.uid + "_" + receiptUID).put(receiptImageData!, metadata: metadata) { (metadata, error) in
    if let receiptDownloadedUrl = metadata?.downloadURL()?.absoluteString {
        self.expenseData(receiptUrl: receiptDownloadedUrl)

        // Saving the image/receipt in local cache for faster access
        //guard let img = UIImage(data: receiptImageData!) else { return }
        //IMAGE_CACHE.setObject(img, forKey: receiptDownloadedUrl as NSString)
    }

}

func expenseData(receiptUrl: String) {...}

我也尝试了let receiptUID = NSUUID().uuidString + ".jpg"但同样的问题。

1 个答案:

答案 0 :(得分:2)

我在引用时犯了一个错误。我不是专家,但我认为这个引用应该有效:DataService.ds.REF_RECEIPTS.child()。delete。有用的是:

let url = demo[indexPath.row].downloadUrl
let imageRef = FIRStorage.storage().reference(forURL: url)
imageRef.delete { (error) in
  if let error == error {
    // error
  } else {
    // success
  }
}

我不知道REF_RECEIPTS是否引用了存储文件夹,这就是为什么我可以put而不是delete的原因。