يتيح لك الأمر Cloud Storage for Firebase عرض محتوى حزمة Cloud Storage. تعرض حِزم SDK كلاً من العناصر والبادئات للكائنات ضمن مرجع Cloud Storage الحالي.
تتطلّب المشاريع التي تستخدم List API الإصدار 2 من Cloud Storage for Firebase Rules. إذا كان لديك مشروع حالي على Firebase، اتّبِع الخطوات الواردة في دليل قواعد الأمان.
يستخدم تطبيق "list()"
واجهة برمجة التطبيقات الخاصة بقائمة "Google Cloud Storage".
في Cloud Storage for Firebase، نستخدم / كمحدِّد، ما يتيح لنا
محاكاة دلالات نظام الملفات. للسماح بالانتقال الفعّال في حِزم Cloud Storage الهرمية الكبيرة، تعرض واجهة برمجة التطبيقات List API البادئات والعناصر بشكل منفصل. على سبيل المثال، إذا حمّلت ملفًا واحدًا /images/uid/file1،
- ستعرض الدالة
root.child('images').listAll()القيمة/images/uidكبادئة. - سيعرض
root.child('images/uid').listAll()الملف كعنصر.
لا تعرض حزمة تطوير البرامج (SDK) لـ "Cloud Storage for Firebase" مسارات العناصر التي تحتوي على حرفَي / متتاليَين أو تنتهي بحرف /.. على سبيل المثال، لنفترض أنّ لديك حزمة تحتوي على
العناصر التالية:
correctPrefix/happyItemwrongPrefix//sadItemlonelyItem/
ستؤدي عمليات عرض القائمة على العناصر في هذا المستودع إلى النتائج التالية:
- تعرض عملية القائمة في الجذر المراجع إلى
correctPrefixوwrongPrefixوlonelyItemعلى شكلprefixes. - تعرض عملية القائمة في
correctPrefix/المراجع إلىcorrectPrefix/happyItemعلى أنّهاitems. - لا تعرض عملية القائمة في
wrongPrefix/أي مراجع لأنّwrongPrefix//sadItemيحتوي على عنصرَين متتاليَين من النوع/. - لا تعرض عملية القائمة في
lonelyItem/أي مراجع لأنّ العنصرlonelyItem/ينتهي بـ/.
عرض قائمة بجميع الملفات
يمكنك استخدام listAll لجلب جميع النتائج لدليل معيّن.
ويُفضَّل استخدام هذه الطريقة مع الأدلة الصغيرة لأنّ جميع النتائج يتم تخزينها مؤقتًا في الذاكرة.
قد لا تعرض العملية أيضًا لقطة متسقة إذا تمت إضافة عناصر أو
إزالتها أثناء العملية.
بالنسبة إلى القوائم الكبيرة، استخدِم طريقة list() المقسَّمة إلى صفحات لأنّ listAll() تخزّن جميع النتائج مؤقتًا في الذاكرة.
يوضّح المثال التالي السمة listAll.
Web
import { getStorage, ref, listAll } from "firebase/storage"; const storage = getStorage(); // Create a reference under which you want to list const listRef = ref(storage, 'files/uid'); // Find all the prefixes and items. listAll(listRef) .then((res) => { res.prefixes.forEach((folderRef) => { // All the prefixes under listRef. // You may call listAll() recursively on them. }); res.items.forEach((itemRef) => { // All the items under listRef. }); }).catch((error) => { // Uh-oh, an error occurred! });
Web
// Create a reference under which you want to list var listRef = storageRef.child('files/uid'); // Find all the prefixes and items. listRef.listAll() .then((res) => { res.prefixes.forEach((folderRef) => { // All the prefixes under listRef. // You may call listAll() recursively on them. }); res.items.forEach((itemRef) => { // All the items under listRef. }); }).catch((error) => { // Uh-oh, an error occurred! });
تقسيم نتائج القائمة إلى صفحات
تفرض واجهة برمجة التطبيقات list() حدًا أقصى على عدد النتائج التي تعرضها. list()
توفّر مشاهدة صفحة على الويب متسقة وتعرض pageToken يسمح بالتحكّم في
وقت جلب نتائج إضافية.
يرمّز pageToken المسار والإصدار لآخر عنصر تم عرضه في النتيجة السابقة. في طلب لاحق باستخدام pageToken، يتم عرض العناصر التي تأتي بعد pageToken.
يوضّح المثال التالي كيفية تقسيم نتيجة إلى صفحات باستخدام async/await.
Web
import { getStorage, ref, list } from "firebase/storage"; async function pageTokenExample(){ // Create a reference under which you want to list const storage = getStorage(); const listRef = ref(storage, 'files/uid'); // Fetch the first page of 100. const firstPage = await list(listRef, { maxResults: 100 }); // Use the result. // processItems(firstPage.items) // processPrefixes(firstPage.prefixes) // Fetch the second page if there are more elements. if (firstPage.nextPageToken) { const secondPage = await list(listRef, { maxResults: 100, pageToken: firstPage.nextPageToken, }); // processItems(secondPage.items) // processPrefixes(secondPage.prefixes) } }
Web
async function pageTokenExample(){ // Create a reference under which you want to list var listRef = storageRef.child('files/uid'); // Fetch the first page of 100. var firstPage = await listRef.list({ maxResults: 100}); // Use the result. // processItems(firstPage.items) // processPrefixes(firstPage.prefixes) // Fetch the second page if there are more elements. if (firstPage.nextPageToken) { var secondPage = await listRef.list({ maxResults: 100, pageToken: firstPage.nextPageToken, }); // processItems(secondPage.items) // processPrefixes(secondPage.prefixes) } }
معالجة الأخطاء
تعرض الدالتان list() وlistAll() كائن Promise مرفوضًا إذا لم تتم ترقية قواعد الأمان إلى الإصدار 2. يجب ترقية قواعد الأمان إذا ظهر لك هذا الخطأ:
Listing objects in a bucket is disallowed for rules_version = "1".
Please update storage security rules to rules_version = "2" to use list.
قد تشير الأخطاء المحتملة الأخرى إلى أنّ المستخدم ليس لديه الإذن المناسب. يمكن الاطّلاع على مزيد من المعلومات حول الأخطاء في مقالة معالجة الأخطاء.