आपकी फ़ाइलें क्लाउड स्टोरेज बाल्टी में संग्रहीत की जाती हैं। इस बाल्टी में फाइलें एक पदानुक्रमित संरचना में प्रस्तुत की जाती हैं, जैसे आपकी स्थानीय हार्ड डिस्क पर फ़ाइल सिस्टम, या फायरबेस रियलटाइम डेटाबेस में डेटा। किसी फ़ाइल का संदर्भ बनाकर, आपका ऐप उस तक पहुंच प्राप्त करता है। इन संदर्भों का उपयोग डेटा अपलोड या डाउनलोड करने, मेटाडेटा प्राप्त करने या अपडेट करने या फ़ाइल को हटाने के लिए किया जा सकता है। एक संदर्भ या तो एक विशिष्ट फ़ाइल या पदानुक्रम में उच्च स्तर के नोड को इंगित कर सकता है।
यदि आपने Firebase Realtime Database का उपयोग किया है, तो ये रास्ते आपको बहुत परिचित लगने चाहिए। हालाँकि, आपका फ़ाइल डेटा क्लाउड स्टोरेज में संग्रहीत है, रियलटाइम डेटाबेस में नहीं ।
एक संदर्भ बनाएँ
फ़ाइल अपलोड करने, डाउनलोड करने या हटाने या उसका मेटाडेटा प्राप्त करने या अद्यतन करने के लिए एक संदर्भ बनाएँ। एक संदर्भ को क्लाउड में एक फ़ाइल के लिए एक संकेतक के रूप में सोचा जा सकता है। संदर्भ हल्के होते हैं, इसलिए आप जितनी ज़रूरत हो उतने बना सकते हैं। वे कई कार्यों के लिए भी पुन: प्रयोज्य हैं।
FirebaseStorage
सिंगलटन इंस्टेंस का उपयोग करके एक संदर्भ बनाएं और इसके getReference()
विधि को कॉल करें।
जावा
// Create a storage reference from our app StorageReference storageRef = storage.getReference();
कोटलिन + केटीएक्स
// Create a storage reference from our app var storageRef = storage.reference
इसके बाद, आप पेड़ में निचले स्थान पर एक संदर्भ बना सकते हैं, मौजूदा संदर्भ पर child()
पद्धति का उपयोग करके "images/space.jpg"
कह सकते हैं।
जावा
// Create a child reference // imagesRef now points to "images" StorageReference imagesRef = storageRef.child("images"); // Child references can also take paths // spaceRef now points to "images/space.jpg // imagesRef still points to "images" StorageReference spaceRef = storageRef.child("images/space.jpg");
कोटलिन + केटीएक्स
// Create a child reference // imagesRef now points to "images" var imagesRef: StorageReference? = storageRef.child("images") // Child references can also take paths // spaceRef now points to "images/space.jpg // imagesRef still points to "images" var spaceRef = storageRef.child("images/space.jpg")
संदर्भों के साथ नेविगेट करें
आप हमारी फ़ाइल पदानुक्रम में नेविगेट करने के लिए getParent()
और getRoot()
विधियों का उपयोग कर सकते हैं। getParent()
एक स्तर तक नेविगेट करता है, जबकि getRoot()
शीर्ष पर सभी तरह से नेविगेट करता है।
जावा
// getParent allows us to move our reference to a parent node // imagesRef now points to 'images' imagesRef = spaceRef.getParent(); // getRoot allows us to move all the way back to the top of our bucket // rootRef now points to the root StorageReference rootRef = spaceRef.getRoot();
कोटलिन + केटीएक्स
// parent allows us to move our reference to a parent node // imagesRef now points to 'images' imagesRef = spaceRef.parent // root allows us to move all the way back to the top of our bucket // rootRef now points to the root val rootRef = spaceRef.root
child()
, getParent()
, और getRoot()
को एक साथ कई बार जंजीर बनाया जा सकता है, क्योंकि प्रत्येक एक संदर्भ देता है। लेकिन getRoot().getParent()
रिटर्न को null
getRoot().getParent()
।
जावा
// References can be chained together multiple times // earthRef points to 'images/earth.jpg' StorageReference earthRef = spaceRef.getParent().child("earth.jpg"); // nullRef is null, since the parent of root is null StorageReference nullRef = spaceRef.getRoot().getParent();
कोटलिन + केटीएक्स
// References can be chained together multiple times // earthRef points to 'images/earth.jpg' val earthRef = spaceRef.parent?.child("earth.jpg") // nullRef is null, since the parent of root is null val nullRef = spaceRef.root.parent
संदर्भ गुण
आप getPath()
, getName()
, और getBucket()
विधियों का उपयोग करने के लिए उनके द्वारा getPath()
गई फ़ाइलों को बेहतर ढंग से समझने के लिए संदर्भों का निरीक्षण कर सकते हैं। इन तरीकों से फाइल का पूरा रास्ता, नाम और बाल्टी मिलता है।
जावा
// Reference's path is: "images/space.jpg" // This is analogous to a file path on disk spaceRef.getPath(); // Reference's name is the last segment of the full path: "space.jpg" // This is analogous to the file name spaceRef.getName(); // Reference's bucket is the name of the storage bucket that the files are stored in spaceRef.getBucket();
कोटलिन + केटीएक्स
// Reference's path is: "images/space.jpg" // This is analogous to a file path on disk spaceRef.path // 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 कूटबद्ध किया जाता है, तो रेफरेंस की कुल लंबाई .fullपाथ 1 और 1024 बाइट्स के बीच होनी चाहिए।
- कोई कैरिज रिटर्न या लाइन फीड वर्ण नहीं।
-
#
,[
,]
,*
, या का उपयोग करने से बचें?
, क्योंकि ये अन्य उपकरणों जैसे Firebase Realtime Database या gsutil के साथ अच्छी तरह से काम नहीं करते हैं।
पूर्ण उदाहरण
जावा
// Points to the root reference storageRef = storage.getReference(); // Points to "images" imagesRef = storageRef.child("images"); // Points to "images/space.jpg" // Note that you can use variables to create child values String fileName = "space.jpg"; spaceRef = imagesRef.child(fileName); // File path is "images/space.jpg" String path = spaceRef.getPath(); // File name is "space.jpg" String name = spaceRef.getName(); // Points to "images" imagesRef = spaceRef.getParent();
कोटलिन + केटीएक्स
0a39395980अगला, क्लाउड स्टोरेज में फाइल अपलोड करने का तरीका जानें।