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

หน้านี้จะอธิบายวิธีใช้ Cloud Storage for Firebase SDK เพื่อแสดงรายการเนื้อหาของที่เก็บข้อมูล 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/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 จะเข้ารหัสเส้นทางและเวอร์ชันของรายการล่าสุดที่แสดงผลในผลลัพธ์ก่อนหน้า ในคำขอต่อมาที่ใช้ 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.

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