处理 Cloud Storage 错误 (Android)
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
错误时有发生。
如有疑问,请检查返回的错误,并查看错误消息的内容。下面的代码显示了一个自定义错误处理程序实现,它会检查 Cloud Storage 返回的错误代码和错误消息。这类错误处理程序可以添加到 Cloud Storage API 使用的各种对象(例如 UploadTask
和 FileDownloadTask
)中。
Kotlin
internal inner class MyFailureListener : OnFailureListener {
override fun onFailure(exception: Exception) {
val errorCode = (exception as StorageException).errorCode
val errorMessage = exception.message
// test the errorCode and errorMessage, and handle accordingly
}
}
Java
class MyFailureListener implements OnFailureListener {
@Override
public void onFailure(@NonNull Exception exception) {
int errorCode = ((StorageException) exception).getErrorCode();
String errorMessage = exception.getMessage();
// test the errorCode and errorMessage, and handle accordingly
}
}
如果您已查看错误消息并且 Cloud Storage Security Rules 允许您执行操作,但仍然无法消除错误,请访问我们的支持页面,并告诉我们如何为您提供帮助。
处理错误消息
很多原因都可能导致错误发生,包括文件不存在、用户没有访问所需文件的权限,或者用户取消文件上传操作。
为了正确诊断问题并处理错误,请查看下面的完整列表,了解我们的客户端可能报告的所有错误以及错误发生的原因。该表中的错误代码在 StorageException
类中定义为整型常量。
代码 |
原因 |
ERROR_UNKNOWN |
发生未知错误。 |
ERROR_OBJECT_NOT_FOUND |
指定的引用中没有任何对象。 |
ERROR_BUCKET_NOT_FOUND |
没有为 Cloud Storage 配置存储桶 |
ERROR_PROJECT_NOT_FOUND |
没有为 Cloud Storage 配置项目 |
ERROR_QUOTA_EXCEEDED |
已超过您的 Cloud Storage 存储桶的配额。如果您使用的是 Spark 定价方案,不妨考虑升级到随用随付 Blaze 定价方案。如果您已采用 Blaze 定价方案,请与 Firebase 支持团队联系。
重要提示:自 2025 年 10 月 1 日起,必须采用 Blaze 定价方案才能使用 Cloud Storage,即使是默认存储桶也是如此。 |
ERROR_NOT_AUTHENTICATED |
用户未经过身份验证,请先进行身份验证,然后重试。 |
ERROR_NOT_AUTHORIZED |
用户无权执行所请求的操作,请检查您的规则,确保其正确无误。 |
ERROR_RETRY_LIMIT_EXCEEDED |
已超出某项操作(上传、下载、删除等)的最长时间限制。请重试。 |
ERROR_INVALID_CHECKSUM |
客户端上的文件与服务器收到的文件的校验和不匹配。请尝试重新上传。 |
ERROR_CANCELED |
用户已取消操作。 |
此外,试图使用无效网址调用 getReferenceFromUrl()
方法将抛出 IllegalArgumentException
异常。上述方法的参数必须采用如下格式:gs://bucket/object
或 https://firebasestorage.googleapis.com/v0/b/bucket/o/object?token=<TOKEN>
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-22。
[null,null,["最后更新时间 (UTC):2025-08-22。"],[],[],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`"]]