Android पर Cloud Storage रेफ़रंस बनाना

आपकी फ़ाइलें Cloud Storage बकेट में सेव होती हैं. इस बकेट में मौजूद फ़ाइलें, हैरारकी में दिखती हैं. ठीक उसी तरह, जैसे आपकी लोकल हार्ड डिस्क पर मौजूद फ़ाइल सिस्टम या Firebase रीयल टाइम डेटाबेस में मौजूद डेटा. किसी फ़ाइल का रेफ़रंस बनाने पर, आपका ऐप्लिकेशन उस फ़ाइल को ऐक्सेस कर सकता है. इसके बाद, इन रेफ़रंस का इस्तेमाल डेटा अपलोड या डाउनलोड करने, मेटाडेटा पाने या अपडेट करने या फ़ाइल को मिटाने के लिए किया जा सकता है. रेफ़रंस किसी खास फ़ाइल या हैरारकी में बड़े लेवल के नोड की ओर इशारा कर सकता है.

अगर आपने Firebase रीयल टाइम डेटाबेस का इस्तेमाल किया है, तो ये पाथ आपको बहुत अच्छे लगते हैं. हालांकि, आपकी फ़ाइल का डेटा Cloud Storage में सेव किया जाता है, कि रीयल टाइम डेटाबेस में.

कोई पहचान फ़ाइल बनाना

फ़ाइल अपलोड करने, डाउनलोड करने या मिटाने के लिए, पहचान फ़ाइल बनाएं. इसके अलावा, उसका मेटाडेटा पाने या अपडेट करने के लिए, पहचान फ़ाइल बनाएं. किसी रेफ़रंस को क्लाउड में मौजूद फ़ाइल के लिए पॉइंटर के तौर पर इस्तेमाल किया जा सकता है. पहचान फ़ाइलें कम वज़न की होती हैं, इसलिए जितनी ज़रूरत हो उतनी बनाई जा सकती हैं. इन्हें कई कार्रवाइयों के लिए फिर से इस्तेमाल किया जा सकता है.

FirebaseStorage सिंगलटन इंस्टेंस का इस्तेमाल करके और इसके getReference() तरीके को कॉल करके, एक पहचान फ़ाइल बनाएं.

Kotlin+KTX

// Create a storage reference from our app
var storageRef = storage.reference

Java

// Create a storage reference from our app
StorageReference storageRef = storage.getReference();

इसके बाद, आप ट्री में नीचे की जगह का रेफ़रंस बना सकते हैं, जैसे कि किसी मौजूदा रेफ़रंस पर child() तरीके का इस्तेमाल करके "images/space.jpg".

Kotlin+KTX

// 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")

Java

// 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");

फ़ाइल की हैरारकी में ऊपर जाने के लिए, getParent() और getRoot() तरीकों का भी इस्तेमाल किया जा सकता है. getParent() एक लेवल ऊपर जाता है, जबकि getRoot() सबसे ऊपर जाता है.

Kotlin+KTX

// 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

Java

// 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();

child(), getParent(), और getRoot() को कई बार एक साथ जोड़ा जा सकता है, क्योंकि हर एक रेफ़रंस देता है. हालांकि, getRoot().getParent() को कॉल करने पर null के नतीजे मिलेंगे.

Kotlin+KTX

// 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

Java

// 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();

रेफ़रंस प्रॉपर्टी

रेफ़रंस getPath(), getName(), और getBucket() तरीकों का इस्तेमाल करके, उन फ़ाइलों को बेहतर तरीके से समझने के लिए उनकी जांच कर सकते हैं जिनकी ओर वे इशारा करते हैं. इन तरीकों से, फ़ाइल का पूरा पाथ, नाम, और बकेट की जानकारी मिलती है.

Kotlin+KTX

// 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

Java

// 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();

पहचान फ़ाइलों की सीमाएं

रेफ़रंस पाथ और नामों में मान्य यूनिकोड वर्णों का कोई भी क्रम हो सकता है, लेकिन कुछ पाबंदियां लागू की जाती हैं, जिनमें ये शामिल हैं:

  1. जब UTF-8 कोड में बदला जाता है, तोreference.fullPath की कुल लंबाई 1 से 1024 बाइट के बीच होनी चाहिए.
  2. कोई कैरेज रिटर्न या लाइन फ़ीड वर्ण नहीं.
  3. #, [, ], * या ? का इस्तेमाल करने से बचें, क्योंकि ये Firebase रीयल टाइम डेटाबेस या gsutil जैसे अन्य टूल के साथ ठीक से काम नहीं करते.

पूरा उदाहरण

Kotlin+KTX

// Points to the root reference
storageRef = storage.reference

// Points to "images"
imagesRef = storageRef.child("images")

// Points to "images/space.jpg"
// Note that you can use variables to create child values
val fileName = "space.jpg"
spaceRef = imagesRef.child(fileName)

// File path is "images/space.jpg"
val path = spaceRef.path

// File name is "space.jpg"
val name = spaceRef.name

// Points to "images"
imagesRef = spaceRef.parent

Java

// 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();

चलिए, अब Cloud Storage में फ़ाइलें अपलोड करने का तरीका जानते हैं.