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

אחרי שמעלים קובץ ל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!
            }
        });

טיפול בשגיאות

יכולות להיות כמה סיבות לשגיאות שמתרחשות כשמקבלים או מעדכנים מטא-נתונים, כולל המקרים שבהם הקובץ לא קיים או שאין למשתמש הרשאה לגשת לקובץ הרצוי. מידע נוסף על שגיאות זמין בקטע Handle Errors במסמכים.

מטא-נתונים בהתאמה אישית

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

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

בהמשך מופיעה רשימה מלאה של מאפייני המטא-נתונים בקובץ:

Property Getter סוג הפונקציה להגדרת ערך קיימת
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.