ওয়েবে ক্লাউড স্টোরেজের মাধ্যমে ফাইলের তালিকা তৈরি করা

Cloud Storage for Firebase আপনাকে Cloud Storage বাকেটের কন্টেন্ট তালিকাভুক্ত করতে দেয়। SDK বর্তমান Cloud Storage রেফারেন্সের অধীনে থাকা অবজেক্টের আইটেম ও প্রিফিক্স, দুটিই রিটার্ন করে।

List API ব্যবহার করে এমন প্রোজেক্টের জন্য Cloud Storage for Firebase Rules ভার্সন ২ প্রয়োজন। আপনার আগে থেকে কোনও 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)
  }
}

সমস্যা ম্যানেজ করা

আপনি যদি নিরাপত্তা সংক্রান্ত নিয়ম ভার্সন ২-এ আপগ্রেড না করেন, তাহলে list() এবং listAll() একটি প্রত্যাখ্যাত প্রমিস রিটার্ন করে। আপনি এই সমস্যা দেখতে পেলে, আপনার নিরাপত্তা সংক্রান্ত নিয়ম আপগ্রেড করুন:

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

অন্যান্য সম্ভাব্য সমস্যা থেকে বোঝা যেতে পারে যে ব্যবহারকারীর কাছে সঠিক অনুমতি নেই। সমস্যা সম্পর্কে আরও তথ্য সমস্যা সমাধান করুন লিঙ্কে পাওয়া যাবে।