צור הפניה ל-Cloud Storage עם Cloud Storage for Unity

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

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

צור הפניה

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

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