Catch up on highlights from Firebase at Google I/O 2023. Learn more

將文件元數據與 Web 上的 Cloud Storage 結合使用

將文件上傳到 Cloud Storage 引用後,您還可以獲取或更新文件元數據,例如更新內容類型。文件還可以存儲帶有附加文件元數據的自定義鍵/值對。

獲取文件元數據

文件元數據包含常見屬性,例如namesizecontentType (通常稱為 MIME 類型)以及一些不太常見的屬性,例如contentDispositiontimeCreated 。可以使用getMetadata()方法從 Cloud Storage 引用中檢索此元數據。 getMetadata()返回包含完整元數據的Promise ,如果Promise拒絕則返回錯誤。

Web modular API

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 namespaced API

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

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 namespaced API

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

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 namespaced API

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

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

Web namespaced API

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包含字符串->字符串映射的對象是的

上傳、下載和更新文件很重要,但刪除它們也很重要。讓我們學習如何從雲存儲中刪除文件