C++ के लिए क्लाउड स्टोरेज के साथ फ़ाइल मेटाडेटा का उपयोग करें

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

फ़ाइल मेटाडेटा प्राप्त करें

फ़ाइल मेटाडेटा में सामान्य गुण जैसे name , size और content_type (अक्सर MIME प्रकार के रूप में संदर्भित) के अलावा कुछ कम सामान्य गुण जैसे content_disposition और time_created शामिल होते हैं। इस मेटाडेटा को GetMetadata विधि का उपयोग करके क्लाउड स्टोरेज संदर्भ से पुनर्प्राप्त किया जा सकता है।

// Create reference to the file whose metadata we want to retrieve
StorageReference forest_ref = storage_ref.Child("images/forest.jpg");

// Get metadata properties
Future future = forest_ref.GetMetadata();

// Wait for Future to complete...

if (future.Error() != firebase::storage::kErrorNone) {
  // Uh-oh, an error occurred!
} else {
  // We can now retrieve the metadata for 'images/forest.jpg'
  Metadata* metadata = future.Result();
}

फ़ाइल मेटाडेटा अद्यतन करें

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

// Create reference to the file whose metadata we want to change
firebase::storage::StorageReference forest_ref = storage_ref.child("images/forest.jpg");

// Create file metadata to update
Metadata new_metadata;
newMetadata.set_cache_control("public,max-age=300");
newMetadata.set_content_type("image/jpeg");

// Update metadata properties
Future future = forest_ref.UpdateMetadata(new_metadata);

// Wait for Future to complete...

if (future.Error() != firebase::storage::kErrorNone) {
  // Uh-oh, an error occurred!
} else {
  // We can now retrieve the updated metadata for 'images/forest.jpg'
  Metadata* metadata = future.Result();
}

आप खाली स्ट्रिंग पास करके लिखने योग्य मेटाडेटा गुणों को हटा सकते हैं:

// Create file metadata with property to delete
StorageMetadata new_metadata;
new_metadata.set_content_type("");

// Delete the metadata property
Future future = forest_ref.UpdateMetadata(new_metadata);

// Wait for Future to complete...

if (future.Error() != 0) {
  // Uh-oh, an error occurred!
} else {
  // metadata.content_type() should be an empty string
  Metadata* metadata = future.Result();
}

त्रुटियाँ संभालें

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

कस्टम मेटाडेटा

आप कस्टम मेटाडेटा को std::string गुणों वाले std::map के रूप में निर्दिष्ट कर सकते हैं।

std::map* custom_metadata = metadata.custom_metadata();
custom_metadata->insert(std::make_pair("location", "Yosemite, CA, USA");
custom_metadata->insert(std::make_pair("activity", "Hiking");

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

फ़ाइल मेटाडेटा गुण

किसी फ़ाइल पर मेटाडेटा गुणों की पूरी सूची नीचे उपलब्ध है:

संपत्ति प्रकार लिखने योग्य
bucket स्थिरांक चार* नहीं
generation स्थिरांक चार* नहीं
metageneration स्थिरांक चार* नहीं
full_path स्थिरांक चार* नहीं
name स्थिरांक चार* नहीं
size int64_t नहीं
time_created int64_t नहीं
updated int64_t नहीं
cache_control स्थिरांक चार* हाँ
content_disposition स्थिरांक चार* हाँ
content_encoding स्थिरांक चार* हाँ
content_language स्थिरांक चार* हाँ
content_type स्थिरांक चार* हाँ
download_urls std::vector<std::string> नहीं
custom_metadata std::map<std::string, std::string> हाँ

अगले कदम

फ़ाइलें अपलोड करना, डाउनलोड करना और अपडेट करना महत्वपूर्ण है, लेकिन साथ ही उन्हें हटाना भी महत्वपूर्ण है। आइए जानें क्लाउड स्टोरेज से फाइल्स को कैसे डिलीट करें