通过 Cloud Storage 使用文件元数据 (Web)

在将文件上传到 Cloud Storage 引用之后,您还可以获取或更新文件元数据,例如更新内容类型。文件还可以使用额外的文件元数据来存储自定义键值对。

获取文件元数据

文件元数据包含 namesizecontentType(通常称为 MIME 类型)等常用属性,以及 contentDispositiontimeCreated 等不太常用的属性。您可以使用 getMetadata() 方法从 Cloud Storage 引用中检索此元数据。 getMetadata() 会返回包含完整元数据的 Promise,如果 Promise 拒绝,则返回错误。

Web

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

// 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

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

// 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

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

// 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

const metadata = {
  customMetadata: {
    'location': 'Yosemite, CA, USA',
    'activity': 'Hiking'
  }
};

Web

var metadata = {
  customMetadata: {
    'location': 'Yosemite, CA, USA',
    'activity': 'Hiking'
  }
};

您可以使用自定义元数据为每个文件存储额外的应用专属数据,不过我们强烈建议您使用数据库(例如 Firebase Realtime Database)来存储和同步这种类型的数据。

文件元数据属性

文件元数据属性的完整列表如下:

属性类型是否可写入
bucket字符串
generation字符串
metageneration字符串
fullPath字符串
name字符串
size数值
timeCreated字符串
updated字符串
md5Hash字符串上传时可以,更新元数据时不可以
cacheControl字符串
contentDisposition字符串
contentEncoding字符串
contentLanguage字符串
contentType字符串
customMetadata包含字符串到字符串映射的对象

上传、下载和更新文件很重要,但能够将其移除也同样重要。请了解如何从 Cloud Storage删除文件