הקבצים מאוחסנים בקטגוריה של Cloud Storage for Firebase. הקבצים בקטגוריה הזו מוצגים במבנה היררכי, בדיוק כמו מערכת הקבצים בדיסק הקשיח המקומי.
כשיוצרים הפניה לקובץ, האפליקציה מקבלת גישה אליו. הפניה יכולה להצביע על קובץ ספציפי או על צומת ברמה גבוהה יותר בהיררכיה. אפשר לחשוב על הפניה כעל מצביע לקובץ או לצומת בענן. אחרי שיוצרים הפניה, אפשר להשתמש בה כדי לבצע פעולות (כמו העלאה או הורדה של נתונים, קבלה או עדכון של מטא-נתונים או מחיקה של הקובץ).
בדף הזה מוסבר איך להשתמש ב-SDK של Cloud Storage for Firebase כדי ליצור הפניות ולעבוד איתן.
יצירת קובץ עזר
כדי לבצע פעולות בקובץ, קודם צריך ליצור הפניה לקובץ. ההפניות הן קלות משקל, כך שאפשר ליצור כמה שצריך. אפשר גם להשתמש בהם שוב לכמה פעולות.
הפניות נוצרות מהשירות 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 the 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 lets you move to the parent of a reference // images_ref now points to 'images' StorageReference images_ref = space_ref.Parent(); // Root lets you 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.