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 เราใช้ /
เป็นตัวคั่นซึ่งช่วยให้เรา
จำลองความหมายของระบบไฟล์ เพื่อให้การข้ามผ่านที่มีประสิทธิภาพในการรับส่งข้อมูลขนาดใหญ่
ที่เก็บข้อมูล Cloud Storage แบบลําดับชั้น โดย API ของรายการจะแสดงคํานําหน้าและรายการต่างๆ
แยกกัน เช่น หากคุณอัปโหลดไฟล์ /images/uid/file1
1 ไฟล์
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 คือ รายการที่เข้ามา หลังจากที่โทเค็นหน้าแสดง
ตัวอย่างต่อไปนี้สาธิตการใส่เลขหน้าให้กับผลลัพธ์โดยใช้ 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()
ส่งคืน "สัญญา" ที่ถูกปฏิเสธหากยังไม่ได้อัปเกรด
กฎความปลอดภัยเป็นเวอร์ชัน 2 โปรดอัปเกรดกฎความปลอดภัยหากพบเห็นไอคอนนี้
ข้อผิดพลาด:
Listing objects in a bucket is disallowed for rules_version = "1".
Please update storage security rules to rules_version = "2" to use list.
ข้อผิดพลาดอื่นๆ ที่อาจเกิดขึ้นอาจบ่งชี้ว่าผู้ใช้ไม่มีสิทธิ์ที่ถูกต้อง ดูข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาดได้ใน จัดการข้อผิดพลาด