Cloud Storage for Firebase ช่วยให้คุณแสดงรายการเนื้อหาในที่เก็บข้อมูล Cloud Storage ได้ SDK จะแสดงผลทั้งออบเจ็กต์และคำนำหน้าของออบเจ็กต์ภายใต้การอ้างอิง Cloud Storage ปัจจุบัน
โปรเจ็กต์ที่ใช้ List API ต้องใช้Cloud Storage for Firebase กฎเวอร์ชัน 2 หากมีโปรเจ็กต์ Firebase อยู่แล้ว ให้ทำตามขั้นตอนในคู่มือกฎความปลอดภัย
list() ใช้ Google Cloud Storage List API
ใน Cloud Storage for Firebase เราใช้ / เป็นตัวคั่น ซึ่งช่วยให้เราจำลองความหมายของระบบไฟล์ได้ List API จะแสดงผลคำนำหน้าและรายการแยกกันเพื่อให้สามารถข้ามบัケット Cloud Storage แบบลำดับชั้นขนาดใหญ่ได้อย่างมีประสิทธิภาพ เช่น หากคุณอัปโหลดไฟล์ 1 ไฟล์ /images/uid/file1
root.child('images').listAll()จะแสดงผล/images/uidเป็นคำนำหน้าroot.child('images/uid').listAll()จะแสดงผลไฟล์เป็นรายการ
SDK ของ Cloud Storage for Firebase จะไม่แสดงเส้นทางออบเจ็กต์ที่มี / 2 ตัวติดกันหรือลงท้ายด้วย /. ตัวอย่างเช่น ลองพิจารณาที่เก็บข้อมูลที่มีออบเจ็กต์ต่อไปนี้
correctPrefix/happyItemwrongPrefix//sadItemlonelyItem/
การดำเนินการกับรายการในที่เก็บข้อมูลนี้จะให้ผลลัพธ์ต่อไปนี้
- การดำเนินการรายการที่รูทจะแสดงผลการอ้างอิงไปยัง
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.
ข้อผิดพลาดอื่นๆ ที่เป็นไปได้อาจบ่งชี้ว่าผู้ใช้ไม่มีสิทธิ์ที่ถูกต้อง ดูข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาดได้ในจัดการข้อผิดพลาด