השתמש במטא נתונים של קבצים עם Cloud Storage באינטרנט

לאחר העלאת קובץ ל-Cloud Storage הפניה, תוכל גם לקבל או לעדכן את המטא נתונים של הקובץ, למשל כדי לעדכן את סוג התוכן. קבצים יכולים גם לאחסן צמדי מפתח/ערך מותאמים אישית עם מטא נתונים נוספים של קבצים.

קבל מטא נתונים של קבצים

מטא נתונים של קובץ מכילים מאפיינים נפוצים כגון name , size ו- contentType (המכונה לעתים קרובות סוג MIME) בנוסף לכמה פחות נפוצים כמו contentDisposition ו- timeCreated . ניתן לאחזר מטא נתונים אלה מהפניה של Cloud Storage באמצעות שיטת getMetadata() ‎. 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 Realtime Database ) כדי לאחסן ולסנכרן סוג זה של נתונים.

מאפייני מטא נתונים של קובץ

רשימה מלאה של מאפייני מטא נתונים בקובץ זמינה להלן:

תכונה סוּג ניתן לכתיבה
bucket חוּט לא
generation חוּט לא
metageneration חוּט לא
fullPath חוּט לא
name חוּט לא
size מספר לא
timeCreated חוּט לא
updated חוּט לא
md5Hash חוּט כן בהעלאה, לא בעדכוןMetadata
cacheControl חוּט כן
contentDisposition חוּט כן
contentEncoding חוּט כן
contentLanguage חוּט כן
contentType חוּט כן
customMetadata אובייקט המכיל מיפוי מחרוזת->מחרוזת כן

העלאה, הורדה ועדכון של קבצים חשובים, אבל גם היכולת להסיר אותם. בואו ללמוד כיצד למחוק קבצים מ-Cloud Storage.