C++ এর জন্য ক্লাউড স্টোরেজ দিয়ে ফাইল ডাউনলোড করুন

Firebase-এর জন্য ক্লাউড স্টোরেজ আপনাকে Firebase দ্বারা প্রদত্ত এবং পরিচালিত ক্লাউড স্টোরেজ বাকেট থেকে দ্রুত এবং সহজে ফাইল ডাউনলোড করতে দেয়।

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

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

আপনি আপনার ক্লাউড স্টোরেজ বাকেটের রুটে চাইল্ড পাথ যুক্ত করে একটি রেফারেন্স তৈরি করতে পারেন, অথবা আপনি ক্লাউড স্টোরেজের একটি অবজেক্টকে উল্লেখ করে বিদ্যমান 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");

ফাইল ডাউনলোড করুন

একবার আপনার কাছে একটি রেফারেন্স থাকলে, আপনি তিনটি উপায়ে ক্লাউড স্টোরেজ থেকে ফাইলগুলি ডাউনলোড করতে পারেন:

  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 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 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 শেয়ার করতে চান, তাহলে আপনি ক্লাউড স্টোরেজ রেফারেন্সে 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 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);
}

হ্যান্ডেল ত্রুটি

ফাইলটি বিদ্যমান না থাকা বা ব্যবহারকারীর পছন্দসই ফাইলটি অ্যাক্সেস করার অনুমতি না থাকা সহ ডাউনলোডে ত্রুটি ঘটতে পারে এমন অনেকগুলি কারণ রয়েছে৷ ত্রুটিগুলি সম্পর্কে আরও তথ্য ডক্সের হ্যান্ডেল ত্রুটি বিভাগে পাওয়া যাবে৷

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

এছাড়াও আপনি ক্লাউড স্টোরেজে সংরক্ষিত ফাইলগুলির জন্য মেটাডেটা পেতে এবং আপডেট করতে পারেন৷