আপনার ফাইল Cloud Storage for Firebase বাকেটে স্টোর করা হয়। এই বাকেটের ফাইলগুলি একটি হায়ারার্কিক্যাল স্ট্রাকচারে উপস্থাপন করা হয়, ঠিক আপনার লোকাল হার্ড ডিস্কের ফাইল সিস্টেমের মতো।
কোনও ফাইলের রেফারেন্স তৈরি করার মাধ্যমে, আপনার অ্যাপ সেটির অ্যাক্সেস পায়। রেফারেন্স নির্দিষ্ট ফাইল বা হায়ারার্কির উচ্চ-লেভেল নোডকে পয়েন্ট করতে পারে। আপনি রেফারেন্সকে ক্লাউডে ফাইল বা নোডের পয়েন্টার হিসেবে ভাবতে পারেন। রেফারেন্স তৈরি করার পরে, আপনি এটি ব্যবহার করে অপারেশন (যেমন ডেটা আপলোড বা ডাউনলোড করা, মেটাডেটা পাওয়া বা আপডেট করা অথবা ফাইল মুছে ফেলা) সম্পন্ন করতে পারবেন।
এই পৃষ্ঠায় Cloud Storage for Firebase SDK ব্যবহার করে কীভাবে রেফারেন্স তৈরি করা ও কাজ করা যায় তা বর্ণনা করা হয়েছে।
রেফারেন্স তৈরি করা
কোনও ফাইলে অপারেশন করতে, আপনাকে প্রথমে ফাইলটির একটি রেফারেন্স তৈরি করতে হবে। রেফারেন্স লাইটওয়েট হয়, তাই আপনি যতগুলি প্রয়োজন ততগুলি তৈরি করতে পারবেন। এগুলি একাধিক অপারেশনের জন্য পুনর্ব্যবহারযোগ্য।
রেফারেন্স তৈরি করতে, getStorage() ব্যবহার করে Storage পরিষেবার ইনস্ট্যান্স পান
এবং তারপর আর্গুমেন্ট হিসেবে পরিষেবার সাথে ref() কল করুন।
এই রেফারেন্স আপনার Cloud Storage বাকেটের রুটকে নির্দেশ করে।
Web
import { getStorage, ref } from "firebase/storage"; // Get a reference to the storage service, which is used to create references in your storage bucket const storage = getStorage(); // Create a storage reference from our storage service const storageRef = ref(storage);
Web
// Get a reference to the storage service, which is used to create references in your storage bucket var storage = firebase.storage(); // Create a storage reference from our storage service var storageRef = storage.ref();
আপনি ট্রি-এর নিচের দিকে কোনও লোকেশনের রেফারেন্স তৈরি করতে পারেন,
যেমন ref() কল করার সময় দ্বিতীয় আর্গুমেন্ট হিসেবে এই পাথ পাস করে
'images/space.jpg'।
Web
import { getStorage, ref } from "firebase/storage"; const storage = getStorage(); // Create a child reference const imagesRef = ref(storage, 'images'); // imagesRef now points to 'images' // Child references can also take paths delimited by '/' const spaceRef = ref(storage, 'images/space.jpg'); // spaceRef now points to "images/space.jpg" // imagesRef still points to "images"
Web
// Create a child reference var imagesRef = storageRef.child('images'); // imagesRef now points to 'images' // Child references can also take paths delimited by '/' var spaceRef = storageRef.child('images/space.jpg'); // spaceRef now points to "images/space.jpg" // imagesRef still points to "images"
রেফারেন্সের সাহায্যে নেভিগেট করা
এছাড়াও, আপনি parent এবং root প্রপার্টি ব্যবহার করে ফাইল হায়ারার্কির উপরের দিকে
নেভিগেট করতে পারবেন। parent এক লেভেল উপরে নেভিগেট করে,
যেখানে root একদম উপরে নেভিগেট করে।
Web
import { getStorage, ref } from "firebase/storage"; const storage = getStorage(); const spaceRef = ref(storage, 'images/space.jpg'); // Parent allows us to move to the parent of a reference const imagesRef = spaceRef.parent; // imagesRef now points to 'images' // Root allows us to move all the way back to the top of our bucket const rootRef = spaceRef.root; // rootRef now points to the root
Web
// Parent allows us to move to the parent of a reference var imagesRef = spaceRef.parent; // imagesRef now points to 'images' // Root allows us to move all the way back to the top of our bucket var rootRef = spaceRef.root; // rootRef now points to the root
child(), parent এবং root একাধিকবার একসাথে চেইন করা যেতে পারে, কারণ
প্রতিটি একটি রেফারেন্স রিটার্ন করে। ব্যতিক্রম হল root-এর parent, যা
হল null।
Web
import { getStorage, ref } from "firebase/storage"; const storage = getStorage(); const spaceRef = ref(storage, 'images/space.jpg'); // References can be chained together multiple times const earthRef = ref(spaceRef.parent, 'earth.jpg'); // earthRef points to 'images/earth.jpg' // nullRef is null, since the parent of root is null const nullRef = spaceRef.root.parent;
Web
// References can be chained together multiple times var earthRef = spaceRef.parent.child('earth.jpg'); // earthRef points to 'images/earth.jpg' // nullRef is null, since the parent of root is null var nullRef = spaceRef.root.parent;
রেফারেন্স প্রপার্টি
fullPath, name এবং bucket প্রপার্টি ব্যবহার করে
রেফারেন্সগুলি যে ফাইলগুলিকে নির্দেশ করে সেগুলি আরও ভালভাবে বুঝতে আপনি রেফারেন্সগুলি পরীক্ষা করতে পারেন। এইসব প্রপার্টি
ফাইলের সম্পূর্ণ পাথ, ফাইলের নাম
এবং ফাইলটি যে বাকেটে স্টোর করা আছে সেটি পায়।
Web
import { getStorage, ref } from "firebase/storage"; const storage = getStorage(); const spaceRef = ref(storage, 'images/space.jpg'); // Reference's path is: 'images/space.jpg' // This is analogous to a file path on disk spaceRef.fullPath; // Reference's name is the last segment of the full path: 'space.jpg' // This is analogous to the file name spaceRef.name; // Reference's bucket is the name of the storage bucket where files are stored spaceRef.bucket;
Web
// Reference's path is: 'images/space.jpg' // This is analogous to a file path on disk spaceRef.fullPath; // Reference's name is the last segment of the full path: 'space.jpg' // This is analogous to the file name spaceRef.name; // Reference's bucket is the name of the storage bucket where files are stored spaceRef.bucket;
রেফারেন্সের উপর সীমাবদ্ধতা
রেফারেন্স পাথ এবং নামে যেকোনও ক্রমের সঠিক ইউনিকোড অক্ষর থাকতে পারে, তবে কিছু বিধিনিষেধ আরোপ করা হয়েছে, যার মধ্যে রয়েছে:
- UTF-8 এনকোড করা হলে
reference.fullPath-এর মোট দৈর্ঘ্য ১ থেকে ১০২৪ বাইটের মধ্যে হতে হবে। - কোনও নতুন লাইন বা লাইন ফিড অক্ষর নেই।
#,[,],*বা?ব্যবহার করা এড়িয়ে চলুন, কারণ এগুলি Firebase Realtime Database বা gsutil-এর মতো অন্যান্য টুলের সাথে ভালভাবে কাজ করে না।
সম্পূর্ণ উদাহরণ
Web
import { getStorage, ref } from "firebase/storage"; const storage = getStorage(); // Points to the root reference const storageRef = ref(storage); // Points to 'images' const imagesRef = ref(storageRef, 'images'); // Points to 'images/space.jpg' // Note that you can use variables to create child values const fileName = 'space.jpg'; const spaceRef = ref(imagesRef, fileName); // File path is 'images/space.jpg' const path = spaceRef.fullPath; // File name is 'space.jpg' const name = spaceRef.name; // Points to 'images' const imagesRefAgain = spaceRef.parent;
Web
// Points to the root reference var storageRef = firebase.storage().ref(); // Points to 'images' var imagesRef = storageRef.child('images'); // Points to 'images/space.jpg' // Note that you can use variables to create child values var fileName = 'space.jpg'; var spaceRef = imagesRef.child(fileName); // File path is 'images/space.jpg' var path = spaceRef.fullPath; // File name is 'space.jpg' var name = spaceRef.name; // Points to 'images' var imagesRef = spaceRef.parent;
ফাইল কীভাবে আপলোড করতে হয় তা Cloud Storage-এ জানুন।