แสดงรายการไฟล์ด้วย Cloud Storage บน Android

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 เราใช้ / เป็นตัวคั่น ซึ่งช่วยให้เรา จำลองความหมายของระบบไฟล์ได้ API รายการจะแสดงคำนำหน้าและรายการแยกกันเพื่อให้การข้ามผ่านCloud Storageที่เก็บข้อมูลแบบลำดับชั้นขนาดใหญ่เป็นไปอย่างมีประสิทธิภาพ เช่น หากคุณอัปโหลดไฟล์ 1 ไฟล์ /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/

การดำเนินการกับรายการในที่เก็บข้อมูลนี้จะให้ผลลัพธ์ต่อไปนี้

  • การดำเนินการกับรายการที่รูทจะแสดงการอ้างอิงถึง 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.

ข้อผิดพลาดอื่นๆ ที่อาจเกิดขึ้นอาจบ่งบอกว่าผู้ใช้ไม่มีสิทธิ์ที่เหมาะสม ดูข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาดได้ในส่วนจัดการข้อผิดพลาด