يتم تخزين ملفاتك في حزمة Cloud Storage for Firebase. يتم عرض الملفات في هذا المستودع في بنية هرمية، تمامًا مثل نظام الملفات على القرص الصلب المحلي.
من خلال إنشاء مرجع لملف، يحصل تطبيقك على إذن الوصول إليه. يمكن أن يشير المرجع إما إلى ملف معيّن أو إلى عقدة ذات مستوى أعلى في التسلسل الهرمي. يمكنك التفكير في المرجع كمؤشر للملف أو العقدة في السحابة. بعد إنشاء مرجع، يمكنك استخدامه لإجراء عمليات (مثل تحميل البيانات أو تنزيلها أو الحصول على البيانات الوصفية أو تعديلها أو حذف الملف).
توضّح هذه الصفحة كيفية استخدام حزمة تطوير البرامج (SDK) لـ Cloud Storage for Firebase لإنشاء المراجع والتعامل معها.
إنشاء مرجع
لتنفيذ عمليات على ملف، يجب أولاً إنشاء مرجع للملف. تتميز المراجع بأنّها خفيفة، لذا يمكنك إنشاء أي عدد تريده منها. كما أنها قابلة لإعادة الاستخدام لعمليات متعددة.
أنشئ مرجعًا باستخدام مثيل 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 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 و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.