יצירת חומר עזר של Cloud Storage ב-Flutter

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

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

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

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

יוצרים הפניה באמצעות מופע היחיד (singleton) של FirebaseStorage ומפעילים את השיטה ref() שלו.

final storageRef = FirebaseStorage.instance.ref();

לאחר מכן, אפשר ליצור הפניה למיקום נמוך יותר בעץ, למשל "images/space.jpg", באמצעות השיטה child() בהפניה קיימת.

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

// Child references can also take paths
// spaceRef now points to "images/space.jpg
// imagesRef still points to "images"
final spaceRef = storageRef.child("images/space.jpg");

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

// parent allows us to move our reference to a parent node
// imagesRef2 now points to 'images'
final imagesRef2 = spaceRef.parent;

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

אפשר לקשר את child(), parent ו-root יחד מאחר שכל אחד מהם הוא הפניה. אבל גישה אל root.parent תוביל לnull.

// References can be chained together multiple times
// earthRef points to 'images/earth.jpg'
final earthRef = spaceRef.parent?.child("earth.jpg");

// nullRef is null, since the parent of root is null
final nullRef = spaceRef.root.parent;

נכסי עזר

אפשר לבדוק קובצי עזר כדי להבין טוב יותר את הקבצים שאליהם הם מפנים באמצעות המאפיינים fullPath, name ו-bucket. הנכסים האלה לקבל את הנתיב המלא, השם והקטגוריה של הקובץ.

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

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

// Reference's bucket is the name of the storage bucket that the files are stored in
spaceRef.bucket;

מגבלות על קובצי עזר

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

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

דוגמה מלאה

// Points to the root reference
final storageRef = FirebaseStorage.instance.ref();

// Points to "images"
Reference? imagesRef = storageRef.child("images");

// Points to "images/space.jpg"
// Note that you can use variables to create child values
final fileName = "space.jpg";
final spaceRef = imagesRef.child(fileName);

// File path is "images/space.jpg"
final path = spaceRef.fullPath;

// File name is "space.jpg"
final name = spaceRef.name;

// Points to "images"
imagesRef = spaceRef.parent;

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