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

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

تتطلّب المشاريع التي تستخدم List API استخدام الإصدار 2 من Cloud Storage for Firebase Rules. إذا كان لديك مشروع حالي على 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.

يوضّح المثال التالي تقسيم نتيجة إلى صفحات باستخدام 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() وعدًا مرفوضًا إذا لم تكن قد أجريت ترقية لقواعد الأمان إلى الإصدار 2. عليك ترقية قواعد الأمان إذا ظهر لك هذا الخطأ:

Listing objects in a bucket is disallowed for rules_version = "1".
Please update storage security rules to rules_version = "2" to use list.

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