הקבצים שלך מאוחסנים בדלי של Cloud Storage . הקבצים בדלי זה מוצגים במבנה היררכי, בדיוק כמו מערכת הקבצים בדיסק הקשיח המקומי שלך, או הנתונים במסד הנתונים של Firebase Realtime. על ידי יצירת הפניה לקובץ, האפליקציה שלך מקבלת גישה אליו. לאחר מכן ניתן להשתמש בהפניות אלה כדי להעלות או להוריד נתונים, לקבל או לעדכן מטא נתונים או למחוק את הקובץ. הפניה יכולה להצביע על קובץ ספציפי או על צומת ברמה גבוהה יותר בהיררכיה.
אם השתמשת ב- Firebase Realtime Database , נתיבים אלה אמורים להיראות לך מוכרים מאוד. עם זאת, נתוני הקבצים שלך מאוחסנים ב-Cloud Storage, לא במסד הנתונים בזמן אמת.
צור הפניה
צור הפניה להעלאה, הורדה או מחיקה של קובץ, או כדי לקבל או לעדכן את המטא נתונים שלו. ניתן לחשוב על הפניה כמצביע לקובץ בענן. הפניות הן קלות משקל, כך שתוכל ליצור כמה שאתה צריך. הם גם ניתנים לשימוש חוזר עבור פעולות מרובות.
צור הפניה באמצעות המופע של FirebaseStorage
singleton וקריאה לשיטת 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 חוקי, אך מוטלות הגבלות מסוימות כולל:
- האורך הכולל של reference.fullPath חייב להיות בין 1 ל-1024 בתים בעת קידוד 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.