Cloud Storage के साथ C++ की फ़ाइलों को डाउनलोड करना

'Firebase के लिए Cloud Storage' की मदद से, Firebase की ओर से उपलब्ध कराए गए और मैनेज किए जा रहे Cloud Storage बकेट से फ़ाइलों को तेज़ी और आसानी से डाउनलोड किया जा सकता है.

रेफ़रंस बनाना

किसी फ़ाइल को डाउनलोड करने के लिए, पहले उस फ़ाइल के लिए Cloud Storage रेफ़रंस बनाएं जिसे आपको डाउनलोड करना है.

अपने Cloud Storage बकेट के रूट में चाइल्ड पाथ जोड़कर, रेफ़रंस बनाया जा सकता है. इसके अलावा, Cloud Storage में मौजूद किसी ऑब्जेक्ट के बारे में बताने वाले मौजूदा 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");

फ़ाइलें डाउनलोड करें

रेफ़रंस फ़ाइल मिलने के बाद, Cloud Storage से तीन तरीकों से फ़ाइलें डाउनलोड की जा सकती हैं:

  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 तर्क देता है जिसका इस्तेमाल अपने डाउनलोड को मैनेज करने के लिए किया जा सकता है. ज़्यादा जानकारी के लिए, डाउनलोड मैनेज करना देखें.

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

अगर आपके पास पहले से ही यूआरएल से जुड़ा डाउनलोड इन्फ़्रास्ट्रक्चर है या आपको सिर्फ़ यूआरएल शेयर करना है, तो किसी फ़ाइल के डाउनलोड का यूआरएल पाने के लिए, Cloud Storage रेफ़रंस पर 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);
}

गड़बड़ियां ठीक करना

डाउनलोड करते समय गड़बड़ियां होने की कई वजहें हो सकती हैं. इनमें, फ़ाइल का मौजूद न होना या उपयोगकर्ता को अपनी पसंद की फ़ाइल ऐक्सेस करने की अनुमति न होना शामिल है. गड़बड़ियों के बारे में ज़्यादा जानकारी, दस्तावेज़ों के गड़बड़ियां मैनेज करना सेक्शन में मिल सकती है.

अगले चरण

Cloud Storage में सेव की गई फ़ाइलों के लिए, मेटाडेटा को डाउनलोड और अपडेट किया जा सकता है.