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