Unity के लिए Cloud Storage के साथ Cloud Storage का रेफ़रंस बनाएं

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

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

पहचान फ़ाइल बनाना

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

Firebase.Storage.FirebaseStorage सेवा से रेफ़रंस बनाए जाते हैं. इसके लिए, अपने Firebase ऐप्लिकेशन पर GetReferenceFromUrl() तरीके को कॉल करें और gs://<your-cloud-storage-bucket> फ़ॉर्मैट का यूआरएल पास करें. आपको यह यूआरएल, Firebase कंसोल के स्टोरेज सेक्शन में मिलेगा.

// Get a reference to the storage service, using the default Firebase App
FirebaseStorage storage = FirebaseStorage.DefaultInstance;

// Create a storage reference from our storage service
StorageReference storageRef =
    storage.GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");

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

// Create a child reference
// imagesRef now points to "images"
StorageReference imagesRef = storageRef.Child("images");

// Child references can also take paths delimited by '/' such as:
// "images/space.jpg".
StorageReference spaceRef = imagesRef.Child("space.jpg");
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"

// This is equivalent to creating the full referenced
StorageReference spaceRefFull = storage.GetReferenceFromUrl(
    "gs://<your-cloud-storage-bucket>/images/space.jpg");

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

// Parent allows us to move to the parent of a reference
// imagesRef now points to 'images'
StorageReference imagesRef = spaceRef.Parent;

// Root allows us to move all the way back to the top of our bucket
// rootRef now points to the root
StorageReference rootRef = spaceRef.Root;

Child, Parent, और Root को एक साथ कई बार इस्तेमाल किया जा सकता है, क्योंकि हर फ़ंक्शन एक रेफ़रंस देता है. अपवाद, Root का Parent है, जो एक अमान्य StorageReference है.

// References can be chained together multiple times
// earthRef points to "images/earth.jpg"
StorageReference earthRef =
    spaceRef.Parent.Child("earth.jpg");

// nullRef is null since the parent of root is an invalid StorageReference
StorageReference nullRef = spaceRef.Root.Parent;

रेफ़रंस के तरीके

Path, Name, और Bucket प्रॉपर्टी का इस्तेमाल करके, उन फ़ाइलों को बेहतर तरीके से समझा जा सकता है जिनके बारे में रेफ़रंस दिया गया है. इन प्रॉपर्टी से, फ़ाइल का पूरा पाथ, नाम, और बकेट मिलता है.

// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
string path = spaceRef.Path;

// Reference's name is the last segment of the full path: "space.jpg"
// This is analogous to the file name
string name = spaceRef.Name;

// Reference's bucket is the name of the storage bucket where files are stored
string bucket = spaceRef.Bucket;

संदर्भों से जुड़ी सीमाएं

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

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

पूरा उदाहरण

FirebaseStorage storage = FirebaseStorage.DefaultInstance;

// Points to the root reference
StorageReference storageRef =
    storage.GetReferenceFromUrl("gs://<your-bucket-name>");

// Points to "images"
StorageReference imagesRef = storageRef.Child("images");

// Points to "images/space.jpg"
// Note that you can use variables to create child values
string filename = "space.jpg";
StorageReference spaceRef = imagesRef.Child(filename);

// File path is "images/space.jpg"
string path = spaceRef.Path;

// File name is "space.jpg"
string name = spaceRef.Name;

// Points to "images"
StorageReference imagesRef = spaceRef.Parent;

अगले चरण

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