Cloud Storage में कोई फ़ाइल अपलोड करने के बाद, फ़ाइल का मेटाडेटा भी पाया और अपडेट किया जा सकता है. उदाहरण के लिए, कॉन्टेंट टाइप देखने या अपडेट करने के लिए. फ़ाइलें, फ़ाइल के अतिरिक्त मेटाडेटा के साथ कस्टम कुंजी/वैल्यू पेयर भी सेव कर सकती हैं.
फ़ाइल का मेटाडेटा पाना
फ़ाइल के मेटाडेटा में, name
, size
, और contentType
(इसे अक्सर MIME टाइप कहा जाता है) जैसी सामान्य प्रॉपर्टी के साथ-साथ, contentDisposition
और timeCreated
जैसी कुछ कम सामान्य प्रॉपर्टी भी शामिल होती हैं. इस मेटाडेटा को Cloud Storage रेफ़रंस से getMetadata()
तरीके का इस्तेमाल करके वापस पाया जा सकता है.
Kotlin
// Create a storage reference from our app val storageRef = storage.reference // Get reference to the file val forestRef = storageRef.child("images/forest.jpg")
forestRef.metadata.addOnSuccessListener { metadata -> // Metadata now contains the metadata for 'images/forest.jpg' }.addOnFailureListener { // Uh-oh, an error occurred! }
Java
// Create a storage reference from our app StorageReference storageRef = storage.getReference(); // Get reference to the file StorageReference forestRef = storageRef.child("images/forest.jpg");
forestRef.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() { @Override public void onSuccess(StorageMetadata storageMetadata) { // Metadata now contains the metadata for 'images/forest.jpg' } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Uh-oh, an error occurred! } });
फ़ाइल का मेटाडेटा अपडेट करना
फ़ाइल अपलोड होने के बाद, updateMetadata()
तरीके का इस्तेमाल करके, फ़ाइल के मेटाडेटा को कभी भी अपडेट किया जा सकता है. कौनसी प्रॉपर्टी अपडेट की जा सकती हैं, इस बारे में ज़्यादा जानने के लिए पूरी सूची देखें. सिर्फ़ मेटाडेटा में दी गई प्रॉपर्टी अपडेट की जाती हैं. बाकी सभी प्रॉपर्टी में कोई बदलाव नहीं किया जाता.
Kotlin
// Create a storage reference from our app val storageRef = storage.reference // Get reference to the file val forestRef = storageRef.child("images/forest.jpg")
// Create file metadata including the content type val metadata = storageMetadata { contentType = "image/jpg" setCustomMetadata("myCustomProperty", "myValue") } // Update metadata properties forestRef.updateMetadata(metadata).addOnSuccessListener { updatedMetadata -> // Updated metadata is in updatedMetadata }.addOnFailureListener { // Uh-oh, an error occurred! }
Java
// Create a storage reference from our app StorageReference storageRef = storage.getReference(); // Get reference to the file StorageReference forestRef = storageRef.child("images/forest.jpg");
// Create file metadata including the content type StorageMetadata metadata = new StorageMetadata.Builder() .setContentType("image/jpg") .setCustomMetadata("myCustomProperty", "myValue") .build(); // Update metadata properties forestRef.updateMetadata(metadata) .addOnSuccessListener(new OnSuccessListener<StorageMetadata>() { @Override public void onSuccess(StorageMetadata storageMetadata) { // Updated metadata is in storageMetadata } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Uh-oh, an error occurred! } });
लिखे जा सकने वाले मेटाडेटा की प्रॉपर्टी को मिटाने के लिए, null
पास करें:
Kotlin
// Create file metadata with property to delete val metadata = storageMetadata { contentType = null } // Delete the metadata property forestRef.updateMetadata(metadata).addOnSuccessListener { updatedMetadata -> // updatedMetadata.contentType should be null }.addOnFailureListener { // Uh-oh, an error occurred! }
Java
// Create file metadata with property to delete StorageMetadata metadata = new StorageMetadata.Builder() .setContentType(null) .build(); // Delete the metadata property forestRef.updateMetadata(metadata) .addOnSuccessListener(new OnSuccessListener<StorageMetadata>() { @Override public void onSuccess(StorageMetadata storageMetadata) { // metadata.contentType should be null } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Uh-oh, an error occurred! } });
गड़बड़ियां ठीक करना
मेटाडेटा पाने या उसे अपडेट करने के दौरान गड़बड़ियां होने की कई वजहें हो सकती हैं. जैसे, फ़ाइल का मौजूद न होना या उपयोगकर्ता के पास, फ़ाइल को ऐक्सेस करने की अनुमति न होना. गड़बड़ियों के बारे में ज़्यादा जानकारी, दस्तावेज़ के गड़बड़ियां ठीक करना सेक्शन में मिल सकती है.
कस्टम मेटाडेटा
setCustomMetadata()
क्लास में setCustomMetadata()
तरीके का इस्तेमाल करके, कस्टम मेटाडेटा तय किया जा सकता है.StorageMetadata.Builder
Kotlin
val metadata = storageMetadata { setCustomMetadata("location", "Yosemite, CA, USA") setCustomMetadata("activity", "Hiking") }
Java
StorageMetadata metadata = new StorageMetadata.Builder() .setCustomMetadata("location", "Yosemite, CA, USA") .setCustomMetadata("activity", "Hiking") .build();
कस्टम मेटाडेटा में, हर फ़ाइल के लिए ऐप्लिकेशन से जुड़ा डेटा सेव किया जा सकता है. हालांकि, हम इस तरह के डेटा को सेव करने और सिंक करने के लिए, डेटाबेस (जैसे कि Firebase Realtime Database) का इस्तेमाल करने का सुझाव देते हैं.
फ़ाइल के मेटाडेटा की प्रॉपर्टी
किसी फ़ाइल पर मौजूद मेटाडेटा प्रॉपर्टी की पूरी सूची यहां दी गई है:
प्रॉपर्टी गैटर | टाइप | Setter Exists |
---|---|---|
getBucket |
String |
नहीं |
getGeneration |
String |
नहीं |
getMetadataGeneration |
String |
नहीं |
getPath |
String |
नहीं |
getName |
String |
नहीं |
getSizeBytes |
long |
नहीं |
getCreationTimeMillis |
long |
नहीं |
getUpdatedTimeMillis |
long |
नहीं |
getMd5Hash |
String |
नहीं |
getCacheControl |
String |
YES |
getContentDisposition |
String |
YES |
getContentEncoding |
String |
YES |
getContentLanguage |
String |
YES |
getContentType |
String |
YES |
getCustomMetadata |
String |
YES |
getCustomMetadataKeys |
Set<String> |
नहीं |
फ़ाइलों को अपलोड, डाउनलोड, और अपडेट करना ज़रूरी है. हालांकि, उन्हें हटाने का विकल्प भी होना चाहिए. आइए, Cloud Storage से फ़ाइलें मिटाने का तरीका जानें.