הצגה של רשימת קבצים באמצעות Cloud Storage באינטרנט

הפקודה Cloud Storage for Firebase מאפשרת להציג את רשימת התכנים בקטגוריה Cloud Storage. ערכות ה-SDK מחזירות גם את הפריטים וגם את הקידומות של האובייקטים בהפניה הנוכחית ל-Cloud Storage.

בפרויקטים שמשתמשים ב-List API נדרשת גרסה 2 של כללי Cloud Storage for Firebase. אם יש לכם פרויקט קיים ב-Firebase, פעלו לפי השלבים במדריך לכללי אבטחה.

‫list() משתמש ב-Google Cloud Storage List API. ב-Cloud Storage for Firebase, אנחנו משתמשים ב-/ כתו מפריד, שמאפשר לנו לדמות סמנטיקה של מערכת קבצים. כדי לאפשר מעבר יעיל בין קטגוריות גדולות והיררכיות של Cloud Storage, ה-API של List מחזיר קידומות ופריטים בנפרד. לדוגמה, אם מעלים קובץ אחד /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!
  });

עימוד תוצאות של רשימה

ב-API של 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.

שגיאות אפשריות אחרות עשויות להצביע על כך שלמשתמש אין את ההרשאה המתאימה. מידע נוסף על שגיאות זמין במאמר טיפול בשגיאות.