आपकी फ़ाइलें Cloud Storage for Firebase बकेट में सेव की जाती हैं. इस बकेट में मौजूद फ़ाइलें, हैरारकी के हिसाब से दिखती हैं. ठीक वैसे ही जैसे आपकी लोकल हार्ड डिस्क पर फ़ाइल सिस्टम दिखता है.
किसी फ़ाइल का रेफ़रंस बनाकर, आपका ऐप्लिकेशन उसे ऐक्सेस कर सकता है. रेफ़रंस, किसी खास फ़ाइल या हैरारकी में मौजूद किसी हाई-लेवल नोड पर ले जा सकता है. रेफ़रंस को क्लाउड में मौजूद फ़ाइल या नोड के पॉइंटर के तौर पर समझा जा सकता है. रेफ़रंस बनाने के बाद, उसका इस्तेमाल करके कई काम किए जा सकते हैं. जैसे, डेटा अपलोड या डाउनलोड करना, मेटाडेटा पाना या अपडेट करना या फ़ाइल मिटाना.
इस पेज पर, रेफ़रंस बनाने और उनके साथ काम करने के लिए, Cloud Storage for Firebase SDK टूल इस्तेमाल करने का तरीका बताया गया है.
कोई रेफ़रंस फ़ाइल बनाना
किसी फ़ाइल पर कार्रवाइयां करने के लिए, आपको पहले उस फ़ाइल का रेफ़रंस बनाना होगा. रेफ़रंस काफ़ी कम स्टोरेज लेते हैं. इसलिए, ज़रूरत के हिसाब से जितने चाहें उतने रेफ़रंस बनाए जा सकते हैं. इन्हें कई ऑपरेशनों के लिए फिर से इस्तेमाल किया जा सकता है.
FirebaseStorage सिंगलटन इंस्टेंस का इस्तेमाल करके और उसके ref() तरीके को कॉल करके, एक रेफ़रंस बनाएं.
final storageRef = FirebaseStorage.instance.ref();
इसके बाद, ट्री में नीचे मौजूद किसी जगह का रेफ़रंस बनाया जा सकता है. जैसे, किसी मौजूदा रेफ़रंस पर child() तरीके का इस्तेमाल करके, "images/space.jpg".
// Create a child reference
// imagesRef now points to "images"
final imagesRef = storageRef.child("images");
// Child references can also take paths
// spaceRef now points to "images/space.jpg
// imagesRef still points to "images"
final spaceRef = storageRef.child("images/space.jpg");
रेफ़रंस की मदद से नेविगेट करना
अपनी फ़ाइल की हैरारकी में ऊपर जाने के लिए, parent और root प्रॉपर्टी का भी इस्तेमाल किया जा सकता है. parent से एक लेवल ऊपर जाया जा सकता है,
जबकि root से सबसे ऊपर जाया जा सकता है.
// parent lets you move your reference to a parent node
// imagesRef2 now points to 'images'
final imagesRef2 = spaceRef.parent;
// root lets you move all the way back to the top of our bucket
// rootRef now points to the root
final rootRef = spaceRef.root;
child(), parent, और root को कई बार एक साथ जोड़ा जा सकता है, क्योंकि हर एक रेफ़रंस है. हालांकि, root.parent को ऐक्सेस करने पर null.
// References can be chained together multiple times
// earthRef points to 'images/earth.jpg'
final earthRef = spaceRef.parent?.child("earth.jpg");
// nullRef is null, since the parent of root is null
final nullRef = spaceRef.root.parent;
रेफ़रंस प्रॉपर्टी
fullPath, name, और bucket प्रॉपर्टी का इस्तेमाल करके, रेफ़रंस की जांच की जा सकती है. इससे, उन फ़ाइलों को बेहतर तरीके से समझा जा सकता है जिनके बारे में वे बताते हैं. इन प्रॉपर्टी से, फ़ाइल का पूरा पाथ, नाम, और बकेट की जानकारी मिलती है.
// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
spaceRef.fullPath;
// Reference's name is the last segment of the full path: "space.jpg"
// This is analogous to the file name
spaceRef.name;
// Reference's bucket is the name of the storage bucket that the files are stored in
spaceRef.bucket;
रेफ़रंस फ़ाइलों से जुड़ी सीमाएं
रेफ़रंस पाथ और नामों में मान्य यूनिकोड वर्णों का कोई भी क्रम हो सकता है. हालांकि, कुछ पाबंदियां लागू होती हैं. जैसे:
- UTF-8 कोड में बदले जाने पर, reference.fullPath की कुल लंबाई 1 से 1024 बाइट के बीच होनी चाहिए.
- नई लाइन शुरू करने का चिह्न या लाइन फ़ीड वर्ण नहीं होने चाहिए.
#,[,],*या?का इस्तेमाल करने से बचें, क्योंकि ये Firebase Realtime Database या gsutil जैसे अन्य टूल के साथ ठीक से काम नहीं करते.
पूरा उदाहरण
// Points to the root reference
final storageRef = FirebaseStorage.instance.ref();
// Points to "images"
Reference? imagesRef = storageRef.child("images");
// Points to "images/space.jpg"
// Note that you can use variables to create child values
final fileName = "space.jpg";
final spaceRef = imagesRef.child(fileName);
// File path is "images/space.jpg"
final path = spaceRef.fullPath;
// File name is "space.jpg"
final name = spaceRef.name;
// Points to "images"
imagesRef = spaceRef.parent;
इसके बाद, Cloud Storage में फ़ाइलें अपलोड करने का तरीका जानें.