Cloud Storage for Firebase ช่วยให้คุณแสดงรายการเนื้อหาของที่เก็บข้อมูล Cloud Storage ของคุณได้ SDK ส่งคืนทั้งรายการและคำนำหน้าของออบเจ็กต์ภายใต้การอ้างอิง Cloud Storage ปัจจุบัน
โปรเจ็กต์ที่ใช้ List API ต้องใช้ Cloud Storage สำหรับกฎ Firebase เวอร์ชัน 2 หากคุณมีโปรเจ็กต์ Firebase อยู่แล้ว ให้ทำตามขั้นตอนใน คู่มือกฎความปลอดภัย
list()
ใช้ Google Cloud Storage List API ใน Cloud Storage สำหรับ Firebase เราใช้ /
เป็นตัวคั่น ซึ่งช่วยให้เราจำลองความหมายของระบบไฟล์ได้ เพื่อให้การข้ามผ่านที่เก็บข้อมูล Cloud Storage ขนาดใหญ่ที่มีลำดับชั้นมีประสิทธิภาพ List API จะส่งคืนคำนำหน้าและรายการแยกกัน ตัวอย่างเช่น หากคุณอัปโหลดหนึ่งไฟล์ /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/
การดำเนินการรายการกับรายการในที่เก็บข้อมูลนี้จะให้ผลลัพธ์ดังต่อไปนี้:
- การดำเนินการรายการที่ root ส่งคืนการอ้างอิงไปยัง
correctPrefix
,wrongPrefix
และlonelyItem
เป็นprefixes
- การดำเนินการรายการที่
correctPrefix/
ส่งคืนการอ้างอิงไปยังcorrectPrefix/happyItem
เป็นitems
- การดำเนินการรายการที่
wrongPrefix/
จะไม่ส่งคืนการอ้างอิงใดๆ เนื่องจากwrongPrefix//sadItem
มีสอง/
s ติดต่อกัน - การดำเนินการรายการที่
lonelyItem/
จะไม่ส่งคืนการอ้างอิงใดๆ เนื่องจากอ็อบเจ็กต์lonelyItem/
ลงท้ายด้วย/
แสดงรายการไฟล์ทั้งหมด
คุณสามารถใช้ listAll
เพื่อดึงผลลัพธ์ทั้งหมดสำหรับไดเร็กทอรี วิธีนี้เหมาะที่สุดสำหรับไดเร็กทอรีขนาดเล็ก เนื่องจากผลลัพธ์ทั้งหมดถูกบัฟเฟอร์ไว้ในหน่วยความจำ การดำเนินการอาจไม่ส่งคืนสแน็ปช็อตที่สอดคล้องกันหากมีการเพิ่มหรือลบออบเจ็กต์ในระหว่างกระบวนการ
สำหรับรายการขนาดใหญ่ ให้ใช้เมธอด paginated 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! });
ผลลัพธ์รายการเลขหน้า
list()
API กำหนดขีดจำกัดจำนวนผลลัพธ์ที่ส่งคืน 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()
ส่งคืน 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.
ข้อผิดพลาดอื่นๆ ที่เป็นไปได้อาจบ่งชี้ว่าผู้ใช้ไม่มีสิทธิ์ที่ถูกต้อง ข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาดสามารถพบได้ใน Handle Errors