Cloud Storage for Firebase আপনাকে আপনার Cloud Storage বাকেটের বিষয়বস্তু তালিকাভুক্ত করতে দেয়। SDK গুলি বর্তমান Cloud Storage রেফারেন্সের অধীনে থাকা বস্তুর আইটেম এবং উপসর্গ উভয়ই ফেরত দেয়।
যেসব প্রকল্প তালিকা API ব্যবহার করে Cloud Storage for Firebase প্রয়োজন। যদি আপনার একটি বিদ্যমান Firebase প্রকল্প থাকে, তাহলে নিরাপত্তা নিয়ম নির্দেশিকাতে ধাপগুলি অনুসরণ করুন।
list() Google Cloud Storage লিস্ট এপিআই ব্যবহার করে। Cloud Storage for Firebase , আমরা / একটি ডিলিমিটার হিসেবে ব্যবহার করি, যা আমাদের ফাইল সিস্টেমের শব্দার্থবিদ্যা অনুকরণ করতে সাহায্য করে। বৃহৎ, শ্রেণিবদ্ধ Cloud Storage বাকেটের দক্ষ ট্র্যাভার্সালের জন্য, লিস্ট এপিআই প্রিফিক্স এবং আইটেমগুলি আলাদাভাবে ফেরত দেয়। উদাহরণস্বরূপ, যদি আপনি একটি ফাইল /images/uid/file1 আপলোড করেন,
-
root.child('images').listAll()/images/uidএকটি প্রিফিক্স হিসেবে রিটার্ন করবে। -
root.child('images/uid').listAll()ফাইলটিকে একটি আইটেম হিসেবে ফেরত দেবে।
Cloud Storage for Firebase এমন অবজেক্ট পাথ ফেরত দেয় না যেখানে পরপর দুটি / s থাকে অথবা /. দিয়ে শেষ হয়। উদাহরণস্বরূপ, এমন একটি বাকেট বিবেচনা করুন যেখানে নিম্নলিখিত অবজেক্ট রয়েছে:
-
correctPrefix/happyItem -
wrongPrefix//sadItem -
lonelyItem/
এই বাকেটের আইটেমগুলির তালিকার ক্রিয়াকলাপগুলি নিম্নলিখিত ফলাফল দেবে:
- রুটে থাকা তালিকার ক্রিয়াকলাপটি
correctPrefix,wrongPrefixএবংlonelyItemএর রেফারেন্সগুলিকেprefixesহিসেবে ফেরত দেয়। -
correctPrefix/এ তালিকার ক্রিয়াকলাপcorrectPrefix/happyItemএর রেফারেন্সগুলিকেitemsহিসাবে ফেরত দেয়। -
wrongPrefix/এ তালিকার ক্রিয়াকলাপটি কোনও রেফারেন্স ফেরত দেয় না কারণwrongPrefix//sadItemপরপর দুটি/s রয়েছে। -
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.
অন্যান্য সম্ভাব্য ত্রুটিগুলি ইঙ্গিত দিতে পারে যে ব্যবহারকারীর কাছে সঠিক অনুমতি নেই। ত্রুটি সম্পর্কে আরও তথ্য হ্যান্ডেল ত্রুটিগুলিতে পাওয়া যাবে।