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