Cloud Storage for Firebase আপনাকে ফায়ারবেস দ্বারা সরবরাহকৃত ও পরিচালিত Cloud Storage বাকেট থেকে দ্রুত এবং সহজে ফাইল ডাউনলোড করার সুযোগ দেয়।
একটি রেফারেন্স তৈরি করুন
ফাইল ডাউনলোড করতে, প্রথমে আপনি যে ফাইলটি ডাউনলোড করতে চান সেটির জন্য একটি Cloud Storage রেফারেন্স তৈরি করুন ।
আপনি আপনার Cloud Storage বাকেটের রুটে চাইল্ড পাথ যুক্ত করে একটি রেফারেন্স তৈরি করতে পারেন, অথবা Cloud Storage কোনো অবজেক্টকে রেফারেন্স করে এমন একটি বিদ্যমান gs:// বা https:// URL থেকেও একটি রেফারেন্স তৈরি করতে পারেন।
// Create a reference with an initial file path and name StorageReference path_reference = storage->GetReference("images/stars.jpg"); // Create a reference from a Cloud Storage URI StorageReference gs_reference = storage->GetReferenceFromUrl("gs://bucket/images/stars.jpg"); // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! StorageReference https_reference = storage->GetReferenceFromUrl("https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg");
ফাইল ডাউনলোড করুন
একবার আপনার কাছে রেফারেন্স থাকলে, আপনি Cloud Storage থেকে তিনভাবে ফাইল ডাউনলোড করতে পারেন:
- মেমরির একটি বাফারে ডাউনলোড করুন
- ডিভাইসের একটি নির্দিষ্ট পাথে ডাউনলোড করুন
- অনলাইনে ফাইলটির প্রতিনিধিত্বকারী একটি স্ট্রিং ইউআরএল তৈরি করুন।
মেমরিতে ডাউনলোড করুন
GetBytes() মেথড ব্যবহার করে ফাইলটিকে মেমরির একটি বাইট বাফারে ডাউনলোড করুন। দ্রুত একটি ফাইল ডাউনলোড করার এটিই সবচেয়ে সহজ উপায়, কিন্তু এর জন্য আপনার ফাইলের সম্পূর্ণ বিষয়বস্তু মেমরিতে লোড করতে হয়। আপনি যদি আপনার অ্যাপের উপলব্ধ মেমরির চেয়ে বড় কোনো ফাইলের জন্য অনুরোধ করেন, তাহলে আপনার অ্যাপটি ক্র্যাশ করবে। মেমরি সংক্রান্ত সমস্যা এড়াতে, সর্বোচ্চ আকার এমন একটি পরিমাণে সেট করুন যা আপনার অ্যাপ সামলাতে পারবে বলে আপনি জানেন, অথবা অন্য কোনো ডাউনলোড পদ্ধতি ব্যবহার করুন।
// Create a reference to the file you want to download StorageReference island_ref = storage_ref.Child("images/island.jpg"); // Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes) const size_t kMaxAllowedSize = 1 * 1024 * 1024 int8_t byte_buffer[kMaxAllowedSize]; firebase::Future<size_t> future = island_ref.GetBytes(byte_buffer, kMaxAllowedSize);
এই পর্যায়ে অনুরোধটি করা হয়েছে, কিন্তু ফাইলটি পড়ার আগে আমাদের Future-এর কাজ শেষ হওয়ার জন্য অপেক্ষা করতে হবে। যেহেতু গেমগুলো সাধারণত একটি লুপের মধ্যে চলে এবং অন্যান্য অ্যাপ্লিকেশনের তুলনায় কম কলব্যাক-নির্ভর, তাই এর কাজ শেষ হয়েছে কিনা তা জানার জন্য সাধারণত পোলিং করা হয়।
// In the game loop that polls for the result... if (future.status() != firebase::kFutureStatusPending) { if (future.status() != firebase::kFutureStatusComplete) { LogMessage("ERROR: GetBytes() returned an invalid future."); // Handle the error... } else if (future.Error() != firebase::storage::kErrorNone) { LogMessage("ERROR: GetBytes() returned error %d: %s", future.Error(), future.error_message()); // Handle the error... } else { // byte_buffer is now populated with data for "images/island.jpg" } }
স্থানীয় ফাইলে ডাউনলোড করুন
GetFile() মেথডটি সরাসরি স্থানীয় ডিভাইসে একটি ফাইল ডাউনলোড করে। আপনার ব্যবহারকারীরা যদি অফলাইনে থাকাকালীন ফাইলটি অ্যাক্সেস করতে চান অথবা অন্য কোনো অ্যাপে শেয়ার করতে চান, তবে এটি ব্যবহার করুন।
// Create a reference to the file you want to download StorageReference islandRef = storage_ref.Child("images/island.jpg"]; // Create local filesystem URL const char* local_url = "file:///local/images/island.jpg"; // Download to the local filesystem Future<size_t> future = islandRef.GetFile(local_url); // Wait for Future to complete... if (future.Error() != firebase::storage::kErrorNone) { // Uh-oh, an error occurred! } else { // The file has been downloaded to local file URL "images/island.jpg" }
GetFile() একটি ঐচ্ছিক Controller আর্গুমেন্ট গ্রহণ করে, যা ব্যবহার করে আপনি আপনার ডাউনলোড পরিচালনা করতে পারেন। আরও তথ্যের জন্য ‘ডাউনলোড পরিচালনা’ দেখুন।
একটি ডাউনলোড ইউআরএল তৈরি করুন
আপনার যদি আগে থেকেই URL-ভিত্তিক ডাউনলোড পরিকাঠামো থাকে, অথবা আপনি শুধু শেয়ার করার জন্য একটি URL চান, তাহলে একটি Cloud Storage রেফারেন্সে GetDownloadUrl() মেথড কল করে ফাইলটির ডাউনলোড URL পেতে পারেন।
// Create a reference to the file you want to download StorageReference stars_ref = storage_ref.Child("images/stars.jpg"); // Fetch the download URL firebase::Future<std::string> future = stars_ref.GetDownloadUrl(); // Wait for Future to complete... if (future.Error() != firebase::storage::kErrorNone) { // Uh-oh, an error occurred! } else { // Get the download URL for 'images/stars.jpg' std::string download_url = future.Result(); }
ডাউনলোডগুলি পরিচালনা করুন
ডাউনলোড শুরু করার পাশাপাশি, আপনি Controller এর Pause() , Resume() , এবং Cancel() মেথডগুলো ব্যবহার করে ডাউনলোড পজ, রিজুম এবং ক্যানসেল করতে পারেন, যেগুলো আপনি ঐচ্ছিকভাবে GetBytes() বা GetFile() মেথডগুলোতে পাস করতে পারেন।
// Start downloading a file Controller controller; storage_ref.Child("images/mountains.jpg").GetFile(local_file, nullptr, &controller); // Pause the download controller.Pause(); // Resume the download controller.Resume(); // Cancel the download controller.Cancel();
ডাউনলোডের অগ্রগতি নিরীক্ষণ করুন
ডাউনলোডের অগ্রগতি পর্যবেক্ষণ করার জন্য আপনি ডাউনলোডের সাথে লিসেনার সংযুক্ত করতে পারেন।
class MyListener : public firebase::storage::Listener { public: virtual void OnProgress(firebase::storage::Controller* controller) { // A progress event occurred } }; { // Start uploading a file MyEventListener my_listener; storage_ref.Child("images/mountains.jpg").GetFile(local_file, my_listener); }
ত্রুটিগুলি পরিচালনা করুন
ডাউনলোড করার সময় বিভিন্ন কারণে ত্রুটি ঘটতে পারে, যার মধ্যে রয়েছে ফাইলটির অস্তিত্ব না থাকা, অথবা কাঙ্ক্ষিত ফাইলটি অ্যাক্সেস করার জন্য ব্যবহারকারীর অনুমতি না থাকা। ত্রুটি সম্পর্কে আরও তথ্য ডকুমেন্টেশনের ' Handle Errors' বিভাগে পাওয়া যাবে।
পরবর্তী পদক্ষেপ
আপনি Cloud Storage সংরক্ষিত ফাইলগুলির মেটাডেটাও পেতে এবং আপডেট করতে পারেন।