إدراج الملفات باستخدام Cloud Storage على الويب

يتيح لك Cloud Storage for Firebase إدراج محتوى حزمة Cloud Storage. تعرض حِزم SDK كلاً من العناصر وبادئات الكائنات ضمن مرجع Cloud Storage الحالي.

تتطلّب المشاريع التي تستخدم List API Cloud Storage for Firebase Rules Version 2. إذا كان لديك مشروع حالي على Firebase، اتّبِع الخطوات الواردة في دليل قواعد الأمان.

تستخدم list() Google Cloud Storage List API. في 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/happyItem
  • wrongPrefix//sadItem
  • lonelyItem/

ستؤدي عمليات القائمة على العناصر في هذه الحزمة إلى النتائج التالية:

  • تعرض عملية القائمة في الجذر المراجع إلى 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.

قد تشير الأخطاء المحتملة الأخرى إلى أنّ المستخدم لا يملك الإذن المناسب. يمكنك الاطّلاع على مزيد من المعلومات حول الأخطاء في مقالة التعامل مع الأخطاء.