Android पर Cloud Storage के लिए रेफ़रंस बनाना

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

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

रेफ़रंस बनाना

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

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

Kotlin

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

Java

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

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

Kotlin

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

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

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

// 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 एनकोडिंग के दौरान, रेफ़रंस.fullPath की कुल लंबाई 1 से 1024 बाइट के बीच होनी चाहिए.
  2. इसमें कैरिज रिटर्न या लाइन फ़ीड वर्ण नहीं होने चाहिए.
  3. #, [, ], *, या ? का इस्तेमाल न करें, क्योंकि ये Firebase Realtime Database या gsutil जैसे अन्य टूल के साथ ठीक से काम नहीं करते.

पूरी जानकारी वाला उदाहरण

Kotlin

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