使用 Cloud Storage 中的文件元数据 (Android)

在将文件上传到 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
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 中删除文件。