แสดงรายการไฟล์ด้วย Cloud Storage บนแพลตฟอร์ม Apple

Cloud Storage for Firebase ให้คุณแสดงรายการเนื้อหาของ ที่เก็บข้อมูล Cloud Storage SDK จะแสดงทั้งรายการและคำนำหน้า ของออบเจ็กต์ภายใต้การอ้างอิง Cloud Storage ปัจจุบัน

โปรเจ็กต์ที่ใช้ 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(completion:) เพื่อเรียกผลลัพธ์ทั้งหมดสำหรับไดเรกทอรี เหมาะสำหรับไดเรกทอรีขนาดเล็กเนื่องจากผลลัพธ์ทั้งหมดจะได้รับการบัฟเฟอร์ในหน่วยความจำ นอกจากนี้ การดำเนินการนี้อาจไม่แสดงผลสแนปชอตที่สอดคล้องกันหากมีการเพิ่มออบเจ็กต์ หรือ ออกระหว่างกระบวนการดังกล่าว

สำหรับรายการขนาดใหญ่ ให้ใช้เมธอด list(withMaxResults:completion:) ที่ใส่เลขหน้าเป็น listAll(completion:) จะบัฟเฟอร์ผลลัพธ์ทั้งหมดในหน่วยความจำ

ตัวอย่างต่อไปนี้คือ listAll(completion:)

Swift

let storageReference = storage.reference().child("files/uid")
do {
  let result = try await storageReference.listAll()
  for prefix in result.prefixes {
    // The prefixes under storageReference.
    // You may call listAll(completion:) recursively on them.
  }
  for item in result.items {
    // The items under storageReference.
  }
} catch {
  // ...
}

Objective-C

FIRStorageReference *storageReference = [storage reference];
[storageReference listAllWithCompletion:^(FIRStorageListResult *result, NSError *error) {
  if (error != nil) {
    // ...
  }

  for (FIRStorageReference *prefix in result.prefixes) {
    // All the prefixes under storageReference.
    // You may call listAllWithCompletion: recursively on them.
  }
  for (FIRStorageReference *item in result.items) {
    // All items under storageReference.
  }
}];

ผลการค้นหารายการเลขหน้า

list(withMaxResults:completion:) API จำกัดจำนวน ผลลัพธ์ที่แสดง list(withMaxResults:completion) มอบขีดจำกัด การดูหน้าเว็บ��ละแสดงโทเค็นหน้าที่ช่วยให้ควบคุมเวลาที่จะดึงข้อมูลได้ ผลลัพธ์เพิ่มเติม

pageToken จะเข้ารหัสเส้นทางและเวอร์ชันของรายการสุดท้ายที่แสดงผลใน ผลลัพธ์ก่อนหน้า ในคำขอต่อๆ มาโดยใช้ pageToken คือ รายการที่เข้ามา หลังจากที่โทเค็นหน้าแสดง

ตัวอย่างต่อไปนี้สาธิตการใส่เลขหน้าให้กับผลลัพธ์

Swift

func listAllPaginated(pageToken: String? = nil) async throws {
  let storage = Storage.storage()
  let storageReference = storage.reference().child("files/uid")

  let listResult: StorageListResult
  if let pageToken = pageToken {
    listResult = try await storageReference.list(maxResults: 100, pageToken: pageToken)
  } else {
    listResult = try await storageReference.list(maxResults: 100)
  }
  let prefixes = listResult.prefixes
  let items = listResult.items
  // Handle list result
  // ...

  // Process next page
  if let token = listResult.pageToken {
    try await listAllPaginated(pageToken: token)
  }
}

Objective-C

- (void)paginateFilesAtReference:(FIRStorageReference *)reference
                       pageToken:(nullable NSString *)pageToken {
  void (^pageHandler)(FIRStorageListResult *_Nonnull, NSError *_Nullable) =
      ^(FIRStorageListResult *result, NSError *error) {
        if (error != nil) {
          // ...
        }
        NSArray *prefixes = result.prefixes;
        NSArray *items = result.items;

        // ...

        // Process next page
        if (result.pageToken != nil) {
          [self paginateFilesAtReference:reference pageToken:result.pageToken];
        }
  };

  if (pageToken != nil) {
    [reference listWithMaxResults:100 pageToken:pageToken completion:pageHandler];
  } else {
    [reference listWithMaxResults:100 completion:pageHandler];
  }
}

จัดการข้อผิดพลาด

เมธอดใน API ของรายการจะล้มเหลวหากคุณไม่ได้อัปเกรดกฎความปลอดภัยเป็น เวอร์ชัน 2 โปรดอัปเกรดกฎความปลอดภัยหากพบข้อผิดพลาดนี้

Listing objects in a bucket is disallowed for rules_version = "1".
Please update storage security rules to rules_version = "2" to use list.

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