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

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

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

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

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

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