Android で Cloud Storage のエラーを処理する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
操作が思いどおりにいかないことや、エラーが発生することがあります。
疑わしいときは、返されたエラーをチェックして、エラー メッセージの内容を確認します。次のコードは、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 日より、デフォルト バケットでも Cloud Storage を使用するには、Blaze 料金プランが必要になります。 |
ERROR_NOT_AUTHENTICATED |
承認されていないユーザーです。認証してから、もう一度お試しください。 |
ERROR_NOT_AUTHORIZED |
要求された操作を行う権限がユーザーにありません。ルールが正しいことをご確認ください。 |
ERROR_RETRY_LIMIT_EXCEEDED |
オペレーション(アップロード、ダウンロード、削除など)の最大時間制限を超えました。もう一度お試しください。 |
ERROR_INVALID_CHECKSUM |
クライアント上のファイルが、サーバーで受信されたファイルのチェックサムと一致しません。もう一度アップロードしてみてください。 |
ERROR_CANCELED |
ユーザーがオペレーションをキャンセルしました。 |
また、無効な URL を指定して getReferenceFromUrl()
を呼び出そうとすると、IllegalArgumentException
がスローされます。上記のメソッドの引数は、gs://bucket/object
または https://firebasestorage.googleapis.com/v0/b/bucket/o/object?token=<TOKEN>
の形式である必要があります。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-22 UTC。
[null,null,["最終更新日 2025-08-22 UTC。"],[],[],null,["# Handle errors for Cloud Storage on Android\n\n\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\n### Kotlin\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\n### Java\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---------------------\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`"]]