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 สำหรับ 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
Kotlin+KTX
val storage = Firebase.storage val listRef = storage.reference.child("files/uid") // You'll need to import com.google.firebase.storage.component1 and // com.google.firebase.storage.component2 listRef.listAll() .addOnSuccessListener { (items, prefixes) -> for (prefix in prefixes) { // All the prefixes under listRef. // You may call listAll() recursively on them. } for (item in items) { // All the items under listRef. } } .addOnFailureListener { // Uh-oh, an error occurred! }
Java
StorageReference listRef = storage.getReference().child("files/uid"); listRef.listAll() .addOnSuccessListener(new OnSuccessListener<ListResult>() { @Override public void onSuccess(ListResult listResult) { for (StorageReference prefix : listResult.getPrefixes()) { // All the prefixes under listRef. // You may call listAll() recursively on them. } for (StorageReference item : listResult.getItems()) { // All the items under listRef. } } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Uh-oh, an error occurred! } });
ผลการค้นหารายการเลขหน้า
list()
API จำกัดจำนวนผลการค้นหาที่แสดง list()
จะมีการดูหน้าเว็บที่สอดคล้องกันและแสดง PageToken ที่ทำให้สามารถควบคุม
เวลาที่ควรเรียกดูผลลัพธ์เพิ่มเติม
pageToken จะเข้ารหัสเส้นทางและเวอร์ชันของรายการสุดท้ายที่แสดงผลใน ผลลัพธ์ก่อนหน้า ในคำขอต่อๆ มาโดยใช้ pageToken คือ รายการที่เข้ามา หลังจากที่โทเค็นหน้าแสดง
ตัวอย่างต่อไปนี้สาธิตการใส่เลขหน้าให้กับผลลัพธ์
Kotlin+KTX
fun listAllPaginated(pageToken: String?) { val storage = Firebase.storage val listRef = storage.reference.child("files/uid") // Fetch the next page of results, using the pageToken if we have one. val listPageTask = if (pageToken != null) { listRef.list(100, pageToken) } else { listRef.list(100) } // You'll need to import com.google.firebase.storage.component1 and // com.google.firebase.storage.component2 listPageTask .addOnSuccessListener { (items, prefixes, pageToken) -> // Process page of results processResults(items, prefixes) // Recurse onto next page pageToken?.let { listAllPaginated(it) } }.addOnFailureListener { // Uh-oh, an error occurred. } }
Java
public void listAllPaginated(@Nullable String pageToken) { FirebaseStorage storage = FirebaseStorage.getInstance(); StorageReference listRef = storage.getReference().child("files/uid"); // Fetch the next page of results, using the pageToken if we have one. Task<ListResult> listPageTask = pageToken != null ? listRef.list(100, pageToken) : listRef.list(100); listPageTask .addOnSuccessListener(new OnSuccessListener<ListResult>() { @Override public void onSuccess(ListResult listResult) { List<StorageReference> prefixes = listResult.getPrefixes(); List<StorageReference> items = listResult.getItems(); // Process page of results // ... // Recurse onto next page if (listResult.getPageToken() != null) { listAllPaginated(listResult.getPageToken()); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Uh-oh, an error occurred. } }); }
จัดการข้อผิดพลาด
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.
ข้อผิดพลาดอื่นๆ ที่อาจเกิดขึ้นอาจบ่งชี้ว่าผู้ใช้ไม่มีสิทธิ์ที่ถูกต้อง ดูข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาดได้ใน จัดการข้อผิดพลาด