Catch up on highlights from Firebase at Google I/O 2023. Learn more

C++ के लिए क्लाउड स्टोरेज के साथ क्लाउड स्टोरेज संदर्भ बनाएं

आपकी फ़ाइलें क्लाउड स्टोरेज बकेट में संग्रहीत हैं। इस बकेट में फ़ाइलें एक पदानुक्रमित संरचना में प्रस्तुत की जाती हैं, जैसे आपकी स्थानीय हार्ड डिस्क पर फ़ाइल सिस्टम, या फायरबेस रीयलटाइम डेटाबेस में डेटा। किसी फ़ाइल का संदर्भ बनाकर, आपका ऐप उस तक पहुंच प्राप्त करता है। फिर इन संदर्भों का उपयोग डेटा अपलोड या डाउनलोड करने, मेटाडेटा प्राप्त करने या अपडेट करने या फ़ाइल को हटाने के लिए किया जा सकता है। एक संदर्भ या तो किसी विशिष्ट फ़ाइल या पदानुक्रम में उच्च स्तर के नोड को इंगित कर सकता है।

यदि आपने Firebase रीयलटाइम डेटाबेस का उपयोग किया है, तो ये पथ आपको बहुत परिचित लगने चाहिए. हालाँकि, आपका फ़ाइल डेटा क्लाउड स्टोरेज में संग्रहीत है, रीयलटाइम डेटाबेस में नहीं

एक संदर्भ बनाएँ

किसी फ़ाइल को अपलोड करने, डाउनलोड करने या हटाने या उसका मेटाडेटा प्राप्त करने या अपडेट करने के लिए एक संदर्भ बनाएँ। एक संदर्भ को क्लाउड में फ़ाइल के सूचक के रूप में माना जा सकता है। संदर्भ हल्के होते हैं, इसलिए आप जितने चाहें उतने संदर्भ बना सकते हैं। वे कई ऑपरेशनों के लिए पुन: प्रयोज्य भी हैं।

GetReferenceFromUrl() विधि को कॉल करके और gs://<your-cloud-storage-bucket> फ़ॉर्म के URL में पास करके आपके Firebase ऐप पर storage सेवा से संदर्भ बनाए जाते हैं। आप इस यूआरएल को फायरबेस कंसोल के स्टोरेज सेक्शन में पा सकते हैं।

// Get a reference to the storage service, using the default Firebase App
Storage* storage = Storage::GetInstance(app);

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

आप किसी मौजूदा संदर्भ पर child मेथड का उपयोग करके ट्री में नीचे किसी स्थान के लिए एक संदर्भ बना सकते हैं, जैसे 'images/space.jpg'

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

// Child references can also take paths delimited by '/'
// space_ref now points to "images/space.jpg"
// images_ref still points to "images"
StorageReference space_ref = storage_ref.Child("images/space.jpg");

// This is equivalent to creating the full reference
StorageReference space_ref = 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
// images_ref now points to 'images'
StorageReference images_ref = space_ref.Parent();

// Root allows us to move all the way back to the top of our bucket
// root_ref now points to the root
StorageReference root_ref = space_ref.Root();

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

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

// null_ref is null, since the parent of root is an invalid StorageReference
StorageReference null_ref = space_ref.Root().Parent();

संदर्भ के तरीके

आप उन फ़ाइलों को बेहतर ढंग से समझने के लिए संदर्भों का निरीक्षण कर सकते हैं, जिन्हें वे full_path , name , और bucket विधियों का उपयोग करके इंगित करते हैं। इन विधियों से फ़ाइल का पूरा पथ, नाम और बकेट प्राप्त होता है।

// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
space_ref.full_path();

// Reference's name is the last segment of the full path: "space.jpg"
// This is analogous to the file name
space_ref.name();

// Reference's bucket is the name of the Cloud Storage bucket where files are stored
space_ref.bucket();

संदर्भों पर सीमाएं

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

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

पूरा उदाहरण

Storage* storage = Storage::GetInstance(app);

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

// Points to "images"
StorageReference images_ref = storage_ref.Child("images");

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

// File path is "images/space.jpg"
std::string path = space_ref.full_path()

// File name is "space.jpg"
std::string name = space_ref.name()

// Points to "images"
StorageReference images_ref = space_ref.Parent();

अगले कदम

आगे, आइए जानें कि क्लाउड स्टोरेज में फाइल कैसे अपलोड करें