שימוש במטא-נתונים של קבצים ב-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<std::string, std::string>* 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 thought std::vector<std::string> לא
custom_metadata <std::string, std::string>std::map כן

השלבים הבאים

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