將檔案上傳至 Cloud Storage 後,您也可以刪除檔案。
刪除檔案
如要刪除檔案,請先建立該檔案的參照。然後對該參照呼叫 delete()
方法。
Kotlin
// 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! } });
處理錯誤
檔案刪除作業可能會發生錯誤,原因有很多,包括檔案不存在,或是使用者沒有權限刪除所需檔案。如要進一步瞭解錯誤,請參閱文件中的「處理錯誤」一節。