आपकी फ़ाइलें, a Cloud Storage बकेट में सेव होती हैं. इस बकेट में मौजूद फ़ाइलें, क्रम के हिसाब से व्यवस्थित होती हैं. जैसे, आपकी लोकल हार्ड डिस्क पर मौजूद फ़ाइल सिस्टम या Firebase Realtime Database में मौजूद डेटा. किसी फ़ाइल का रेफ़रंस बनाने पर, आपके ऐप्लिकेशन को उसका ऐक्सेस मिल जाता है. इसके बाद, इन रेफ़रंस का इस्तेमाल करके, डेटा अपलोड या डाउनलोड किया जा सकता है. साथ ही, मेटाडेटा पाया या अपडेट किया जा सकता है या फ़ाइल मिटाई जा सकती है. रेफ़रंस, किसी खास फ़ाइल या क्रम में सबसे ऊपर मौजूद नोड की ओर इशारा कर सकता है.
अगर आपने Firebase Realtime Database का इस्तेमाल किया है, तो आपको ये पाथ बहुत जाने-पहचाने लगेंगे. हालांकि, आपकी फ़ाइल का डेटा Cloud Storage में सेव होता है. यह Realtime Database में नहीं सेव होता.
रेफ़रंस बनाना
कोई फ़ाइल अपलोड, डाउनलोड या मिटाने के लिए या उसका मेटाडेटा पाने या अपडेट करने के लिए, उसका रेफ़रंस बनाएं. रेफ़रंस को क्लाउड में मौजूद किसी फ़ाइल के पॉइंटर के तौर पर देखा जा सकता है. रेफ़रंस का साइज़ बहुत कम होता है. इसलिए, जितने चाहें उतने रेफ़रंस बनाए जा सकते हैं. इन्हें कई कार्रवाइयों के लिए भी इस्तेमाल किया जा सकता है.
रेफ़रंस, FirebaseStorage सेवा और उसके reference तरीके का इस्तेमाल करके बनाए जाते हैं.
Swift
// Get a reference to the storage service using the default Firebase App let storage = Storage.storage() // Create a storage reference from our storage service let storageRef = storage.reference()
Objective-C
// Get a reference to the storage service using the default Firebase App FIRStorage *storage = [FIRStorage storage]; // Create a storage reference from our storage service FIRStorageReference *storageRef = [storage reference];
ट्री में नीचे मौजूद किसी जगह का रेफ़रंस बनाया जा सकता है,
जैसे 'images/space.jpg'. इसके लिए, मौजूदा रेफ़रंस पर child तरीके का इस्तेमाल करें.
Swift
// Create a child reference // imagesRef now points to "images" let imagesRef = storageRef.child("images") // Child references can also take paths delimited by '/' // spaceRef now points to "images/space.jpg" // imagesRef still points to "images" var spaceRef = storageRef.child("images/space.jpg") // This is equivalent to creating the full reference let storagePath = "\(your_firebase_storage_bucket)/images/space.jpg" spaceRef = storage.reference(forURL: storagePath)
Objective-C
// Create a child reference // imagesRef now points to "images" FIRStorageReference *imagesRef = [storageRef child:@"images"]; // Child references can also take paths delimited by '/' // spaceRef now points to "images/space.jpg" // imagesRef still points to "images" FIRStorageReference *spaceRef = [storageRef child:@"images/space.jpg"]; // This is equivalent to creating the full reference spaceRef = [storage referenceForURL:@"gs://<your-firebase-storage-bucket>/images/space.jpg"];
रेफ़रंस की मदद से नेविगेट करना
हमारी फ़ाइल के क्रम में ऊपर की ओर नेविगेट करने के लिए, parent और root तरीकों का भी इस्तेमाल किया जा सकता है. parent एक लेवल ऊपर की ओर नेविगेट करता है, जबकि root सबसे ऊपर की ओर नेविगेट करता है.
Swift
// Parent allows us to move to the parent of a reference // imagesRef now points to 'images' let imagesRef = spaceRef.parent() // Root allows us to move all the way back to the top of our bucket // rootRef now points to the root let rootRef = spaceRef.root()
Objective-C
// Parent allows us to move to the parent of a reference // 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 FIRStorageReference *rootRef = [spaceRef root];
child, parent, और root को एक साथ कई बार जोड़ा जा सकता है, क्योंकि हर बार एक रेफ़रंस मिलता है. हालांकि, root का parent, nil होता है.
Swift
// References can be chained together multiple times // earthRef points to "images/earth.jpg" let earthRef = spaceRef.parent()?.child("earth.jpg") // nilRef is nil, since the parent of root is nil let nilRef = spaceRef.root().parent()
Objective-C
// References can be chained together multiple times // earthRef points to "images/earth.jpg" FIRStorageReference *earthRef = [[spaceRef parent] child:@"earth.jpg"]; // nilRef is nil, since the parent of root is nil FIRStorageReference *nilRef = [[spaceRef root] parent];
रेफ़रंस की प्रॉपर्टी
fullPath, name, और bucket प्रॉपर्टी का इस्तेमाल करके, रेफ़रंस की जांच की जा सकती है. इससे उन फ़ाइलों के बारे में बेहतर तरीके से समझा जा सकता है जिनकी ओर ये रेफ़रंस इशारा करते हैं. इन प्रॉपर्टी से, फ़ाइल का पूरा पाथ, नाम, और बकेट मिलती है.
Swift
// 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 where files are stored spaceRef.bucket
Objective-C
// 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 where files are stored spaceRef.bucket;
रेफ़रंस पर लगी पाबंदियां
रेफ़रंस के पाथ और नामों में, मान्य यूनिकोड वर्णों का कोई भी क्रम हो सकता है. हालांकि, इन पर कुछ पाबंदियां लागू होती हैं. जैसे:
- UTF-8 एनकोड किए जाने पर, reference.fullPath की कुल लंबाई 1 से 1024 बाइट के बीच होनी चाहिए.
- इसमें कैरिज रिटर्न या लाइन फ़ीड वर्ण नहीं होने चाहिए.
#,[,],*, या?का इस्तेमाल न करें, क्योंकि ये अन्य टूल जैसे कि Firebase Realtime Database या gsutil के साथ ठीक से काम नहीं करते.
पूरी जानकारी वाला उदाहरण
Swift
// Points to the root reference let storageRef = Storage.storage().reference() // Points to "images" let imagesRef = storageRef.child("images") // Points to "images/space.jpg" // Note that you can use variables to create child values let fileName = "space.jpg" let spaceRef = imagesRef.child(fileName) // File path is "images/space.jpg" let path = spaceRef.fullPath // File name is "space.jpg" let name = spaceRef.name // Points to "images" let images = spaceRef.parent()
Objective-C
// Points to the root reference FIRStorageReference *storageRef = [[FIRStorage storage] reference]; // Points to "images" FIRStorageReference *imagesRef = [storageRef child:@"images"]; // Points to "images/space.jpg" // Note that you can use variables to create child values NSString *fileName = @"space.jpg"; FIRStorageReference *spaceRef = [imagesRef child:fileName]; // File path is "images/space.jpg" NSString *path = spaceRef.fullPath; // File name is "space.jpg" NSString *name = spaceRef.name; // Points to "images" imagesRef = [spaceRef parent];
इसके बाद, जानते हैं कि फ़ाइलें कैसे अपलोड करें Cloud Storage.