Android の Cloud Storage でファイル メタデータを使用する

ファイルを Cloud Storage 参照にアップロードした後で、ファイル メタデータを取得、更新して、コンテンツ タイプの表示や更新などを行うことができます。ファイルには、追加のファイル メタデータを使用してカスタム Key-Value ペアを格納することもできます。

ファイル メタデータを取得する

ファイル メタデータには、namesizecontentType(多くの場合 MIME タイプと呼ばれます)などの一般的なプロパティに加え、contentDispositiontimeCreated などのあまり一般的でないプロパティも含まれます。このメタデータは、getMetadata() メソッドを使用して Cloud Storage 参照から取得できます。

Kotlin+KTX

// Create a storage reference from our app
val storageRef = storage.reference

// Get reference to the file
val forestRef = storageRef.child("images/forest.jpg")
forestRef.metadata.addOnSuccessListener { metadata ->
    // Metadata now contains the metadata for 'images/forest.jpg'
}.addOnFailureListener {
    // Uh-oh, an error occurred!
}

Java

// Create a storage reference from our app
StorageReference storageRef = storage.getReference();

// Get reference to the file
StorageReference forestRef = storageRef.child("images/forest.jpg");
forestRef.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
    @Override
    public void onSuccess(StorageMetadata storageMetadata) {
        // Metadata now contains the metadata for 'images/forest.jpg'
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Uh-oh, an error occurred!
    }
});

ファイル メタデータを更新する

ファイルのアップロードが完了したら、updateMetadata() メソッドを使用して、いつでもファイル メタデータを更新できます。更新できるプロパティについて詳しくは、全一覧をご覧ください。メタデータに指定されているプロパティのみ更新され、それ以外のプロパティはすべて変更されずに残ります。

Kotlin+KTX

// Create a storage reference from our app
val storageRef = storage.reference

// Get reference to the file
val forestRef = storageRef.child("images/forest.jpg")
// Create file metadata including the content type
val metadata = storageMetadata {
    contentType = "image/jpg"
    setCustomMetadata("myCustomProperty", "myValue")
}

// Update metadata properties
forestRef.updateMetadata(metadata).addOnSuccessListener { updatedMetadata ->
    // Updated metadata is in updatedMetadata
}.addOnFailureListener {
    // Uh-oh, an error occurred!
}

Java

// Create a storage reference from our app
StorageReference storageRef = storage.getReference();

// Get reference to the file
StorageReference forestRef = storageRef.child("images/forest.jpg");
// Create file metadata including the content type
StorageMetadata metadata = new StorageMetadata.Builder()
        .setContentType("image/jpg")
        .setCustomMetadata("myCustomProperty", "myValue")
        .build();

// Update metadata properties
forestRef.updateMetadata(metadata)
        .addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
            @Override
            public void onSuccess(StorageMetadata storageMetadata) {
                // Updated metadata is in storageMetadata
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Uh-oh, an error occurred!
            }
        });

書き込み可能なメタデータ プロパティを削除するには、次のように null を渡します。

Kotlin+KTX

// Create file metadata with property to delete
val metadata = storageMetadata {
    contentType = null
}

// Delete the metadata property
forestRef.updateMetadata(metadata).addOnSuccessListener { updatedMetadata ->
    // updatedMetadata.contentType should be null
}.addOnFailureListener {
    // Uh-oh, an error occurred!
}

Java

// Create file metadata with property to delete
StorageMetadata metadata = new StorageMetadata.Builder()
        .setContentType(null)
        .build();

// Delete the metadata property
forestRef.updateMetadata(metadata)
        .addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
            @Override
            public void onSuccess(StorageMetadata storageMetadata) {
                // metadata.contentType should be null
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Uh-oh, an error occurred!
            }
        });

エラーを処理する

メタデータの取得または更新時にエラーが発生する理由として、ファイルが存在しない、目的のファイルのアクセス権がユーザーにないなど、多くの理由が考えられます。エラーについて詳しくは、このドキュメントのエラーの処理のセクションをご覧ください。

カスタム メタデータ

カスタム メタデータを指定するには、StorageMetadata.Builder クラスの setCustomMetadata() メソッドを使用します。

Kotlin+KTX

val metadata = storageMetadata {
    setCustomMetadata("location", "Yosemite, CA, USA")
    setCustomMetadata("activity", "Hiking")
}

Java

StorageMetadata metadata = new StorageMetadata.Builder()
        .setCustomMetadata("location", "Yosemite, CA, USA")
        .setCustomMetadata("activity", "Hiking")
        .build();

各ファイルについてアプリ固有のデータをカスタム メタデータに格納することは可能ですが、このタイプのデータを格納して同期するには、データベース(Firebase Realtime Database など)を使用することを強くおすすめします。

ファイル メタデータのプロパティ

ファイルに関するメタデータ プロパティの全一覧は次のとおりです。

プロパティ ゲッター セッターの有無
getBucket String ×
getGeneration String いいえ
getMetadataGeneration String いいえ
getPath String いいえ
getName String いいえ
getSizeBytes long いいえ
getCreationTimeMillis long いいえ
getUpdatedTimeMillis long いいえ
getMd5Hash String ×
getCacheControl String
getContentDisposition String
getContentEncoding String
getContentLanguage String
getContentType String
getCustomMetadata String
getCustomMetadataKeys Set<String> ×

ファイルのアップロード、ダウンロード、更新は重要ですが、ファイルを削除できることも重要です。Cloud Storage からファイルを削除する方法を学習しましょう。