Cloud Storage की मदद से वेब पर फ़ाइलों की सूची बनाना

Cloud Storage for Firebase की मदद से, आपके Cloud Storage बकेट में मौजूद कॉन्टेंट की सूची देखी जा सकती है. एसडीके, मौजूदा Cloud Storage रेफ़रंस के तहत आने वाले ऑब्जेक्ट के आइटम और प्रीफ़िक्स, दोनों दिखाते हैं.

List API का इस्तेमाल करने वाले प्रोजेक्ट के लिए, 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 का इस्तेमाल किया जा सकता है. इसका इस्तेमाल, छोटी डायरेक्ट्री के लिए करना सबसे सही होता है, क्योंकि सभी नतीजे मेमोरी में बफ़र किए जाते हैं. अगर प्रोसेस के दौरान ऑब्जेक्ट जोड़े या हटाए जाते हैं, तो हो सकता है कि ऑपरेशन के बाद, नतीजों का स्नैपशॉट न दिखे.

बड़ी सूची के लिए, पेजिनेट किए गए list() तरीके का इस्तेमाल करें, क्योंकि listAll() सभी नतीजों को मेमोरी में बफ़र करता है.

यहां दिए गए उदाहरण में, listAll का इस्तेमाल दिखाया गया है.

Web

import { getStorage, ref, listAll } from "firebase/storage";

const storage = getStorage();

// Create a reference under which you want to list
const listRef = ref(storage, 'files/uid');

// Find all the prefixes and items.
listAll(listRef)
  .then((res) => {
    res.prefixes.forEach((folderRef) => {
      // All the prefixes under listRef.
      // You may call listAll() recursively on them.
    });
    res.items.forEach((itemRef) => {
      // All the items under listRef.
    });
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

Web

// Create a reference under which you want to list
var listRef = storageRef.child('files/uid');

// Find all the prefixes and items.
listRef.listAll()
  .then((res) => {
    res.prefixes.forEach((folderRef) => {
      // All the prefixes under listRef.
      // You may call listAll() recursively on them.
    });
    res.items.forEach((itemRef) => {
      // All the items under listRef.
    });
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

सूची के नतीजों को पेजिनेट करना

list() API, नतीजों की संख्या पर सीमा लगाता है. list() से पेज व्यू की जानकारी मिलती है. साथ ही, यह एक pageToken दिखाता है. इसकी मदद से, यह कंट्रोल किया जा सकता है कि ज़्यादा नतीजे कब फ़ेच किए जाएं.

pageToken, पिछले नतीजे में दिखाए गए आखिरी आइटम का पाथ और वर्शन एनकोड करता है. pageToken का इस्तेमाल करके, अगले अनुरोध में, pageToken के बाद आने वाले आइटम दिखाए जाते हैं.

यहां दिए गए उदाहरण में, async/await का इस्तेमाल करके, नतीजे को पेजिनेट करने का तरीका दिखाया गया है.

Web

import { getStorage, ref, list } from "firebase/storage";

async function pageTokenExample(){
  // Create a reference under which you want to list
  const storage = getStorage();
  const listRef = ref(storage, 'files/uid');

  // Fetch the first page of 100.
  const firstPage = await list(listRef, { maxResults: 100 });

  // Use the result.
  // processItems(firstPage.items)
  // processPrefixes(firstPage.prefixes)

  // Fetch the second page if there are more elements.
  if (firstPage.nextPageToken) {
    const secondPage = await list(listRef, {
      maxResults: 100,
      pageToken: firstPage.nextPageToken,
    });
    // processItems(secondPage.items)
    // processPrefixes(secondPage.prefixes)
  }
}

Web

async function pageTokenExample(){
  // Create a reference under which you want to list
  var listRef = storageRef.child('files/uid');

  // Fetch the first page of 100.
  var firstPage = await listRef.list({ maxResults: 100});

  // Use the result.
  // processItems(firstPage.items)
  // processPrefixes(firstPage.prefixes)

  // Fetch the second page if there are more elements.
  if (firstPage.nextPageToken) {
    var secondPage = await listRef.list({
      maxResults: 100,
      pageToken: firstPage.nextPageToken,
    });
    // processItems(secondPage.items)
    // processPrefixes(secondPage.prefixes)
  }
}

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

अगर आपने सुरक्षा के नियमों को वर्शन 2 में अपग्रेड नहीं किया है, तो list() और listAll() , अस्वीकार किया गया Promise दिखाते हैं. अगर आपको यह गड़बड़ी दिखती है, तो सुरक्षा के नियमों को अपग्रेड करें:

Listing objects in a bucket is disallowed for rules_version = "1".
Please update storage security rules to rules_version = "2" to use list.

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