在将文件上传到 Cloud Storage 引用之后,您还可以获取和更新文件元数据,例如查看或更新内容类型。文件还可以使用额外的文件元数据来存储自定义键值对。
注意:默认情况下,需要经过 Firebase Authentication 身份验证才能对 Cloud Storage 存储分区中的数据或文件执行任何操作。您可以更改 Cloud Storage 的 Firebase 安全规则以允许未经身份验证的访问。由于 Firebase 和项目的默认 App Engine 应用共用此存储分区,因此配置公开访问权限可能会使新上传的 App Engine 文件也可被公开访问。设置 Authentication 时,请务必重新限制对 Cloud Storage 存储分区的访问权限。
获取文件元数据
文件元数据包含 name、size 和 contentType(通常称为 MIME 类型)等常用属性;以及 contentDisposition 和 timeCreated 等不太常用的属性。您可以使用 getMetadata() 方法从 Cloud Storage 引用中检索此元数据。
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() {
@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!
}
});
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!
}
更新文件元数据
文件上传完成后,您可以使用 updateMetadata() 方法随时更新文件元数据。请参考完整清单,详细了解哪些属性可以更新。只有元数据中指定的属性才会更新,所有其他属性不会被修改。
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() {
@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!
}
});
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!
}
对于可写元数据属性,您可以通过向其传递 null 来将其删除:
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() {
@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!
}
});
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!
}
处理错误
获取或更新元数据时,有很多原因会引发错误,包括文件不存在或者用户无权访问相关的文件。如需了解有关错误的详细信息,请参阅文档的处理错误部分。
自定义元数据
您可以使用 StorageMetadata.Builder 类中的 setCustomMetadata() 方法指定自定义元数据。
Java
StorageMetadata metadata = new StorageMetadata.Builder()
.setCustomMetadata("location", "Yosemite, CA, USA")
.setCustomMetadata("activity", "Hiking")
.build();
Kotlin+KTX
val metadata = storageMetadata {
setCustomMetadata("location", "Yosemite, CA, USA")
setCustomMetadata("activity", "Hiking")
}
您可以在自定义元数据中为每个文件存储应用专属的数据,不过我们强烈建议您使用数据库(例如 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
否
上传、下载和更新文件很重要,但能够将文件移除也同样重要。请了解如何从 Cloud Storage 中删除文件。