يتم تخزين ملفاتك في حاوية Cloud Storage. تظهر الملفات في هذه الحاوية في بنية هرمية، تمامًا مثل نظام الملفات على القرص الثابت المحلي أو البيانات في Firebase Realtime Database. من خلال إنشاء مرجع لملف، يمكن لتطبيقك الوصول إليه. يمكن بعد ذلك استخدام هذه المراجع لتحميل البيانات أو تنزيلها، أو الحصول على البيانات الوصفية أو تعديلها، أو حذف الملف. يمكن أن يشير المرجع إلى ملف معيّن أو إلى عقدة أعلى مستوى في التسلسل الهرمي.
إذا كنت قد استخدمت Firebase Realtime Database، من المفترض أن تبدو لك هذه المسارات مألوفة جدًا. ومع ذلك، يتم تخزين بيانات ملفك في Cloud Storage, وليس في الـ Realtime Database.
إنشاء مرجع
لتحميل الملفات أو تنزيلها أو حذفها أو الحصول على البيانات الوصفية أو تعديلها، يجب إنشاء مرجع للملف الذي تريد إجراء عملية عليه. يمكن اعتبار المرجع مؤشرًا إلى ملف في السحابة الإلكترونية. المراجع خفيفة الوزن، لذا يمكنك إنشاء أي عدد تريده منها، ويمكن أيضًا إعادة استخدامها في عمليات متعددة.
لإنشاء مرجع، احصل على مثيل من خدمة التخزين باستخدام getStorage() ثم استدعِ ref() مع الخدمة كمعلَمة.
يشير هذا المرجع إلى جذر حاوية Cloud Storage.
Web
import { getStorage, ref } from "firebase/storage"; // Get a reference to the storage service, which is used to create references in your storage bucket const storage = getStorage(); // Create a storage reference from our storage service const storageRef = ref(storage);
Web
// Get a reference to the storage service, which is used to create references in your storage bucket var storage = firebase.storage(); // Create a storage reference from our storage service var storageRef = storage.ref();
يمكنك إنشاء مرجع لموقع أدنى في الشجرة،
مثلاً 'images/space.jpg' من خلال تمرير هذا المسار كمعلَمة ثانية عند
استدعاء ref().
Web
import { getStorage, ref } from "firebase/storage"; const storage = getStorage(); // Create a child reference const imagesRef = ref(storage, 'images'); // imagesRef now points to 'images' // Child references can also take paths delimited by '/' const spaceRef = ref(storage, 'images/space.jpg'); // spaceRef now points to "images/space.jpg" // imagesRef still points to "images"
Web
// Create a child reference var imagesRef = storageRef.child('images'); // imagesRef now points to 'images' // Child references can also take paths delimited by '/' var spaceRef = storageRef.child('images/space.jpg'); // spaceRef now points to "images/space.jpg" // imagesRef still points to "images"
التنقّل باستخدام المراجع
يمكنك أيضًا استخدام السمتَين parent وroot للتنقّل لأعلى في التسلسل الهرمي للملفات. تنتقل السمة parent لأعلى مستوى واحد، بينما تنتقل السمة root إلى أعلى مستوى.
Web
import { getStorage, ref } from "firebase/storage"; const storage = getStorage(); const spaceRef = ref(storage, 'images/space.jpg'); // Parent allows us to move to the parent of a reference const imagesRef = spaceRef.parent; // imagesRef now points to 'images' // Root allows us to move all the way back to the top of our bucket const rootRef = spaceRef.root; // rootRef now points to the root
Web
// Parent allows us to move to the parent of a reference var imagesRef = spaceRef.parent; // imagesRef now points to 'images' // Root allows us to move all the way back to the top of our bucket var rootRef = spaceRef.root; // rootRef now points to the root
يمكن ربط child() وparent وroot معًا عدة مرات، لأنّ كلّ منها يعرض مرجعًا. الاستثناء هو parent من root، وهو null.
Web
import { getStorage, ref } from "firebase/storage"; const storage = getStorage(); const spaceRef = ref(storage, 'images/space.jpg'); // References can be chained together multiple times const earthRef = ref(spaceRef.parent, 'earth.jpg'); // earthRef points to 'images/earth.jpg' // nullRef is null, since the parent of root is null const nullRef = spaceRef.root.parent;
Web
// References can be chained together multiple times var earthRef = spaceRef.parent.child('earth.jpg'); // earthRef points to 'images/earth.jpg' // nullRef is null, since the parent of root is null var nullRef = spaceRef.root.parent;
سمات المرجع
يمكنك فحص المراجع لفهم الملفات التي تشير إليها بشكل أفضل باستخدام السمات fullPath وname وbucket. تحصل هذه السمات على المسار الكامل للملف واسمه والحاوية التي يتم تخزين الملف فيها.
Web
import { getStorage, ref } from "firebase/storage"; const storage = getStorage(); const spaceRef = ref(storage, 'images/space.jpg'); // 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 where files are stored spaceRef.bucket;
Web
// 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 where files are stored spaceRef.bucket;
القيود المفروضة على المراجع
يمكن أن تحتوي مسارات المراجع وأسماؤها على أي تسلسل من أحرف Unicode الصالحة، ولكن يتم فرض قيود معيّنة، بما في ذلك:
- يجب أن يتراوح الطول الإجمالي لـ
reference.fullPathبين بايت واحد و1024 بايت عند استخدام الترميز UTF-8. - يجب عدم استخدام أحرف "إرجاع السطر" أو "تغذية السطر".
- تجنَّب استخدام
#أو[أو]أو*أو?، لأنّ هذه الأحرف لا تعمل بشكل جيد مع الأدوات الأخرى، مثل Firebase Realtime Database أو gsutil.
مثال كامل
Web
import { getStorage, ref } from "firebase/storage"; const storage = getStorage(); // Points to the root reference const storageRef = ref(storage); // Points to 'images' const imagesRef = ref(storageRef, 'images'); // Points to 'images/space.jpg' // Note that you can use variables to create child values const fileName = 'space.jpg'; const spaceRef = ref(imagesRef, fileName); // File path is 'images/space.jpg' const path = spaceRef.fullPath; // File name is 'space.jpg' const name = spaceRef.name; // Points to 'images' const imagesRefAgain = spaceRef.parent;
Web
// Points to the root reference var storageRef = firebase.storage().ref(); // Points to 'images' var imagesRef = storageRef.child('images'); // Points to 'images/space.jpg' // Note that you can use variables to create child values var fileName = 'space.jpg'; var spaceRef = imagesRef.child(fileName); // File path is 'images/space.jpg' var path = spaceRef.fullPath; // File name is 'space.jpg' var name = spaceRef.name; // Points to 'images' var imagesRef = spaceRef.parent;
بعد ذلك، لنتعرّف على كيفية تحميل الملفات إلى Cloud Storage.