הקבצים מאוחסנים בקטגוריה של Cloud Storage. הקבצים בקטגוריה הזו מוצגים במבנה היררכי, בדיוק כמו מערכת הקבצים בדיסק הקשיח המקומי או הנתונים ב-Firebase Realtime Database. כשיוצרים הפניה לקובץ, האפליקציה מקבלת גישה אליו. אפשר להשתמש בהפניות האלה כדי להעלות או להוריד נתונים, לקבל או לעדכן מטא-נתונים או למחוק את הקובץ. ההפניה יכולה להצביע על קובץ ספציפי או על צומת ברמה גבוהה יותר בהיררכיה.
אם השתמשתם ב-Firebase Realtime Database, הנתיבים האלה אמורים להיות מוכרים לכם. עם זאת, נתוני הקבצים מאוחסנים ב-Cloud Storage, ולא ב-Realtime Database.
יצירת קובץ עזר
יוצרים הפניה כדי להעלות, להוריד או למחוק קובץ, או כדי לקבל או לעדכן את המטא-נתונים שלו. אפשר לחשוב על הפניה כעל מצביע לקובץ בענן. ההפניות הן קלות משקל, כך שאפשר ליצור כמה שצריך. אפשר גם להשתמש בהם שוב לכמה פעולות.
הפניות נוצרות מהשירות storage באפליקציית Firebase על ידי קריאה ל-method GetReferenceFromUrl() והעברת כתובת URL בפורמט gs://<your-cloud-storage-bucket>. אפשר למצוא את כתובת ה-URL הזו בקטע Storage במסוף Firebase.
// Get a reference to the storage service, using the default Firebase App Storage* storage = Storage::GetInstance(app); // Create a Cloud Storage reference from our storage service StorageReference storage_ref = storage->GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");
אפשר ליצור הפניה למיקום נמוך יותר בעץ, למשל 'images/space.jpg', באמצעות השיטה child בהפניה קיימת.
// Create a child reference // images_ref now points to "images" StorageReference images_ref = storage_ref.Child("images"); // Child references can also take paths delimited by '/' // space_ref now points to "images/space.jpg" // images_ref still points to "images" StorageReference space_ref = storage_ref.Child("images/space.jpg"); // This is equivalent to creating the full reference StorageReference space_ref = storage.GetReferenceFromUrl("gs://<your-cloud-storage-bucket>/images/space.jpg");
ניווט באמצעות הפניות
אפשר גם להשתמש בשיטות Parent ו-Root כדי לעבור למעלה בהיררכיית הקבצים. Parent מעביר רמה אחת למעלה, ו-Root מעביר עד לרמה העליונה.
// Parent allows us to move to the parent of a reference // images_ref now points to 'images' StorageReference images_ref = space_ref.Parent(); // Root allows us to move all the way back to the top of our bucket // root_ref now points to the root StorageReference root_ref = space_ref.Root();
אפשר לשרשר את Child, Parent ו-Root כמה פעמים, כי כל אחת מהן מחזירה הפניה. החריג הוא Parent של Root, שהוא StorageReference לא חוקי.
// References can be chained together multiple times // earth_ref points to "images/earth.jpg" StorageReference earth_ref = space_ref.Parent().Child("earth.jpg"); // null_ref is null, since the parent of root is an invalid StorageReference StorageReference null_ref = space_ref.Root().Parent();
שיטות עזר
כדי להבין טוב יותר את הקבצים שההפניות מצביעות עליהם, אתם יכולים לבדוק את ההפניות באמצעות השיטות full_path, name ו-bucket. השיטות האלה מקבלות את הנתיב המלא, השם והקטגוריה של הקובץ.
// Reference's path is: "images/space.jpg" // This is analogous to a file path on disk space_ref.full_path(); // Reference's name is the last segment of the full path: "space.jpg" // This is analogous to the file name space_ref.name(); // Reference's bucket is the name of the Cloud Storage bucket where files are stored space_ref.bucket();
מגבלות על קובצי עזר
הנתיבים והשמות של קובצי העזר יכולים להכיל כל רצף של תווי Unicode תקינים, אבל יש כמה הגבלות, כולל:
- האורך הכולל של reference.fullPath צריך להיות בין בייט אחד ל-1,024 בייטים בקידוד UTF-8.
- אסור להשתמש בתווים של חזרה לתחילת השורה או מעבר שורה.
- אל תשתמשו בתווים
#,[,],*או?, כי הם לא פועלים היטב עם כלים אחרים כמו Firebase Realtime Database או gsutil.
דוגמה מלאה
Storage* storage = Storage::GetInstance(app); // Points to the root reference StorageReference storage_ref = storage->GetReferenceFromUrl("gs://<your-bucket-name>"); // Points to "images" StorageReference images_ref = storage_ref.Child("images"); // Points to "images/space.jpg" // Note that you can use variables to create child values std::string filename = "space.jpg"; StorageReference space_ref = images_ref.Child(filename); // File path is "images/space.jpg" std::string path = space_ref.full_path() // File name is "space.jpg" std::string name = space_ref.name() // Points to "images" StorageReference images_ref = space_ref.Parent();
השלבים הבאים
עכשיו נלמד איך להעלות קבצים אל Cloud Storage.