在 Android 上透過 Cloud Storage 使用檔案中繼資料

將檔案上傳至 Cloud Storage 參考資料後,您也可以取得及更新檔案中繼資料,例如查看或更新內容類型。檔案也能儲存自訂鍵/值組合與其他檔案中繼資料。

取得檔案中繼資料

檔案中繼資料包含 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 即時資料庫) 來儲存及同步處理這類資料。

檔案中繼資料屬性

完整的檔案中繼資料屬性清單請見下方:

屬性 getter 類型 設定者存在
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 刪除檔案