Cloud Storage for Firebase আপনাকে 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 থেকে তিনটি উপায়ে ফাইল ডাউনলোড করতে পারবেন:
- মেমরির একটি বাফারে ডাউনলোড করুন
- ডিভাইসের একটি নির্দিষ্ট পথে ডাউনলোড করুন
- অনলাইনে ফাইলটি উপস্থাপন করে একটি স্ট্রিং URL তৈরি করুন।
মেমোরিতে ডাউনলোড করুন
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);
অনুরোধটি করার পর, কিন্তু ফাইলটি পড়ার আগে আমাদের ফিউচার সম্পূর্ণ হওয়ার জন্য অপেক্ষা করতে হবে। যেহেতু গেমগুলি সাধারণত একটি লুপে চলে এবং অন্যান্য অ্যাপ্লিকেশনের তুলনায় কম কলব্যাক চালিত হয়, তাই আপনি সাধারণত সম্পূর্ণতার জন্য পোল করবেন।
// 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-এর উপর ভিত্তি করে ডাউনলোড পরিকাঠামো থাকে, অথবা শুধুমাত্র একটি 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); }
হ্যান্ডেল ত্রুটি
ডাউনলোড করার সময় ত্রুটি দেখা দেওয়ার অনেক কারণ থাকতে পারে, যার মধ্যে রয়েছে ফাইলটি বিদ্যমান না থাকা, অথবা ব্যবহারকারীর পছন্দসই ফাইলটি অ্যাক্সেস করার অনুমতি না থাকা। ত্রুটি সম্পর্কে আরও তথ্য ডক্সের "হ্যান্ডেল এররস" বিভাগে পাওয়া যাবে।
পরবর্তী পদক্ষেপ
আপনি Cloud Storage সংরক্ষিত ফাইলগুলির মেটাডেটা পেতে এবং আপডেট করতে পারেন।