Cloud Storage for C++ ব্যবহার করে ফাইল ডাউনলোড করা

Cloud Storage for Firebase আপনাকে Firebase-এর প্রদান ও ম্যানেজ করা Cloud Storage বাকেট থেকে দ্রুত ও সহজে ফাইল ডাউনলোড করতে দেয়।

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

কোনও ফাইল ডাউনলোড করতে, প্রথমে আপনি যে ফাইল ডাউনলোড করতে চান সেটির Cloud Storage রেফারেন্স তৈরি করুন।

আপনার Cloud Storage বাকেটের রুটে চাইল্ড পাথ যোগ করে আপনি রেফারেন্স তৈরি করতে পারেন অথবা আগে থেকে থাকা gs:// বা https:// URL থেকে রেফারেন্স তৈরি করতে পারেন যা Cloud Storage-এ কোনও অবজেক্টকে রেফার করছে।

// 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 তিনটি উপায়ে ফাইল ডাউনলোড করতে পারবেন:

  1. মেমরিতে বাফার হিসেবে ডাউনলোড করা
  2. ডিভাইসে নির্দিষ্ট পাথে ডাউনলোড করা
  3. অনলাইনে ফাইলটি দেখানোর জন্য একটি স্ট্রিং 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);

অনুরোধ করা হয়েছে, তবে ফাইলটি পড়ার আগে 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-এর উপর ভিত্তি করে ডাউনলোড ইনফ্রাস্ট্রাকচার থাকে অথবা আপনি যদি শুধু শেয়ার করার জন্য 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();
}

ডাউনলোড ম্যানেজ করা

ডাউনলোড শুরু করা ছাড়াও, আপনি Pause(), Resume() এবং Cancel() পদ্ধতি ব্যবহার করে ডাউনলোড পজ, আবার শুরু এবং বাতিল করতে পারবেন Controller, যা আপনি ঐচ্ছিকভাবে 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-এ সেভ করা ফাইলের জন্য আপনি মেটাডেটা পেতে ও আপডেট করতে পারবেন।