רשימת קבצים עם Cloud Storage באינטרנט

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

פרויקטים המשתמשים ב-List API דורשים Cloud Storage for Firebase Rules גרסה 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() יחזיר את הקובץ כפריט.

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

  • correctPrefix/happyItem
  • wrongPrefix//sadItem
  • lonelyItem/

פעולות הרשימה על פריטים בדלי זה יתנו את התוצאות הבאות:

  • פעולת הרשימה בשורש מחזירה את ההפניות ל- correctPrefix , wrongPrefix ו- lonelyItem בתור prefixes .
  • פעולת הרשימה ב- correctPrefix/ מחזירה את ההפניות ל- correctPrefix/happyItem items .
  • פעולת הרשימה ב- wrongPrefix/ לא מחזירה הפניות כלשהן כי wrongPrefix//sadItem מכילה שני / ים עוקבים.
  • פעולת הרשימה ב- lonelyItem/ אינה מחזירה שום הפניות מכיוון שהאובייקט lonelyItem/ מסתיים ב- / .

רשום את כל הקבצים

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

עבור רשימה גדולה, השתמש בשיטת ה-pagened list() שכן listAll() מאחסנת את כל התוצאות בזיכרון.

הדוגמה הבאה מדגימה listAll .

Web modular API

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 namespaced API

// 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 modular API

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 namespaced API

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.

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