將檔案中繼資料與網頁版 Cloud Storage 搭配使用

將檔案上傳至 Cloud Storage 參考資料後,也能取得下列權限: 或更新檔案中繼資料,例如更新內容類型。檔案 也可以儲存自訂鍵/值組合,以及額外的檔案中繼資料。

取得檔案中繼資料

檔案中繼資料包含常用屬性,例如 namesizecontentType (通常稱為 MIME 類型) contentDispositiontimeCreated 等常用屬性。中繼資料可以是 從 Cloud Storage 參考資料中擷取 getMetadata() 方法。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 即時資料庫) 儲存和同步處理這種類型的資料

檔案中繼資料屬性

完整的檔案中繼資料屬性清單請見下方:

屬性 類型 可寫入
bucket 字串
generation 字串
metageneration 字串
fullPath 字串
name 字串
size 數字
timeCreated 字串
updated 字串
md5Hash 字串 上傳時為「是」,更新中繼資料時沒有
cacheControl 字串
contentDisposition 字串
contentEncoding 字串
contentLanguage 字串
contentType 字串
customMetadata 包含 string->字串對應關係的物件

上傳、下載及更新檔案很重要 移除。現在來瞭解如何 刪除檔案 從 Cloud Storage 執行