C++ के लिए क्लाउड स्टोरेज वाली फ़ाइलें डाउनलोड करें

फायरबेस के लिए क्लाउड स्टोरेज आपको फायरबेस द्वारा प्रदान और प्रबंधित क्लाउड स्टोरेज बकेट से फ़ाइलों को जल्दी और आसानी से डाउनलोड करने की अनुमति देता है।

एक संदर्भ बनाएँ

किसी फ़ाइल को डाउनलोड करने के लिए, पहले उस फ़ाइल का क्लाउड स्टोरेज संदर्भ बनाएं जिसे आप डाउनलोड करना चाहते हैं।

आप अपने क्लाउड स्टोरेज बकेट के रूट में चाइल्ड पाथ जोड़कर एक संदर्भ बना सकते हैं, या आप क्लाउड स्टोरेज में किसी ऑब्जेक्ट को संदर्भित करने वाले मौजूदा gs:// या https:// यूआरएल से एक संदर्भ बना सकते हैं।

// 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. ऑनलाइन फ़ाइल का प्रतिनिधित्व करने वाला एक स्ट्रिंग यूआरएल जेनरेट करें

स्मृति में डाउनलोड करें

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 तर्क लेता है जिसका उपयोग आप अपने डाउनलोड को प्रबंधित करने के लिए कर सकते हैं। अधिक जानकारी के लिए डाउनलोड प्रबंधित करें देखें.

एक डाउनलोड यूआरएल जनरेट करें

यदि आपके पास पहले से ही यूआरएल पर आधारित डाउनलोड इंफ्रास्ट्रक्चर है, या आप साझा करने के लिए एक यूआरएल चाहते हैं, तो आप क्लाउड स्टोरेज संदर्भ पर GetDownloadUrl() विधि को कॉल करके किसी फ़ाइल के लिए डाउनलोड यूआरएल प्राप्त कर सकते हैं।

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

त्रुटियाँ संभालें

ऐसे कई कारण हैं जिनकी वजह से डाउनलोड पर त्रुटियां हो सकती हैं, जिनमें फ़ाइल का मौजूदा न होना, या उपयोगकर्ता के पास वांछित फ़ाइल तक पहुंचने की अनुमति न होना शामिल है। त्रुटियों के बारे में अधिक जानकारी डॉक्स के हैंडल एरर्स अनुभाग में पाई जा सकती है।

अगले कदम

आप क्लाउड स्टोरेज में संग्रहीत फ़ाइलों के लिए मेटाडेटा भी प्राप्त और अपडेट कर सकते हैं।