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

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

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

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

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

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

डाउनलोड मैनेज करें

डाउनलोड शुरू करने के अलावा, आपके पास डाउनलोड रोकने, उन्हें फिर से शुरू करने, और रद्द करने का विकल्प भी होता है चालू है 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 में सेव की गई फ़ाइलों के लिए.