Cloud Storage की मदद से, Apple प्लैटफ़ॉर्म पर फ़ाइलों की सूची बनाना

Firebase के लिए Cloud Storage की मदद से Cloud Storage बकेट. SDK ट��ल, आइटम और प्रीफ़िक्स, दोनों दिखाते हैं के ऑब्जेक्ट की जानकारी मिल सकती है.

सूची एपीआई का इस्तेमाल करने वाले प्रोजेक्ट के लिए, Firebase के लिए Cloud Storage की ज़रूरत होती है नियम का वर्शन 2. अगर आपके पास कोई मौजूदा Firebase प्रोजेक्ट है, तो सुरक्षा के नियमों की गाइड पढ़ें.

list() Google Cloud Storage सूची एपीआई. 'Firebase के लिए Cloud Storage' में, हम / का इस्तेमाल डीलिमिटर के तौर पर करते हैं. इसकी मदद से, हम ये काम कर पाते हैं इससे फ़ाइल सिस्टम सिमैंटिक को एम्युलेट किया जा सकता है. बड़े स्तर के असरदार ट्रैवर्सल की अनुमति देने के लिए, हैरारकी के हिसाब से Cloud Storage बकेट, लिस्ट एपीआई की मदद से प्रीफ़िक्स और आइटम दिखाए जाते हैं अलग करना होगा. उदाहरण के लिए, अगर आपने /images/uid/file1 फ़ाइल अपलोड की है,

  • root.child('images').listAll(), /images/uid को प्रीफ़िक्स के तौर पर दिखाएगा.
  • root.child('images/uid').listAll(), फ़ाइल को आइटम के तौर पर दिखाएगा.

'Firebase के लिए Cloud Storage' SDK टूल ऐसे ऑब्जेक्ट पाथ नहीं दिखाता जिनमें दो शामिल हों लगातार / या /. पर खत्म होना चाहिए. उदाहरण के लिए, एक ऐसी बकेट पर विचार करें जिसमें ये ऑब्जेक्ट शामिल हैं:

  • correctPrefix/happyItem
  • wrongPrefix//sadItem
  • lonelyItem/

इस बकेट में मौजूद आइटम पर की जाने वाली कार्रवाइयों की सूची से ये नतीजे मिलेंगे:

  • रूट में तय की गई कार्रवाई, correctPrefix के रेफ़रंस दिखाता है, prefixes के तौर पर wrongPrefix और lonelyItem.
  • correctPrefix/ पर सूची कार्रवाई, रेफ़रंस को रिटर्न ��रती है items के तौर पर correctPrefix/happyItem.
  • 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];
  }
}

गड़बड़ियां ठीक करना

अगर आपने अपने सुरक्षा नियमों को इस पर अपग्रेड नहीं किया है, तो सूची 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.

अन्य संभावित गड़बड़ियों का यह मतलब हो सकता है कि उपयोगकर्ता के पास सही अनुमतियां नहीं हैं. गड़बड़ियों के बारे में ज़्यादा जानकारी यहां मिल सकती है गड़बड़ियां ठीक करें.