ইউনিটির জন্য ক্লাউড স্টোরেজের সাথে একটি ক্লাউড স্টোরেজ রেফারেন্স তৈরি করুন

আপনার ফাইলগুলো একটি Cloud Storage বাকেটে সংরক্ষিত থাকে। এই বাকেটের ফাইলগুলো একটি স্তরক্রমিক কাঠামোতে সাজানো থাকে, ঠিক যেমন আপনার লোকাল হার্ড ডিস্কের ফাইল সিস্টেম বা Firebase Realtime Database ডেটা। কোনো ফাইলের রেফারেন্স তৈরি করার মাধ্যমে আপনার অ্যাপ সেটিতে অ্যাক্সেস পায়। এরপর এই রেফারেন্সগুলো ডেটা আপলোড বা ডাউনলোড করতে, মেটাডেটা পেতে বা আপডেট করতে, অথবা ফাইলটি মুছে ফেলতে ব্যবহার করা যায়। একটি রেফারেন্স কোনো নির্দিষ্ট ফাইলকে অথবা স্তরক্রমের উচ্চতর কোনো নোডকে নির্দেশ করতে পারে।

আপনি যদি Firebase Realtime Database ব্যবহার করে থাকেন, তাহলে এই পাথগুলো আপনার কাছে খুবই পরিচিত মনে হবে। তবে, আপনার ফাইলের ডেটা Cloud Storage সংরক্ষিত থাকে, Realtime Database নয়

একটি রেফারেন্স তৈরি করুন

কোনো ফাইল আপলোড, ডাউনলোড বা ডিলিট করতে, অথবা এর মেটাডেটা পেতে বা আপডেট করতে একটি রেফারেন্স তৈরি করুন। একটি রেফারেন্সকে ক্লাউডে থাকা কোনো ফাইলের একটি পয়েন্টার হিসেবে ভাবা যেতে পারে। রেফারেন্সগুলো হালকা হওয়ায়, আপনার প্রয়োজন অনুযায়ী যত খুশি তৈরি করতে পারেন। এছাড়াও, এগুলো একাধিক অপারেশনের জন্য পুনরায় ব্যবহারযোগ্য।

আপনার Firebase অ্যাপে Firebase.Storage.FirebaseStorage সার্ভিস থেকে GetReferenceFromUrl() মেথডটি কল করে এবং gs://<your-cloud-storage-bucket> ফর্মের একটি URL পাস করে রেফারেন্স তৈরি করা হয়। আপনি Firebase কনসোলের স্টোরেজ সেকশনে এই URL-টি খুঁজে পাবেন।

// Get a reference to the storage service, using the default Firebase App
FirebaseStorage storage = FirebaseStorage.DefaultInstance;

// Create a storage reference from our storage service
StorageReference storageRef =
    storage.GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");

বিদ্যমান কোনো রেফারেন্সের child মেথড ব্যবহার করে আপনি ট্রি-এর নিচের কোনো লোকেশনের, যেমন 'images/space.jpg' , একটি রেফারেন্স তৈরি করতে পারেন।

// Create a child reference
// imagesRef now points to "images"
StorageReference imagesRef = storageRef.Child("images");

// Child references can also take paths delimited by '/' such as:
// "images/space.jpg".
StorageReference spaceRef = imagesRef.Child("space.jpg");
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"

// This is equivalent to creating the full referenced
StorageReference spaceRefFull = storage.GetReferenceFromUrl(
    "gs://<your-cloud-storage-bucket>/images/space.jpg");

আমাদের ফাইল হায়ারার্কিতে উপরে যাওয়ার জন্য আপনি Parent এবং Root মেথডও ব্যবহার করতে পারেন। Parent এক স্তর উপরে নিয়ে যায়, আর Root একেবারে শীর্ষে নিয়ে যায়।

// Parent allows us to move to the parent of a reference
// imagesRef now points to 'images'
StorageReference imagesRef = spaceRef.Parent;

// Root allows us to move all the way back to the top of our bucket
// rootRef now points to the root
StorageReference rootRef = spaceRef.Root;

Child , Parent , এবং Root একাধিকবার একসাথে ব্যবহার করা যায়, কারণ প্রতিটি একটি রেফারেন্স প্রদান করে। এর ব্যতিক্রম হলো Root এর Parent , যা একটি অবৈধ StorageReference

// References can be chained together multiple times
// earthRef points to "images/earth.jpg"
StorageReference earthRef =
    spaceRef.Parent.Child("earth.jpg");

// nullRef is null since the parent of root is an invalid StorageReference
StorageReference nullRef = spaceRef.Root.Parent;

রেফারেন্স পদ্ধতি

Path , Name , এবং Bucket প্রপার্টিগুলো ব্যবহার করে আপনি রেফারেন্সগুলো পরীক্ষা করে সেগুলোর মাধ্যমে নির্দেশিত ফাইলগুলোকে আরও ভালোভাবে বুঝতে পারেন। এই প্রপার্টিগুলো ফাইলের সম্পূর্ণ পাথ, নাম এবং বাকেট প্রদান করে।

// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
string path = spaceRef.Path;

// Reference's name is the last segment of the full path: "space.jpg"
// This is analogous to the file name
string name = spaceRef.Name;

// Reference's bucket is the name of the storage bucket where files are stored
string bucket = spaceRef.Bucket;

তথ্যসূত্রের সীমাবদ্ধতা

রেফারেন্স পাথ এবং নামে যেকোনো বৈধ ইউনিকোড অক্ষরের অনুক্রম থাকতে পারে, তবে কিছু নির্দিষ্ট বিধিনিষেধ আরোপ করা হয়, যার মধ্যে রয়েছে:

  1. UTF-8 এনকোড করা হলে reference.Path এর মোট দৈর্ঘ্য অবশ্যই ১ থেকে ১০২৪ বাইটের মধ্যে হতে হবে।
  2. কোনো ক্যারেজ রিটার্ন বা লাইন ফিড ক্যারেক্টার নেই।
  3. # , [ , ] , * , বা ? ব্যবহার করা থেকে বিরত থাকুন, কারণ এগুলো Firebase Realtime Database বা gsutil-এর মতো অন্যান্য টুলের সাথে ভালোভাবে কাজ করে না।

সম্পূর্ণ উদাহরণ

FirebaseStorage storage = FirebaseStorage.DefaultInstance;

// Points to the root reference
StorageReference storageRef =
    storage.GetReferenceFromUrl("gs://<your-bucket-name>");

// Points to "images"
StorageReference imagesRef = storageRef.Child("images");

// Points to "images/space.jpg"
// Note that you can use variables to create child values
string filename = "space.jpg";
StorageReference spaceRef = imagesRef.Child(filename);

// File path is "images/space.jpg"
string path = spaceRef.Path;

// File name is "space.jpg"
string name = spaceRef.Name;

// Points to "images"
StorageReference imagesRef = spaceRef.Parent;

পরবর্তী পদক্ষেপ

এরপর, চলুন জেনে নিই Cloud Storage কীভাবে ফাইল আপলোড করতে হয়।