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

इस पेज पर, 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 प्रीफ़िक्स और आइटम अलग-अलग दिखाता है. उदाहरण के लिए, अगर आपने एक फ़ाइल /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() एपीआई, नतीजों की संख्या को सीमित करता है. 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)
  }
}

गड़बड़ी ठीक करना

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

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

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