Cloud Storage for Firebase की मदद से, Cloud Storage बकेट में मौजूद कॉन्टेंट की सूची बनाई जा सकती है. SDK, मौजूदा Cloud Storage रेफ़रंस के तहत ऑब्जेक्ट के आइटम और प्रीफ़िक्स, दोनों दिखाता है.
सूची वाले एपीआई का इस्तेमाल करने वाले प्रोजेक्ट के लिए, Cloud Storage for Firebase नियमों का वर्शन 2 ज़रूरी है. अगर आपके पास कोई मौजूदा Firebase प्रोजेक्ट है, तो सुरक्षा के नियमों से जुड़ी गाइड में दिया गया तरीका अपनाएं.
list()
Google Cloud Storage List API का इस्तेमाल करता है.
Cloud Storage for Firebase में, हम /
का इस्तेमाल डीलिमिटर के तौर पर करते हैं. इससे हमें फ़ाइल सिस्टम के सिमैंटिक को समझने में मदद मिलती है. बड़े और क्रमबद्ध Cloud Storage बकेट को आसानी से ट्रैवर्स करने के लिए, List API प्रीफ़िक्स और आइटम अलग-अलग दिखाता है. उदाहरण के लिए, अगर आपने एक फ़ाइल /images/uid/file1
अपलोड की है,
root.child('images').listAll()
,/images/uid
को प्रीफ़िक्स के तौर पर दिखाएगा.root.child('images/uid').listAll()
फ़ाइल को आइटम के तौर पर दिखाएगा.
Cloud Storage for Firebase SDK टूल, ऐसे ऑब्जेक्ट पाथ नहीं दिखाता जिनमें लगातार दो /
हों या जो /.
से खत्म होते हों. उदाहरण के लिए, मान लें कि किसी बकेट में ये ऑब्जेक्ट मौजूद हैं:
correctPrefix/happyItem
wrongPrefix//sadItem
lonelyItem/
इस बकेट में मौजूद आइटम पर सूची से जुड़ी कार्रवाइयां करने पर, ये नतीजे मिलेंगे:
- रूट पर मौजूद सूची की कार्रवाई,
correctPrefix
,wrongPrefix
, औरlonelyItem
के रेफ़रंस कोprefixes
के तौर पर दिखाती है. correctPrefix/
पर की गई सूची की कार्रवाई,correctPrefix/happyItem
के रेफ़रंस कोitems
के तौर पर दिखाती है.wrongPrefix/
पर मौजूद सूची के ऑपरेशन से कोई रेफ़रंस नहीं मिलता, क्योंकिwrongPrefix//sadItem
में दो लगातार/
शामिल हैं.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:)
एपीआई, खोज के नतीजों की संख्या को सीमित करता है. list(withMaxResults:completion)
लगातार पेजव्यू देता है. साथ ही, एक पेजटोकन दिखाता है. इससे यह कंट्रोल किया जा सकता है कि अतिरिक्त नतीजे कब फ़ेच किए जाएं.
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]; } }
गड़बड़ियां ठीक करना
अगर आपने सुरक्षा नियमों को वर्शन 2 पर अपग्रेड नहीं किया है, तो सूची वाले एपीआई में मौजूद तरीके काम नहीं करेंगे. अगर आपको यह गड़बड़ी दिखती है, तो सुरक्षा के नियमों को अपग्रेड करें:
Listing objects in a bucket is disallowed for rules_version = "1".
Please update storage security rules to rules_version = "2" to use list.
अन्य गड़बड़ियों से पता चल सकता है कि उपयोगकर्ता के पास सही अनुमतियां नहीं हैं. गड़बड़ियों के बारे में ज़्यादा जानकारी के लिए, गड़बड़ियों को ठीक करना लेख पढ़ें.