在 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 即時資料庫)來儲存和同步此類資料。

文件元資料屬性

下面提供了文件元資料屬性的完整清單:

屬性獲取器類型二傳手存在
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>

上傳、下載和更新檔案很重要,但刪除它們的能力也很重要。讓我們了解如何從雲端儲存中刪除檔案