יצירת הפניה ל-Cloud Storage באמצעות Cloud Storage for Unity

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

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

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

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

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

// Get a reference to the storage service, using the default Firebase App
FirebaseStorage storage = FirebaseStorage.DefaultInstance;

// Create a storage reference from our storage service
StorageReference storageRef =
    storage.GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");

אפשר ליצור קובץ עזר למיקום נמוך יותר בעץ, למשל 'images/space.jpg', באמצעות השיטה child בקובץ עזר קיים.

// Create a child reference
// imagesRef now points to "images"
StorageReference imagesRef = storageRef.Child("images");

// Child references can also take paths delimited by '/' such as:
// "images/space.jpg".
StorageReference spaceRef = imagesRef.Child("space.jpg");
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"

// This is equivalent to creating the full referenced
StorageReference spaceRefFull = 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
// imagesRef now points to 'images'
StorageReference imagesRef = spaceRef.Parent;

// Root allows us to move all the way back to the top of our bucket
// rootRef now points to the root
StorageReference rootRef = spaceRef.Root;

אפשר לשרשר את הפונקציות Child, Parent ו-Root כמה פעמים, כי כל אחת מהן מחזירה הפניה. החריג הוא Parent של Root, שהוא StorageReference לא תקין.

// References can be chained together multiple times
// earthRef points to "images/earth.jpg"
StorageReference earthRef =
    spaceRef.Parent.Child("earth.jpg");

// nullRef is null since the parent of root is an invalid StorageReference
StorageReference nullRef = spaceRef.Root.Parent;

שיטות הפניה

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

// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
string path = spaceRef.Path;

// Reference's name is the last segment of the full path: "space.jpg"
// This is analogous to the file name
string name = spaceRef.Name;

// Reference's bucket is the name of the storage bucket where files are stored
string bucket = spaceRef.Bucket;

מגבלות על הפניות

נתיבי הפניה ושמות יכולים להכיל כל רצף של תווי Unicode תקפים, אבל יש הגבלות מסוימות, כולל:

  1. האורך הכולל של reference.Path צריך להיות בין 1 ל-1,024 בייטים בקידוד UTF-8.
  2. אסור להשתמש בתו של חזרה לתחילת השורה או בתו של מעבר שורה.
  3. מומלץ להימנע משימוש בפונקציות #,‏ [,‏ ],‏ * או ?, כי הן לא פועלות טוב עם כלים אחרים כמו Firebase Realtime Database או gsutil.

דוגמה מלאה

FirebaseStorage storage = FirebaseStorage.DefaultInstance;

// Points to the root reference
StorageReference storageRef =
    storage.GetReferenceFromUrl("gs://<your-bucket-name>");

// Points to "images"
StorageReference imagesRef = storageRef.Child("images");

// Points to "images/space.jpg"
// Note that you can use variables to create child values
string filename = "space.jpg";
StorageReference spaceRef = imagesRef.Child(filename);

// File path is "images/space.jpg"
string path = spaceRef.Path;

// File name is "space.jpg"
string name = spaceRef.Name;

// Points to "images"
StorageReference imagesRef = spaceRef.Parent;

השלבים הבאים

בשלב הבא נלמד איך מעלים קבצים אל Cloud Storage.