Cloud Storage for Firebase की मदद से, 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 से फ़ाइलें डाउनलोड की जा सकती हैं. इसके लिए, ये तीन तरीके अपनाए जा सकते हैं:
- मेमोरी में बफ़र में डाउनलोड करना
- डिवाइस पर किसी खास पाथ में डाउनलोड करना
- फ़ाइल को ऑनलाइन दिखाने के लिए, स्ट्रिंग यूआरएल जनरेट करें
मेमोरी में डाउनलोड करें
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);
इस समय, अनुरोध किया जा चुका है. हालांकि, फ़ाइल को पढ़ने से पहले, हमें फ़्यूचर के पूरा होने का इंतज़ार करना होगा. आम तौर पर, गेम लूप में चलते हैं और अन्य ऐप्लिकेशन की तुलना में कम कॉलबैक पर आधारित होते हैं. इसलिए, आम तौर पर आपको टास्क पूरा होने के लिए पोल करना होगा.
// 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 आर्ग्युमेंट होता है. इसका इस्तेमाल करके, डाउनलोड को मैनेज किया जा सकता है. ज़्यादा जानकारी के लिए, डाउनलोड मैनेज करना देखें.
डाउनलोड करने के लिए यूआरएल जनरेट करना
अगर आपके पास पहले से ही यूआरएल पर आधारित डाउनलोड इन्फ़्रास्ट्रक्चर है या आपको सिर्फ़ शेयर करने के लिए कोई यूआरएल चाहिए, तो 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<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 में सेव की गई फ़ाइलों के लिए, मेटाडेटा को ऐक्सेस और अपडेट भी किया जा सकता है.