আপনার ফাইল Cloud Storage বাকেটে স্টোর করা হয়। এই বাকেটের ফাইলগুলি একটি হায়ারার্কিক্যাল স্ট্রাকচারে উপস্থাপন করা হয়, ঠিক যেমন আপনার লোকাল হার্ড ডিস্কের ফাইল সিস্টেম বা Firebase Realtime Database-এর ডেটা। কোনও ফাইলের রেফারেন্স তৈরি করার মাধ্যমে, আপনার অ্যাপ সেটির অ্যাক্সেস পায়। এই রেফারেন্সগুলি তারপরে ডেটা আপলোড বা ডাউনলোড করতে, মেটাডেটা পেতে বা আপডেট করতে বা ফাইলটি মুছতে ব্যবহার করা যেতে পারে। রেফারেন্স কোনও নির্দিষ্ট ফাইল বা হায়ারার্কিতে উচ্চ লেভেলের নোডকে নির্দেশ করতে পারে।
আপনি Firebase Realtime Database ব্যবহার করে থাকলে, এইসব পাথ আপনার কাছে খুব পরিচিত বলে মনে হবে। তবে, আপনার ফাইল ডেটা Cloud Storage-এ স্টোর করা হয়, Realtime Database-এ নয়।
রেফারেন্স তৈরি করা
কোনও ফাইল আপলোড, ডাউনলোড বা মুছে ফেলতে অথবা তার মেটাডেটা পেতে বা আপডেট করতে রেফারেন্স তৈরি করুন। রেফারেন্সকে ক্লাউডে থাকা কোনও ফাইলের পয়েন্টার হিসেবে ভাবা যেতে পারে। রেফারেন্স লাইটওয়েট, তাই আপনি যতগুলি প্রয়োজন ততগুলি তৈরি করতে পারবেন। এগুলি একাধিক অপারেশনের জন্য পুনর্ব্যবহারযোগ্য।
FirebaseStorage পরিষেবা ব্যবহার করে এবং এর reference পদ্ধতি কল করে রেফারেন্স তৈরি করা হয়।
Swift
// Get a reference to the storage service using the default Firebase App let storage = Storage.storage() // Create a storage reference from our storage service let storageRef = storage.reference()
Objective-C
// Get a reference to the storage service using the default Firebase App FIRStorage *storage = [FIRStorage storage]; // Create a storage reference from our storage service FIRStorageReference *storageRef = [storage reference];
আপনি ট্রি-এর নিচের দিকে কোনও লোকেশনের রেফারেন্স তৈরি করতে পারবেন,
যেমন 'images/space.jpg', আগে থেকে থাকা রেফারেন্সে child পদ্ধতি ব্যবহার করে।
Swift
// Create a child reference // imagesRef now points to "images" let imagesRef = storageRef.child("images") // Child references can also take paths delimited by '/' // spaceRef now points to "images/space.jpg" // imagesRef still points to "images" var spaceRef = storageRef.child("images/space.jpg") // This is equivalent to creating the full reference let storagePath = "\(your_firebase_storage_bucket)/images/space.jpg" spaceRef = storage.reference(forURL: storagePath)
Objective-C
// Create a child reference // imagesRef now points to "images" FIRStorageReference *imagesRef = [storageRef child:@"images"]; // Child references can also take paths delimited by '/' // spaceRef now points to "images/space.jpg" // imagesRef still points to "images" FIRStorageReference *spaceRef = [storageRef child:@"images/space.jpg"]; // This is equivalent to creating the full reference spaceRef = [storage referenceForURL:@"gs://<your-firebase-storage-bucket>/images/space.jpg"];
রেফারেন্সের সাহায্যে নেভিগেট করা
এছাড়াও, আমাদের ফাইল হায়ারার্কিতে উপরের দিকে নেভিগেট করতে আপনি parent এবং root পদ্ধতি ব্যবহার করতে পারেন। parent এক লেভেল উপরে নেভিগেট করে,
যেখানে root একদম উপরে নেভিগেট করে।
Swift
// Parent allows us to move to the parent of a reference // imagesRef now points to 'images' let imagesRef = spaceRef.parent() // Root allows us to move all the way back to the top of our bucket // rootRef now points to the root let rootRef = spaceRef.root()
Objective-C
// Parent allows us to move to the parent of a reference // imagesRef now points to 'images' imagesRef = [spaceRef parent]; // Root allows us to move all the way back to the top of our bucket // rootRef now points to the root FIRStorageReference *rootRef = [spaceRef root];
child, parent এবং root একাধিকবার একসাথে চেইন করা যেতে পারে, কারণ
প্রতিটি একটি রেফারেন্স রিটার্ন করে। ব্যতিক্রম হল root-এর parent, যা
হল nil।
Swift
// References can be chained together multiple times // earthRef points to "images/earth.jpg" let earthRef = spaceRef.parent()?.child("earth.jpg") // nilRef is nil, since the parent of root is nil let nilRef = spaceRef.root().parent()
Objective-C
// References can be chained together multiple times // earthRef points to "images/earth.jpg" FIRStorageReference *earthRef = [[spaceRef parent] child:@"earth.jpg"]; // nilRef is nil, since the parent of root is nil FIRStorageReference *nilRef = [[spaceRef root] parent];
রেফারেন্স প্রপার্টি
fullPath, name এবং bucket প্রপার্টি ব্যবহার করে
রেফারেন্সগুলি যে ফাইলগুলিকে নির্দেশ করে সেগুলি আরও ভালভাবে বুঝতে আপনি রেফারেন্সগুলি পরীক্ষা করতে পারেন। এইসব প্রপার্টি
ফাইলের সম্পূর্ণ পাথ, নাম ও বাকেট পায়।
Swift
// 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
Objective-C
// 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-এর মতো অন্যান্য টুলের সাথে ভালোভাবে কাজ করে না।
সম্পূর্ণ উদাহরণ
Swift
// Points to the root reference let storageRef = Storage.storage().reference() // Points to "images" let imagesRef = storageRef.child("images") // Points to "images/space.jpg" // Note that you can use variables to create child values let fileName = "space.jpg" let spaceRef = imagesRef.child(fileName) // File path is "images/space.jpg" let path = spaceRef.fullPath // File name is "space.jpg" let name = spaceRef.name // Points to "images" let images = spaceRef.parent()
Objective-C
// Points to the root reference FIRStorageReference *storageRef = [[FIRStorage storage] reference]; // Points to "images" FIRStorageReference *imagesRef = [storageRef child:@"images"]; // Points to "images/space.jpg" // Note that you can use variables to create child values NSString *fileName = @"space.jpg"; FIRStorageReference *spaceRef = [imagesRef child:fileName]; // File path is "images/space.jpg" NSString *path = spaceRef.fullPath; // File name is "space.jpg" NSString *name = spaceRef.name; // Points to "images" imagesRef = [spaceRef parent];
এরপর, Cloud Storage-এ কীভাবে ফাইল আপলোড করতে হয় তা জেনে নেওয়া যাক।