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

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

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

फ़ाइल के मेटाडेटा में सामान्य प्रॉपर्टी मौजूद हैं. जैसे, name, size, और contentType (इन्हें अक्सर MIME टाइप कहा जाता है) और कुछ कम contentDisposition और timeCreated जैसी सामान्य क्वेरी. यह मेटाडेटा इनमें से कोई भी हो सकता है: Cloud Storage के रेफ़रंस से लिया गया है, तो 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!
            }
        });

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

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

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

कस्टम मेटाडेटा डालने के लिए, यहां दिए गए setCustomMetadata() तरीके का इस्तेमाल करें: StorageMetadata.Builder क्लास.

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 नहीं
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 से.