将文件上传到 Cloud Storage 引用后,您还可以获取或更新文件元数据,例如更新内容类型。文件还可以存储带有附加文件元数据的自定义键/值对。
获取文件元数据
文件元数据包含常见属性,例如name
、 size
和contentType
(通常称为 MIME 类型)以及一些不太常见的属性,例如contentDisposition
和timeCreated
。可以使用getMetadata()
方法从 Cloud Storage 引用中检索此元数据。 getMetadata()
返回包含完整元数据的Promise
,如果Promise
拒绝则返回错误。
Web version 9
import { getStorage, ref, getMetadata } from "firebase/storage"; // Create a reference to the file whose metadata we want to retrieve const storage = getStorage(); const forestRef = ref(storage, 'images/forest.jpg'); // Get metadata properties getMetadata(forestRef) .then((metadata) => { // Metadata now contains the metadata for 'images/forest.jpg' }) .catch((error) => { // Uh-oh, an error occurred! });
Web version 8
// Create a reference to the file whose metadata we want to retrieve var forestRef = storageRef.child('images/forest.jpg'); // Get metadata properties forestRef.getMetadata() .then((metadata) => { // Metadata now contains the metadata for 'images/forest.jpg' }) .catch((error) => { // Uh-oh, an error occurred! });
更新文件元数据
您可以在文件上传完成后随时使用updateMetadata()
方法更新文件元数据。有关可以更新哪些属性的更多信息,请参阅完整列表。仅更新元数据中指定的属性,所有其他属性保持不变。 updateMetadata()
返回包含完整元数据的Promise
,如果Promise
拒绝则返回错误。
Web version 9
import { getStorage, ref, updateMetadata } from "firebase/storage"; // Create a reference to the file whose metadata we want to change const storage = getStorage(); const forestRef = ref(storage, 'images/forest.jpg'); // Create file metadata to update const newMetadata = { cacheControl: 'public,max-age=300', contentType: 'image/jpeg' }; // Update metadata properties updateMetadata(forestRef, newMetadata) .then((metadata) => { // Updated metadata for 'images/forest.jpg' is returned in the Promise }).catch((error) => { // Uh-oh, an error occurred! });
Web version 8
// Create a reference to the file whose metadata we want to change var forestRef = storageRef.child('images/forest.jpg'); // Create file metadata to update var newMetadata = { cacheControl: 'public,max-age=300', contentType: 'image/jpeg' }; // Update metadata properties forestRef.updateMetadata(newMetadata) .then((metadata) => { // Updated metadata for 'images/forest.jpg' is returned in the Promise }).catch((error) => { // Uh-oh, an error occurred! });
您可以通过将元数据属性设置为null
来删除它:
Web version 9
import { getStorage, ref, updateMetadata } from "firebase/storage"; const storage = getStorage(); const forestRef = ref(storage, 'images/forest.jpg'); // Create file metadata with property to delete const deleteMetadata = { contentType: null }; // Delete the metadata property updateMetadata(forestRef, deleteMetadata) .then((metadata) => { // metadata.contentType should be null }).catch((error) => { // Uh-oh, an error occurred! });
Web version 8
// Create file metadata with property to delete var deleteMetadata = { contentType: null }; // Delete the metadata property forestRef.updateMetadata(deleteMetadata) .then((metadata) => { // metadata.contentType should be null }).catch((error) => { // Uh-oh, an error occurred! });
处理错误
获取或更新元数据时可能发生错误的原因有很多,包括文件不存在,或者用户无权访问所需文件。有关错误的更多信息,请参阅文档的处理错误部分。
自定义元数据
您可以将自定义元数据指定为包含String
属性的对象。
Web version 9
const metadata = { customMetadata: { 'location': 'Yosemite, CA, USA', 'activity': 'Hiking' } };
Web version 8
var metadata = { customMetadata: { 'location': 'Yosemite, CA, USA', 'activity': 'Hiking' } };
您可以使用自定义元数据为每个文件存储额外的应用特定数据,但我们强烈建议使用数据库(例如Firebase 实时数据库)来存储和同步此类数据。
文件元数据属性
下面提供了文件元数据属性的完整列表:
财产 | 类型 | 可写 |
---|---|---|
bucket | 细绳 | 不 |
generation | 细绳 | 不 |
metageneration | 细绳 | 不 |
fullPath | 细绳 | 不 |
name | 细绳 | 不 |
size | 数字 | 不 |
timeCreated | 细绳 | 不 |
updated | 细绳 | 不 |
md5Hash | 细绳 | 上传时是,更新元数据时否 |
cacheControl | 细绳 | 是的 |
contentDisposition | 细绳 | 是的 |
contentEncoding | 细绳 | 是的 |
contentLanguage | 细绳 | 是的 |
contentType | 细绳 | 是的 |
customMetadata | 包含字符串->字符串映射的对象 | 是的 |
上传、下载和更新文件很重要,但删除它们也很重要。让我们学习如何从云存储中删除文件。