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

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

App Engine

फ़ाइल का मेटाडेटा पाना

फ़ाइल के मेटाडेटा में, सामान्य प्रॉपर्टी शामिल होती हैं. जैसे, name, size, और content_type. इसे अक्सर MIME टाइप कहा जाता है. इसके अलावा, इसमें कुछ कम सामान्य प्रॉपर्टी भी शामिल होती हैं. जैसे, content_disposition और time_created. ` तरीके का इस्तेमाल करके, Cloud Storage रेफ़रंस से यह मेटाडेटा वापस पाया जा सकता है.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::map के तौर पर, कस्टम मेटाडेटा तय किया जा सकता है. इसमें std::string प्रॉपर्टी शामिल होती हैं.

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

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

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

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

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

अगले चरण

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