หลังจากอัปโหลดไฟล์ไปยังข้อมูลอ้างอิงของ 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! } });
จัดการข้อผิดพลาด
ข้อผิดพลาดอาจเกิดขึ้นเมื่อรับหรืออัปเดตข้อมูลเมตาด้วยเหตุผลหลายประการ เช่น ไฟล์ไม่มีอยู่ หรือผู้ใช้ไม่มีสิทธิ์เข้าถึงไฟล์ที่ต้องการ ดูข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาดได้ใน ส่วนจัดการข้อผิดพลาด ของเอกสาร
ข้อมูลเมตาที่กำหนดเอง
คุณระบุข้อมูลเมตาที่กำหนดเองได้โดยใช้วิธี 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) เพื่อจัดเก็บและซิงค์ข้อมูลประเภทนี้
พร็อพเพอร์ตี้ข้อมูลเมตาของไฟล์
ดูรายการพร็อพเพอร์ตี้ข้อมูลเมตาทั้งหมดในไฟล์ได้ที่ด้านล่าง
| Getter พร็อพเพอร์ตี้ | ประเภท | มี Setter |
|---|---|---|
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 กัน