將檔案上傳至 Cloud Storage 參考檔案後,您也可以取得或更新檔案中繼資料,例如更新內容類型。檔案也可以儲存自訂鍵/值組合,以及其他檔案中繼資料。
取得檔案中繼資料
檔案中繼資料包含 name、size 和 contentType (通常稱為 MIME 類型) 等常見屬性,以及 contentDisposition 和 timeCreated 等較不常見的屬性。您可以使用 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 |
字串 | YES (上傳),NO (updateMetadata) |
cacheControl |
字串 | 是 |
contentDisposition |
字串 | 是 |
contentEncoding |
字串 | 是 |
contentLanguage |
字串 | 是 |
contentType |
字串 | 是 |
customMetadata |
包含字串對應關係的物件 | 是 |
上傳、下載和更新檔案固然重要,但移除檔案也同樣重要。接下來,我們將說明如何從 Cloud Storage 刪除檔案。