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

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

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

מטא-נתונים של קובץ מכילים מאפיינים נפוצים כגון name , size content_type (המכונה לעתים קרובות סוג MIME) בנוסף לכמה פחות נפוצים כמו content_disposition ו- time_created . ניתן לאחזר מטא נתונים אלה מהפניה של Cloud Storage באמצעות שיטת GetMetadata .

// Create reference to the file whose metadata we want to retrieve
StorageReference forest_ref = storage_ref.Child("images/forest.jpg");

// Get metadata properties
Future future = forest_ref.GetMetadata();

// Wait for Future to complete...

if (future.Error() != firebase::storage::kErrorNone) {
  // Uh-oh, an error occurred!
} else {
  // We can now retrieve the metadata for 'images/forest.jpg'
  Metadata* metadata = future.Result();
}

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

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

// Create reference to the file whose metadata we want to change
firebase::storage::StorageReference forest_ref = storage_ref.child("images/forest.jpg");

// Create file metadata to update
Metadata new_metadata;
newMetadata.set_cache_control("public,max-age=300");
newMetadata.set_content_type("image/jpeg");

// Update metadata properties
Future future = forest_ref.UpdateMetadata(new_metadata);

// Wait for Future to complete...

if (future.Error() != firebase::storage::kErrorNone) {
  // Uh-oh, an error occurred!
} else {
  // We can now retrieve the updated metadata for 'images/forest.jpg'
  Metadata* metadata = future.Result();
}

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

// Create file metadata with property to delete
StorageMetadata new_metadata;
new_metadata.set_content_type("");

// Delete the metadata property
Future future = forest_ref.UpdateMetadata(new_metadata);

// Wait for Future to complete...

if (future.Error() != 0) {
  // Uh-oh, an error occurred!
} else {
  // metadata.content_type() should be an empty string
  Metadata* metadata = future.Result();
}

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

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

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

אתה יכול לציין מטא נתונים מותאמים אישית כמפת std::map המכילה מאפייני std::string .

std::map* custom_metadata = metadata.custom_metadata();
custom_metadata->insert(std::make_pair("location", "Yosemite, CA, USA");
custom_metadata->insert(std::make_pair("activity", "Hiking");

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

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

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

תכונה סוּג ניתן לכתיבה
bucket const char* לא
generation const char* לא
metageneration const char* לא
full_path const char* לא
name const char* לא
size int64_t לא
time_created int64_t לא
updated int64_t לא
cache_control const char* כן
content_disposition const char* כן
content_encoding const char* כן
content_language const char* כן
content_type const char* כן
download_urls std::vector<std::string> לא
custom_metadata std::map<std::string, std::string> כן

הצעדים הבאים

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