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