Cloud Storage for Firebase আপনাকে Cloud Storage বাকেটের কন্টেন্ট তালিকাভুক্ত করতে দেয়। SDK বর্তমান Cloud Storage রেফারেন্সের অধীনে থাকা অবজেক্টের আইটেম ও প্রিফিক্স, দুটিই রিটার্ন করে।
List API ব্যবহার করে এমন প্রোজেক্টের জন্য Cloud Storage for Firebase Rules Version 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/happyItemwrongPrefix//sadItemlonelyItem/
এই বাকেটের আইটেমে তালিকা অপারেশন করলে নিম্নলিখিত ফলাফল পাওয়া যাবে:
- রুট লেভেলে তালিকা অপারেশন
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 প্রকাশ করে যা অতিরিক্ত ফলাফল কখন আনতে হবে তার উপর নিয়ন্ত্রণ দেয়।
The pageToken encodes the path and version of the last item returned in the previous result. 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.
অন্যান্য সম্ভাব্য সমস্যা থেকে বোঝা যেতে পারে যে ব্যবহারকারীর কাছে সঠিক অনুমতি নেই। সমস্যা সম্পর্কে আরও তথ্য সমস্যা সমাধান করুন বিকল্পে পাওয়া যাবে।