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

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

取得檔案中繼資料

檔案中繼資料包含常見屬性,例如 name、size 和 contentType (通常稱為 MIME 類型),以及一些較不常見的屬性,例如 contentDisposition 和 timeCreated。您可以使用 getMetadata() 方法,從 Cloud Storage 參照擷取這項中繼資料。

Kotlin

// 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

// 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

// 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

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) 儲存及同步這類資料。

檔案中繼資料屬性

檔案的中繼資料屬性完整清單如下:

屬性 Getter 類型 Setter Exists
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刪除檔案。