ใช้ข้อมูลเมตาของไฟล์กับ 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 ) เพื่อจัดเก็บและซิงโครไนซ์ข้อมูลประเภทนี้

คุณสมบัติข้อมูลเมตาของไฟล์

รายการคุณสมบัติข้อมูลเมตาทั้งหมดในไฟล์มีอยู่ด้านล่าง:

ผู้ได้รับทรัพย์สิน พิมพ์ มีเซ็ตเตอร์อยู่
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 กันดีกว่า