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

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

  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);

At the point the request has been made but we have to wait for the Future to complete before we can read the file. যেহেতু গেম সাধারণত লুপে চলে এবং অন্যান্য অ্যাপ্লিকেশনের তুলনায় কম কলব্যাক-চালিত হয়, তাই আপনি সাধারণত সম্পূর্ণ করার জন্য পোল করবেন।

// 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-এ স্টোর করা ফাইলের জন্য মেটাডেটা পেতে ও আপডেট করতে পারবেন।