C++ के लिए Cloud Storage के साथ फ़ाइल मेटाडेटा का इस्तेमाल करना

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

फ़ाइल मेटाडेटा पाएं

फ़ाइल मेटाडेटा में content_disposition और time_created जैसी कुछ सामान्य प्रॉपर्टी के अलावा, name, size, और content_type (जिन्हें अक्सर MIME टाइप कहा जाता है) जैसी सामान्य प्रॉपर्टी भी शामिल होती हैं. इस मेटाडेटा को GetMetadata तरीके का इस्तेमाल करके, Cloud Storage के रेफ़रंस से वापस लाया जा सकता है.

// 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::map के तौर पर तय करने का विकल्प होता है. इसमें std::string प्रॉपर्टी शामिल होती हैं.

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

हर फ़ाइल के लिए, ऐप्लिकेशन से जुड़ा खास डेटा कस्टम मेटाडेटा में सेव किया जा सकता है. हालांकि, हमारा सुझाव है कि इस तरह के डेटा को सेव और सिंक करने के लिए, आप किसी डेटाबेस (जैसे कि Firebase रीयल टाइम डेटाबेस) का इस्तेमाल करें.

फ़ाइल मेटाडेटा की प्रॉपर्टी

फ़ाइल में मौजूद मेटाडेटा प्रॉपर्टी की पूरी सूची यहां दी गई है:

प्रॉपर्टी टाइप लिखा जा सकता है
bucket कॉन्स्ट वर्ण* NO
generation कॉन्स्ट वर्ण* NO
metageneration कॉन्स्ट वर्ण* NO
full_path कॉन्स्ट वर्ण* NO
name कॉन्स्ट वर्ण* NO
size int64_t NO
time_created int64_t NO
updated int64_t NO
cache_control कॉन्स्ट वर्ण* YES
content_disposition कॉन्स्ट वर्ण* YES
content_encoding कॉन्स्ट वर्ण* YES
content_language कॉन्स्ट वर्ण* YES
content_type कॉन्स्ट वर्ण* YES
download_urls std::वेक्टर<std::स्ट्रिंग> NO
custom_metadata std::map<std::स्ट्रिंग, std::string> YES

अगले चरण

फ़ाइलों को अपलोड, डाउनलोड, और अपडेट करना ज़रूरी है, लेकिन इन्हें हटाने में मदद पाना भी ज़रूरी है. आइए, Cloud Storage से फ़ाइलें मिटाने का तरीका जानें.