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

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

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

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

// Create reference to the file whose metadata we want to retrieve
final forestRef = storageRef.child("images/forest.jpg");

// Get metadata properties
final metadata = await forestRef.getMetadata();

// Metadata now contains the metadata for 'images/forest.jpg'

עדכן מטא נתונים של קובץ

אתה יכול לעדכן מטא נתונים של קובץ בכל עת לאחר השלמת העלאת הקובץ באמצעות שיטת updateMetadata() . עיין ברשימה המלאה למידע נוסף על אילו נכסים ניתן לעדכן. רק המאפיינים שצוינו במטא נתונים מתעדכנים, כל השאר נותרים ללא שינוי.

// Create reference to the file whose metadata we want to change
final forestRef = storageRef.child("images/forest.jpg");

// Create file metadata to update
final newMetadata = SettableMetadata(
  cacheControl: "public,max-age=300",
  contentType: "image/jpeg",
);

// Update metadata properties
final metadata = await forestRef.updateMetadata(newMetadata);

// Updated metadata for 'images/forest.jpg' is returned

אתה יכול למחוק מאפייני מטא נתונים הניתנים לכתיבה על ידי העברת null :

// Delete the cacheControl property
final newMetadata = SettableMetadata(cacheControl: null);
final metadata = await forestRef.updateMetadata(newMetadata);

טיפול בשגיאות

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

מטא נתונים מותאמים אישית

אתה יכול לציין מטא נתונים מותאמים אישית באמצעות הפרמטר customMetadata של בנאי SettableMetadata :

// Create reference to the file whose metadata we want to change
final forestRef = storageRef.child("images/forest.jpg");

// Create file metadata to update
final newCustomMetadata = SettableMetadata(
  customMetadata: {
    "location": "Yosemite, CA, USA",
    "activity": "Hiking",
  },
);

// Update metadata properties
final metadata = await forestRef.updateMetadata(newCustomMetadata);

// Updated metadata for 'images/forest.jpg' is returned

אתה יכול לאחסן נתונים ספציפיים לאפליקציה עבור כל קובץ במטא נתונים מותאמים אישית, אך אנו ממליצים בחום להשתמש במסד נתונים (כגון Firebase Realtime Database ) כדי לאחסן ולסנכרן סוג זה של נתונים.

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

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

תכונה סוּג ניתן להגדרה?
bucket String לא
generation String לא
metageneration String לא
metadataGeneration String לא
fullPath String לא
name String לא
size int לא
timeCreated DateTime לא
updated DateTime לא
md5Hash String לא
cacheControl String כן
contentDisposition String כן
contentEncoding String כן
contentLanguage String כן
contentType String כן
customMetadata Map<String, String> כן

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