השתמש במטא נתונים של קבצים עם Cloud Storage ב-Android

לאחר העלאת קובץ ל-Cloud Storage הפניה, תוכל גם לקבל ולעדכן את המטא נתונים של הקובץ, למשל כדי להציג או לעדכן את סוג התוכן. קבצים יכולים גם לאחסן צמדי מפתח/ערך מותאמים אישית עם מטא נתונים נוספים של קבצים.

קבל מטא נתונים של קבצים

מטא נתונים של קובץ מכילים מאפיינים נפוצים כגון 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 Realtime Database ) כדי לאחסן ולסנכרן סוג זה של נתונים.

מאפייני מטא נתונים של קובץ

רשימה מלאה של מאפייני מטא נתונים בקובץ זמינה להלן:

נכס גטר סוּג סטר קיים
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> לא

העלאה, הורדה ועדכון של קבצים חשובים, אבל גם היכולת להסיר אותם. בואו ללמוד כיצד למחוק קבצים מ-Cloud Storage.