הקבצים מאוחסנים בקטגוריה של Cloud Storage for Firebase. הקבצים בקטגוריה הזו מוצגים במבנה היררכי, בדיוק כמו מערכת הקבצים בדיסק הקשיח המקומי.
כשיוצרים הפניה לקובץ, האפליקציה מקבלת גישה אליו. הפניה יכולה להצביע על קובץ ספציפי או על צומת ברמה גבוהה יותר בהיררכיה. אפשר לחשוב על הפניה כעל מצביע לקובץ או לצומת בענן. אחרי שיוצרים הפניה, אפשר להשתמש בה כדי לבצע פעולות (כמו העלאה או הורדה של נתונים, קבלה או עדכון של מטא-נתונים או מחיקה של הקובץ).
בדף הזה מוסבר איך להשתמש ב-SDK של Cloud Storage for Firebase כדי ליצור הפניות ולעבוד איתן.
יצירת קובץ עזר
כדי לבצע פעולות בקובץ, קודם צריך ליצור הפניה לקובץ. ההפניות הן קלות משקל, כך שאפשר ליצור כמה שצריך. אפשר גם להשתמש בהם שוב לכמה פעולות.
יוצרים הפניה באמצעות המכונה היחידה (singleton) של FirebaseStorage וקוראים ל-method 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 lets you move your reference to a parent node
// imagesRef2 now points to 'images'
final imagesRef2 = spaceRef.parent;
// root lets you 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 תקפים, אבל יש כמה הגבלות, כולל:
- האורך הכולל של reference.fullPath צריך להיות בין בייט אחד ל-1,024 בייטים בקידוד UTF-8.
- אסור להשתמש בתווים של חזרה לתחילת השורה או של מעבר שורה.
- הימנעו משימוש ב-
#,[,],*או?, כי הם לא פועלים היטב עם כלים אחרים כמו Firebase Realtime Database או 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