Android पर Cloud Storage के साथ फ़ाइल मेटाडेटा का इस्तेमाल करना

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

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

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

Kotlin+KTX

// 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+KTX

// 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+KTX

// 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!
            }
        });

गड़बड़ियां ठीक करना

मेटाडेटा मिलने या अपडेट करने में कई वजहों से गड़बड़ियां आ सकती हैं. जैसे, फ़ाइल मौजूद न हो या उपयोगकर्ता को अपनी पसंद की फ़ाइल ऐक्सेस करने की अनुमति न हो. गड़बड़ियों के बारे में ज़्यादा जानकारी, दस्तावेज़ों के गड़बड़ियां मैनेज करना सेक्शन में मिल सकती है.

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

StorageMetadata.Builder क्लास में, setCustomMetadata() तरीके का इस्तेमाल करके कस्टम मेटाडेटा तय किया जा सकता है.

Kotlin+KTX

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 रीयल टाइम डेटाबेस) का इस्तेमाल करें.

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

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

प्रॉपर्टी गेटर टाइप सेटर मौजूद है
getBucket String NO
getGeneration String NO
getMetadataGeneration String NO
getPath String NO
getName String NO
getSizeBytes long NO
getCreationTimeMillis long NO
getUpdatedTimeMillis long NO
getMd5Hash String NO
getCacheControl String YES
getContentDisposition String YES
getContentEncoding String YES
getContentLanguage String YES
getContentType String YES
getCustomMetadata String YES
getCustomMetadataKeys Set<String> NO

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