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

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

取得檔案中繼資料

檔案中繼資料包含常用屬性,例如 namesizecontentType (通常稱為 MIME 類型) contentDispositiontimeCreated 等常用屬性。中繼資料可以是 從 Cloud Storage 參考資料中擷取 getMetadata() 方法。

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!
            }
        });

處理錯誤

發生錯誤的可能原因有很多, 中繼資料,包括不存在的檔案,或使用者沒有權限 來存取所需檔案如要進一步瞭解錯誤,請前往 處理錯誤 一節。

自訂中繼資料

您可以使用 setCustomMetadata() 方法 (位於以下位置) 指定自訂中繼資料: StorageMetadata.Builder 類別。

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 執行