將檔案上傳到「Cloud Storage」後,也可以刪除這些檔案。
刪除檔案
如要刪除檔案
建立參考檔案。
檔案。然後對該參照呼叫 delete()
方法。
Kotlin+KTX
// Create a storage reference from our app val storageRef = storage.reference // Create a reference to the file to delete val desertRef = storageRef.child("images/desert.jpg") // Delete the file desertRef.delete().addOnSuccessListener { // File deleted successfully }.addOnFailureListener { // Uh-oh, an error occurred! }
Java
// Create a storage reference from our app StorageReference storageRef = storage.getReference(); // Create a reference to the file to delete StorageReference desertRef = storageRef.child("images/desert.jpg"); // Delete the file desertRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { // File deleted successfully } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Uh-oh, an error occurred! } });
處理錯誤
檔案刪除發生錯誤的可能原因有很多, 包含檔案不存在,或使用者沒有權限 刪除所需檔案如要進一步瞭解錯誤,請前往 處理錯誤 一節。