Cloud Storage for C++ ব্যবহার করে Cloud Storage রেফারেন্স তৈরি করা

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

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

রেফারেন্স তৈরি করা

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

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

// Get a reference to the storage service, using the default Firebase App
Storage* storage = Storage::GetInstance(app);

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

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

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

// Child references can also take paths delimited by '/'
// space_ref now points to "images/space.jpg"
// images_ref still points to "images"
StorageReference space_ref = storage_ref.Child("images/space.jpg");

// This is equivalent to creating the full reference
StorageReference space_ref = 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
// images_ref now points to 'images'
StorageReference images_ref = space_ref.Parent();

// Root allows us to move all the way back to the top of our bucket
// root_ref now points to the root
StorageReference root_ref = space_ref.Root();

Child, Parent ও Root-কে একাধিকবার একসাথে চেইন করা যেতে পারে, কারণ প্রত্যেকটি একটি রেফারেন্স রিটার্ন করে। ব্যতিক্রম হল Root-এর Parent, যা একটি ভুল StorageReference।

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

// null_ref is null, since the parent of root is an invalid StorageReference
StorageReference null_ref = space_ref.Root().Parent();

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

full_path, name ও bucket পদ্ধতি ব্যবহার করে, ফাইলগুলি আরও ভালোভাবে বুঝতে আপনি রেফারেন্সগুলি পরীক্ষা করে দেখতে পারেন। এইসব পদ্ধতি ফাইলের সম্পূর্ণ পাথ, নাম ও বাকেট পায়।

// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
space_ref.full_path();

// Reference's name is the last segment of the full path: "space.jpg"
// This is analogous to the file name
space_ref.name();

// Reference's bucket is the name of the Cloud Storage bucket where files are stored
space_ref.bucket();

রেফারেন্সের উপর সীমাবদ্ধতা

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

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

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

Storage* storage = Storage::GetInstance(app);

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

// Points to "images"
StorageReference images_ref = storage_ref.Child("images");

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

// File path is "images/space.jpg"
std::string path = space_ref.full_path()

// File name is "space.jpg"
std::string name = space_ref.name()

// Points to "images"
StorageReference images_ref = space_ref.Parent();

পরবর্তী ধাপ

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