แสดงรายการไฟล์ด้วย Cloud Storage บนเว็บ

Cloud Storage for Firebase ช่วยให้คุณแสดงรายการเนื้อหาของบัคเก็ต Cloud Storage ได้ โดย SDK จะแสดงทั้งรายการและคำนำหน้าของ ออบเจ็กต์ภายใต้การอ้างอิง Cloud Storage ปัจจุบัน

โปรเจ็กต์ที่ใช้ List API ต้องใช้ Cloud Storage for Firebase Rules เวอร์ชัน 2 หากมีโปรเจ็กต์ Firebase อยู่แล้ว ให้ทำตามขั้นตอนใน คู่มือเกี่ยวกับกฎความปลอดภัย

list() ใช้ Google Cloud Storage List API ใน Cloud Storage for Firebase เราใช้ / เป็นตัวคั่น ซึ่งช่วยให้เรา จำลองความหมายของระบบไฟล์ได้ List API จะแสดงคำนำหน้าและรายการ แยกกันเพื่อให้การข้ามผ่านบัคเก็ตขนาดใหญ่ที่มี ลำดับชั้น Cloud Storageเป็นไปอย่างมีประสิทธิภาพ ตัวอย่างเช่น หากคุณอัปโหลดไฟล์ /images/uid/file1

  • root.child('images').listAll() จะแสดง /images/uid เป็นคำนำหน้า
  • root.child('images/uid').listAll() จะแสดงไฟล์เป็นรายการ

Cloud Storage for Firebase SDK จะไม่แสดงเส้นทางออบเจ็กต์ที่มี / 2 ตัวติดกันหรือลงท้ายด้วย /. ตัวอย่างเช่น พิจารณาบัคเก็ตที่มีออบเจ็กต์ต่อไปนี้

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

การดำเนินการกับรายการในบัคเก็ตนี้จะให้ผลลัพธ์ต่อไปนี้

  • การดำเนินการกับรายการที่รูทจะแสดงการอ้างอิงถึง correctPrefix, wrongPrefix และ lonelyItem เป็น prefixes
  • การดำเนินการกับรายการที่ correctPrefix/ จะแสดงการอ้างอิงถึง correctPrefix/happyItem เป็น items
  • การดำเนินการกับรายการที่ wrongPrefix/ จะไม่แสดงการอ้างอิงใดๆ เนื่องจาก wrongPrefix//sadItem มี / 2 ตัวติดกัน
  • การดำเนินการกับรายการที่ 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() API กำหนดขีดจำกัดจำนวนผลลัพธ์ที่จะแสดง 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.

ข้อผิดพลาดอื่นๆ ที่อาจเกิดขึ้นอาจบ่งบอกว่าผู้ใช้ไม่มีสิทธิ์ที่เหมาะสม ดูข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาดได้ใน หัวข้อจัดการข้อผิดพลาด.