Cloud Storage for Firebase ช่วยให้คุณแสดงรายการเนื้อหาในถังCloud Storageได้ SDK จะแสดงทั้งรายการและคำนำหน้าของออบเจ็กต์ภายใต้การอ้างอิง Cloud Storage ปัจจุบัน
โปรเจ็กต์ที่ใช้ 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()
จะแสดงผลไฟล์เป็นรายการ
SDK ของ Cloud Storage for Firebase จะไม่แสดงผลเส้นทางออบเจ็กต์ที่มี /
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:)
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 { // ... }
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)
ให้ Pageview ที่สอดคล้องกันและแสดง pageToken ที่ให้คุณควบคุมเวลาที่จะดึงข้อมูลผลลัพธ์เพิ่มเติม
pageToken จะเข้ารหัสเส้นทางและเวอร์ชันของรายการสุดท้ายที่แสดงในผลการค้นหาก่อนหน้า ในคำขอที่ตามมาโดยใช้ pageToken ระบบจะแสดงรายการที่อยู่หลัง pageToken
ตัวอย่างต่อไปนี้แสดงการแบ่งหน้าผลการค้นหา
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) } }
- (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.
ข้อผิดพลาดอื่นๆ ที่อาจเกิดขึ้นอาจบ่งบอกว่าผู้ใช้ไม่มีสิทธิ์ที่เหมาะสม ดูข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาดได้ในจัดการข้อผิดพลาด