আপনার ফাইলগুলো একটি Cloud Storage বাকেটে সংরক্ষিত থাকে। এই বাকেটের ফাইলগুলো একটি স্তরক্রমিক কাঠামোতে সাজানো থাকে, ঠিক যেমন আপনার লোকাল হার্ড ডিস্কের ফাইল সিস্টেম বা Firebase Realtime Database ডেটা। কোনো ফাইলের রেফারেন্স তৈরি করার মাধ্যমে আপনার অ্যাপ সেটিতে অ্যাক্সেস পায়। এরপর এই রেফারেন্সগুলো ডেটা আপলোড বা ডাউনলোড করতে, মেটাডেটা পেতে বা আপডেট করতে, অথবা ফাইলটি মুছে ফেলতে ব্যবহার করা যায়। একটি রেফারেন্স কোনো নির্দিষ্ট ফাইলকে অথবা স্তরক্রমের উচ্চতর কোনো নোডকে নির্দেশ করতে পারে।
আপনি যদি Firebase Realtime Database ব্যবহার করে থাকেন, তাহলে এই পাথগুলো আপনার কাছে খুবই পরিচিত মনে হবে। তবে, আপনার ফাইলের ডেটা Cloud Storage সংরক্ষিত থাকে, Realtime Database নয় ।
একটি রেফারেন্স তৈরি করুন
ফাইল আপলোড বা ডাউনলোড করতে, ফাইল ডিলিট করতে, অথবা মেটাডেটা পেতে বা আপডেট করতে, আপনাকে অবশ্যই সেই ফাইলটির একটি রেফারেন্স তৈরি করতে হবে যার উপর আপনি কাজ করতে চান। একটি রেফারেন্সকে ক্লাউডে থাকা কোনো ফাইলের একটি পয়েন্টার হিসেবে ভাবা যেতে পারে। রেফারেন্সগুলো হালকা প্রকৃতির, তাই আপনার প্রয়োজন অনুযায়ী যত খুশি তৈরি করতে পারেন, এবং এগুলো একাধিক কাজের জন্য পুনরায় ব্যবহারযোগ্যও।
একটি রেফারেন্স তৈরি করতে, getStorage() ব্যবহার করে স্টোরেজ সার্ভিসের একটি ইনস্ট্যান্স নিন, তারপর সার্ভিসটিকে আর্গুমেন্ট হিসেবে দিয়ে 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();
আপনি ট্রি-এর নিচের কোনো অবস্থানের, যেমন 'images/space.jpg' একটি রেফারেন্স তৈরি করতে পারেন ref() কল করার সময় এই পাথটিকে দ্বিতীয় আর্গুমেন্ট হিসেবে পাস করার মাধ্যমে।
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 কীভাবে ফাইল আপলোড করতে হয়।