Đôi khi, mọi thứ không diễn ra theo kế hoạch và xảy ra lỗi.
Khi nghi ngờ, hãy kiểm tra lỗi được trả về và xem thông báo lỗi có nội dung gì.
Đoạn mã sau đây cho thấy một cách triển khai trình xử lý lỗi tuỳ chỉnh, kiểm tra mã lỗi và thông báo lỗi do Cloud Storage trả về. Bạn có thể thêm các trình xử lý lỗi như vậy vào nhiều đối tượng được dùng trong API Cloud Storage (ví dụ: UploadTask và FileDownloadTask).
Nếu bạn đã kiểm tra thông báo lỗi và có Cloud Storage Security Rules cho phép bạn thực hiện hành động, nhưng vẫn gặp khó khăn trong việc khắc phục lỗi, hãy truy cập vào trang Hỗ trợ của chúng tôi và cho chúng tôi biết cách chúng tôi có thể trợ giúp.
Xử lý thông báo lỗi
Có một số lý do khiến lỗi có thể xảy ra, bao gồm cả việc tệp không tồn tại, người dùng không có quyền truy cập vào tệp mong muốn hoặc người dùng huỷ tải tệp lên.
Để chẩn đoán đúng vấn đề và xử lý lỗi, sau đây là danh sách đầy đủ tất cả các lỗi mà ứng dụng khách của chúng tôi sẽ đưa ra và cách các lỗi đó có thể xảy ra. Mã lỗi trong bảng này được xác định trong lớp StorageException dưới dạng hằng số nguyên.
Mã
Lý do
ERROR_UNKNOWN
Đã xảy ra lỗi không xác định.
ERROR_OBJECT_NOT_FOUND
Không có đối tượng nào tại thông tin tham chiếu đã chỉ định.
ERROR_BUCKET_NOT_FOUND
Không có vùng chứa nào được định cấu hình cho Cloud Storage
ERROR_PROJECT_NOT_FOUND
Không có dự án nào được định cấu hình cho Cloud Storage
ERROR_QUOTA_EXCEEDED
Bạn đã vượt quá hạn mức trên vùng chứa Cloud Storage. Nếu bạn đang dùng gói giá Spark, hãy cân nhắc nâng cấp lên gói giá linh hoạt (trả tiền theo mức dùng). Nếu bạn đang sử dụng gói giá Linh hoạt, hãy liên hệ với Nhóm hỗ trợ Firebase.
Lưu ý quan trọng: Kể từ ngày 1 tháng 10 năm 2025, bạn bắt buộc phải sử dụng gói giá Linh hoạt để dùng Cloud Storage, kể cả các vùng chứa mặc định.
ERROR_NOT_AUTHENTICATED
Người dùng chưa được xác thực, vui lòng xác thực rồi thử lại.
ERROR_NOT_AUTHORIZED
Người dùng không được phép thực hiện hành động được yêu cầu, hãy kiểm tra các quy tắc để đảm bảo chúng chính xác.
ERROR_RETRY_LIMIT_EXCEEDED
Đã vượt quá giới hạn thời gian tối đa cho một thao tác (tải lên, tải xuống, xoá, v.v.). Hãy thử lại.
ERROR_INVALID_CHECKSUM
Tệp trên máy khách không khớp với tổng kiểm của tệp mà máy chủ nhận được. Hãy thử tải lên lại.
ERROR_CANCELED
Người dùng đã huỷ thao tác.
Ngoài ra, việc cố gắng gọi getReferenceFromUrl() bằng một URL không hợp lệ sẽ dẫn đến việc IllegalArgumentException bị loại bỏ. Đối số cho phương thức trên phải có dạng gs://bucket/object hoặc https://firebasestorage.googleapis.com/v0/b/bucket/o/object?token=<TOKEN>
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-22 UTC."],[],[],null,["\u003cbr /\u003e\n\nSometimes things don't go as planned and an error occurs.\n\nWhen in doubt, check the error returned and see what the error message says.\nThe following code shows a custom error handler implementation that inspects\nthe error code and error message returned by Cloud Storage. Such error\nhandlers can be added to various objects used in the Cloud Storage API (for\nexample, `UploadTask` and `FileDownloadTask`). \n\nKotlin \n\n```kotlin\ninternal inner class MyFailureListener : OnFailureListener {\n override fun onFailure(exception: Exception) {\n val errorCode = (exception as StorageException).errorCode\n val errorMessage = exception.message\n // test the errorCode and errorMessage, and handle accordingly\n }\n}https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/storage/app/src/main/java/com/google/firebase/referencecode/storage/kotlin/StorageActivity.kt#L492-L498\n```\n\nJava \n\n```java\nclass MyFailureListener implements OnFailureListener {\n @Override\n public void onFailure(@NonNull Exception exception) {\n int errorCode = ((StorageException) exception).getErrorCode();\n String errorMessage = exception.getMessage();\n // test the errorCode and errorMessage, and handle accordingly\n }\n}https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/storage/app/src/main/java/com/google/firebase/referencecode/storage/StorageActivity.java#L618-L625\n```\n\nIf you've checked the error message and have Cloud Storage Security Rules that allow your\naction, but are still struggling to fix the error, visit our\n[Support page](/support) and let us know how we can help.\n\nHandle Error Messages\n\nThere are a number of reasons why errors may occur, including the file\nnot existing, the user not having permission to access the desired file, or the\nuser cancelling the file upload.\n\nTo properly diagnose the issue and handle the error, here is a full list of all\nthe errors our client will raise, and how they can occur. Error codes in this\ntable are defined in the `StorageException` class as integer constants.\n\n| Code | Reason |\n|------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `ERROR_UNKNOWN` | An unknown error occurred. |\n| `ERROR_OBJECT_NOT_FOUND` | No object exists at the specified reference. |\n| `ERROR_BUCKET_NOT_FOUND` | No bucket is configured for Cloud Storage |\n| `ERROR_PROJECT_NOT_FOUND` | No project is configured for Cloud Storage |\n| `ERROR_QUOTA_EXCEEDED` | Quota on your Cloud Storage bucket has been exceeded. If you're on the Spark pricing plan, consider upgrading to the [pay-as-you-go Blaze pricing plan](/pricing). If you're already on the Blaze pricing plan, reach out to Firebase Support. **Important** : Starting October 1, 2025, the [Blaze pricing plan will be *required* to use Cloud Storage](/docs/storage/faqs-storage-changes-announced-sept-2024), even default buckets. |\n| `ERROR_NOT_AUTHENTICATED` | User is unauthenticated, please authenticate and try again. |\n| `ERROR_NOT_AUTHORIZED` | User is not authorized to perform the requested action, check your rules to ensure they are correct. |\n| `ERROR_RETRY_LIMIT_EXCEEDED` | The maximum time limit on an operation (upload, download, delete, etc.) has been excceded. Try again. |\n| `ERROR_INVALID_CHECKSUM` | File on the client does not match the checksum of the file received by the server. Try uploading again. |\n| `ERROR_CANCELED` | User canceled the operation. |\n\nAdditionally, attempting to call `getReferenceFromUrl()` with an invalid URL\nwill result in an `IllegalArgumentException` from being thrown. The argument to\nthe above method must be of the form `gs://bucket/object` or\n`https://firebasestorage.googleapis.com/v0/b/bucket/o/object?token=\u003cTOKEN\u003e`"]]