आपकी फ़ाइलें Cloud Storage बकेट में सेव होती हैं. इस बकेट में मौजूद फ़ाइलें, हैरारकी के हिसाब से स्ट्रक्चर में दिखती हैं. ठीक वैसे ही जैसे आपकी लोकल हार्ड डिस्क पर मौजूद फ़ाइल सिस्टम या Firebase Realtime Database में मौजूद डेटा दिखता है. किसी फ़ाइल का रेफ़रंस बनाकर, आपका ऐप्लिकेशन उसे ऐक्सेस कर सकता है. इन रेफ़रंस का इस्तेमाल, डेटा अपलोड या डाउनलोड करने, मेटाडेटा पाने या अपडेट करने या फ़ाइल को मिटाने के लिए किया जा सकता है. रेफ़रंस, किसी खास फ़ाइल या हैरारकी में मौजूद किसी हाई लेवल नोड पर ले जा सकता है.
अगर आपने Firebase Realtime Database का इस्तेमाल किया है, तो आपको ये पाथ बहुत जाने-पहचाने लगेंगे. हालांकि, आपकी फ़ाइल का डेटा Cloud Storage में सेव होता है, Realtime Database में नहीं.
कोई रेफ़रंस फ़ाइल बनाना
किसी फ़ाइल को अपलोड, डाउनलोड या मिटाने के लिए रेफ़रंस बनाएं. इसके अलावा, फ़ाइल का मेटाडेटा पाने या उसे अपडेट करने के लिए भी रेफ़रंस बनाएं. रेफ़रंस को क्लाउड में मौजूद किसी फ़ाइल के पॉइंटर के तौर पर समझा जा सकता है. रेफ़रंस काफ़ी कम जगह लेते हैं. इसलिए, जितने चाहें उतने रेफ़रंस बनाए जा सकते हैं. इन्हें कई ऑपरेशंस के लिए फिर से इस्तेमाल किया जा सकता है.
रेफ़रंस, आपके Firebase ऐप्लिकेशन पर storage सेवा से बनाए जाते हैं. इसके लिए, GetReferenceFromUrl() तरीके को कॉल किया जाता है और gs://<your-cloud-storage-bucket> फ़ॉर्म का यूआरएल पास किया जाता है. यह यूआरएल, Firebase कंसोल के स्टोरेज सेक्शन में मिलेगा.
// Get a reference to the storage service, using the default Firebase App Storage* storage = Storage::GetInstance(app); // Create a Cloud Storage reference from our storage service StorageReference storage_ref = storage->GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");
ट्री में नीचे मौजूद किसी जगह का रेफ़रंस बनाया जा सकता है. जैसे, 'images/space.jpg'. इसके लिए, किसी मौजूदा रेफ़रंस पर child तरीके का इस्तेमाल करें.
// Create a child reference // images_ref now points to "images" StorageReference images_ref = storage_ref.Child("images"); // Child references can also take paths delimited by '/' // space_ref now points to "images/space.jpg" // images_ref still points to "images" StorageReference space_ref = storage_ref.Child("images/space.jpg"); // This is equivalent to creating the full reference StorageReference space_ref = storage.GetReferenceFromUrl("gs://<your-cloud-storage-bucket>/images/space.jpg");
रेफ़रंस की मदद से नेविगेट करना
हमारी फ़ाइल की हैरारकी में ऊपर जाने के लिए, Parent और Root तरीकों का भी इस्तेमाल किया जा सकता है. Parent एक लेवल ऊपर ले जाता है, जबकि Root सबसे ऊपर ले जाता है.
// Parent allows us to move to the parent of a reference // images_ref now points to 'images' StorageReference images_ref = space_ref.Parent(); // Root allows us to move all the way back to the top of our bucket // root_ref now points to the root StorageReference root_ref = space_ref.Root();
Child, Parent, और Root को कई बार एक साथ जोड़ा जा सकता है, क्योंकि हर एक रेफ़रंस दिखाता है. Root का Parent एक अपवाद है, जो एक अमान्य StorageReference है.
// References can be chained together multiple times // earth_ref points to "images/earth.jpg" StorageReference earth_ref = space_ref.Parent().Child("earth.jpg"); // null_ref is null, since the parent of root is an invalid StorageReference StorageReference null_ref = space_ref.Root().Parent();
रेफ़रंस के तरीके
full_path, name, और bucket तरीकों का इस्तेमाल करके, रेफ़रंस की जांच की जा सकती है. इससे, उन फ़ाइलों को बेहतर तरीके से समझा जा सकता है जिनकी ओर वे इशारा करते हैं. इन तरीकों से, फ़ाइल का पूरा पाथ, नाम, और बकेट की जानकारी मिलती है.
// Reference's path is: "images/space.jpg" // This is analogous to a file path on disk space_ref.full_path(); // Reference's name is the last segment of the full path: "space.jpg" // This is analogous to the file name space_ref.name(); // Reference's bucket is the name of the Cloud Storage bucket where files are stored space_ref.bucket();
रेफ़रंस फ़ाइलों से जुड़ी सीमाएं
रेफ़रंस पाथ और नामों में, मान्य यूनिकोड वर्णों का कोई भी क्रम हो सकता है. हालांकि, कुछ पाबंदियां लागू होती हैं. जैसे:
- UTF-8 कोड में बदले जाने पर, reference.fullPath की कुल लंबाई 1 से 1024 बाइट के बीच होनी चाहिए.
- नई लाइन शुरू करने का चिह्न या लाइन फ़ीड वर्ण नहीं होने चाहिए.
#,[,],*या?का इस्तेमाल करने से बचें. ये Firebase Realtime Database या gsutil जैसे अन्य टूल के साथ ठीक से काम नहीं करते.
पूरी जानकारी वाला उदाहरण
Storage* storage = Storage::GetInstance(app); // Points to the root reference StorageReference storage_ref = storage->GetReferenceFromUrl("gs://<your-bucket-name>"); // Points to "images" StorageReference images_ref = storage_ref.Child("images"); // Points to "images/space.jpg" // Note that you can use variables to create child values std::string filename = "space.jpg"; StorageReference space_ref = images_ref.Child(filename); // File path is "images/space.jpg" std::string path = space_ref.full_path() // File name is "space.jpg" std::string name = space_ref.name() // Points to "images" StorageReference images_ref = space_ref.Parent();
अगले चरण
अब Cloud Storage में फ़ाइलें अपलोड करने का तरीका जानें.