处理 Cloud Storage 错误 (Flutter)
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在您构建应用时,事情有时并不会按预期发展,而是会发生错误。
如有疑问,请捕获函数抛出的异常,并查看错误消息的内容。
final storageRef = FirebaseStorage.instance.ref().child("files/uid");
try {
final listResult = await storageRef.listAll();
} on FirebaseException catch (e) {
// Caught an exception from Firebase.
print("Failed with error '${e.code}': ${e.message}");
}
处理错误消息
很多原因都可能导致错误发生,包括文件不存在、用户没有访问所需文件的权限,或者用户取消文件上传操作。
要正确诊断问题并处理错误,请查看下面的完整列表,了解我们的客户端可能报告的所有错误以及错误发生的原因。
代码 |
说明 |
storage/unknown |
发生未知错误。 |
storage/object-not-found |
指定的引用中没有任何对象。 |
storage/bucket-not-found |
没有为 Cloud Storage 配置任何存储桶。 |
storage/project-not-found |
没有为 Cloud Storage 配置任何项目。 |
storage/quota-exceeded |
已超过您的 Cloud Storage 存储桶的配额。如果您使用的是 Spark 定价方案,不妨考虑升级到随用随付 Blaze 定价方案。如果您已采用 Blaze 定价方案,请与 Firebase 支持团队联系。
重要提示:自 2025 年 10 月 1 日起,必须采用 Blaze 定价方案才能使用 Cloud Storage,即使是默认存储桶也是如此。 |
storage/unauthenticated |
用户未经过身份验证,请先进行身份验证,然后重试。 |
storage/unauthorized |
用户无权执行所需的操作,请检查您的安全规则以确保其正确无误。 |
storage/retry-limit-exceeded |
已超出某项操作(上传、下载、删除等)的最长时间限制。请尝试重新上传。 |
storage/invalid-checksum |
客户端上的文件与服务器收到的文件的校验和不匹配。请尝试重新上传。 |
storage/canceled |
用户已取消操作。 |
storage/invalid-event-name |
提供的事件名称无效。必须是以下各项之一:running 、progress 、pause 。 |
storage/invalid-url |
提供给 refFromURL() 的网址无效。必须采用 gs://bucket/object 或 https://firebasestorage.googleapis.com/v0/b/bucket/o/object?token=<TOKEN> 格式。 |
storage/invalid-argument |
传递给 put() 的参数必须是 File 、Blob 或 UInt8 数组。传递给 putString() 的参数必须是原始字符串、Base64 或 Base64URL 字符串。 |
storage/no-default-bucket |
您的配置的 storageBucket 属性中未设置任何存储桶。 |
storage/cannot-slice-blob |
通常在本地文件发生更改(已删除、已再次保存等)时出现。请在验证文件未发生更改后尝试重新上传。 |
storage/server-file-wrong-size |
客户端上的文件与服务器收到的文件的大小不匹配。请尝试重新上传。 |
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-12。
[null,null,["最后更新时间 (UTC):2025-08-12。"],[],[],null,["\u003cbr /\u003e\n\nSometimes when you're building an app, things don't go as planned and an\nerror occurs!\n\nWhen in doubt, catch the exception thrown by the function\nand see what the error message has to say. \n\n final storageRef = FirebaseStorage.instance.ref().child(\"files/uid\");\n try {\n final listResult = await storageRef.listAll();\n } on FirebaseException catch (e) {\n // Caught an exception from Firebase.\n print(\"Failed with error '${e.code}': ${e.message}\");\n }\n\n| **Note:** By default, a Cloud Storage for Firebase bucket requires Firebase Authentication to perform any action on the bucket's data or files. You can change your Firebase Security Rules for Cloud Storage to [allow unauthenticated access for specific situations](/docs/storage/security/rules-conditions#public). However, for most situations, we strongly recommend [restricting access and setting up robust security rules](/docs/storage/security/get-started) (especially for production apps). Note that if you use Google App Engine and have a default Cloud Storage bucket with a name format of `*.appspot.com`, you may need to consider [how your security rules impact access to App Engine files](/docs/storage/gcp-integration#security-rules-and-app-engine-files).\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\nall the errors our client will raise, and how they occurred.\n\n| Code | Description |\n|----------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `storage/unknown` | An unknown error occurred. |\n| `storage/object-not-found` | No object exists at the desired reference. |\n| `storage/bucket-not-found` | No bucket is configured for Cloud Storage |\n| `storage/project-not-found` | No project is configured for Cloud Storage |\n| `storage/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| `storage/unauthenticated` | User is unauthenticated, please authenticate and try again. |\n| `storage/unauthorized` | User is not authorized to perform the desired action, check your security rules to ensure they are correct. |\n| `storage/retry-limit-exceeded` | The maximum time limit on an operation (upload, download, delete, etc.) has been excceded. Try uploading again. |\n| `storage/invalid-checksum` | File on the client does not match the checksum of the file received by the server. Try uploading again. |\n| `storage/canceled` | User canceled the operation. |\n| `storage/invalid-event-name` | Invalid event name provided. Must be one of \\[`running`, `progress`, `pause`\\] |\n| `storage/invalid-url` | Invalid URL provided to `refFromURL()`. Must be of the form: `gs://bucket/object` or `https://firebasestorage.googleapis.com/v0/b/bucket/o/object?token=\u003cTOKEN\u003e` |\n| `storage/invalid-argument` | The argument passed to `put()` must be `File`, `Blob`, or `UInt8` Array. The argument passed to `putString()` must be a raw, `Base64`, or `Base64URL` string. |\n| `storage/no-default-bucket` | No bucket has been set in your config's `storageBucket` property. |\n| `storage/cannot-slice-blob` | Commonly occurs when the local file has changed (deleted, saved again, etc.). Try uploading again after verifying that the file hasn't changed. |\n| `storage/server-file-wrong-size` | File on the client does not match the size of the file received by the server. Try uploading again. |"]]