יצירת חומר עזר של Cloud Storage באמצעות Cloud Storage ל-C++

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

אם השתמשתם ב-Firebase Realtime Database, הנתיבים האלה אמורים להיות מוכרים לכם. עם זאת, נתוני הקובץ מאוחסנים ב-Cloud Storage, לא ב-Realtime Database.

יצירת קובץ עזר

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

ההפניות נוצרות מהשירות storage באפליקציית Firebase שלכם על ידי קריאה לשיטה GetReferenceFromUrl() והעברת כתובת URL מהצורה gs://<your-cloud-storage-bucket>. אפשר למצוא את כתובת ה-URL הזו בקטע אחסון במסוף 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 תקפים, אבל יש הגבלות מסוימות, כולל:

  1. האורך הכולל של reference.fullPath צריך להיות בין 1 ל-1,024 בייטים בקידוד UTF-8.
  2. אסור להשתמש בתו של חזרה לתחילת השורה או בתו של מעבר שורה.
  3. מומלץ להימנע משימוש בפונקציות #,‏ [,‏ ],‏ * או ?, כי הן לא פועלות טוב עם כלים אחרים כמו 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.