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

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 เราใช้ / เป็นตัวคั่น ซึ่งช่วยให้เรา จำลองความหมายของระบบไฟล์ได้ 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/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.
  }
}];

แบ่งหน้าผลลัพธ์ของรายการ

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

pageToken จะเข้ารหัสเส้นทางและเวอร์ชันของรายการสุดท้ายที่แสดงในผลลัพธ์ก่อนหน้า ในคำขอที่ตามมาซึ่งใช้ 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];
  }
}

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

เมธอดใน List 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.

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