אחרי שמעלים קובץ ל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 Futurefuture = 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 Futurefuture = 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 Futurefuture = 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 | std::vector<std::string> | לא | 
| custom_metadata | std::map<std::string, std::string> | כן | 
השלבים הבאים
העלאה, הורדה ועדכון של קבצים הם חשובים, אבל חשוב גם להיות מסוגלים להסיר אותם. במאמר הזה נסביר איך למחוק קבצים מ-Cloud Storage.