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

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

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

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

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

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

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

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

संपत्ति प्राप्तकर्ता प्रकार सेटर मौजूद है
getBucket String नहीं
getGeneration String नहीं
getMetadataGeneration String नहीं
getPath String नहीं
getName String नहीं
getSizeBytes long नहीं
getCreationTimeMillis long नहीं
getUpdatedTimeMillis long नहीं
getMd5Hash String नहीं
getCacheControl String हाँ
getContentDisposition String हाँ
getContentEncoding String हाँ
getContentLanguage String हाँ
getContentType String हाँ
getCustomMetadata String हाँ
getCustomMetadataKeys Set<String> नहीं

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