ใช้ข้อมูลเมตาของไฟล์กับ Cloud Storage สำหรับ C++

หลังจากอัปโหลดไฟล์ไปยังข้อมูลอ้างอิงของ Cloud Storage แล้ว คุณยังสามารถรับ และอัปเดตข้อมูลเมตาของไฟล์ได้ด้วย เช่น เพื่ออัปเดตประเภทเนื้อหา นอกจากนี้ ไฟล์ยังจัดเก็บคู่คีย์/ค่าที่กำหนดเองพร้อมข้อมูลเมตาของไฟล์เพิ่มเติมได้ด้วย

รับข้อมูลเมตาของไฟล์

ข้อมูลเมตาของไฟล์มีพร็อพเพอร์ตี้ทั่วไป เช่น name, size และ content_type (มักเรียกว่าประเภท MIME) นอกเหนือจากพร็อพเพอร์ตี้ที่ไม่ค่อยได้ใช้ เช่น content_disposition และ time_created คุณสามารถดึงข้อมูลเมตานี้จากข้อมูลอ้างอิง Cloud Storage ได้โดยใช้วิธี GetMetadata

// Create reference to the file whose metadata we want to retrieve
StorageReference forest_ref = storage_ref.Child("images/forest.jpg");

// Get metadata properties
Future future = forest_ref.GetMetadata();

// Wait for Future to complete...

if (future.Error() != firebase::storage::kErrorNone) {
  // Uh-oh, an error occurred!
} else {
  // We can now retrieve the metadata for 'images/forest.jpg'
  Metadata* metadata = future.Result();
}

อัปเดตข้อมูลเมตาของไฟล์

คุณสามารถอัปเดตข้อมูลเมตาของไฟล์ได้ทุกเมื่อหลังจากอัปโหลดไฟล์เสร็จสมบูรณ์โดยใช้วิธี UpdateMetadata ดูข้อมูลเพิ่มเติมเกี่ยวกับพร็อพเพอร์ตี้ที่ อัปเดตได้ใน รายการทั้งหมด ระบบจะอัปเดตเฉพาะพร็อพเพอร์ตี้ที่ระบุในข้อมูลเมตา ส่วนพร็อพเพอร์ตี้อื่นๆ จะไม่เปลี่ยนแปลง

// Create reference to the file whose metadata we want to change
firebase::storage::StorageReference forest_ref = storage_ref.child("images/forest.jpg");

// Create file metadata to update
Metadata new_metadata;
newMetadata.set_cache_control("public,max-age=300");
newMetadata.set_content_type("image/jpeg");

// Update metadata properties
Future future = forest_ref.UpdateMetadata(new_metadata);

// Wait for Future to complete...

if (future.Error() != firebase::storage::kErrorNone) {
  // Uh-oh, an error occurred!
} else {
  // We can now retrieve the updated metadata for 'images/forest.jpg'
  Metadata* metadata = future.Result();
}

คุณสามารถลบพร็อพเพอร์ตี้ข้อมูลเมตาที่เขียนได้โดยส่งสตริงว่าง ดังนี้

// Create file metadata with property to delete
StorageMetadata new_metadata;
new_metadata.set_content_type("");

// Delete the metadata property
Future future = forest_ref.UpdateMetadata(new_metadata);

// Wait for Future to complete...

if (future.Error() != 0) {
  // Uh-oh, an error occurred!
} else {
  // metadata.content_type() should be an empty string
  Metadata* metadata = future.Result();
}

จัดการข้อผิดพลาด

ข้อผิดพลาดอาจเกิดขึ้นเมื่อรับหรืออัปเดตข้อมูลเมตาด้วยเหตุผลหลายประการ เช่น ไฟล์ไม่มีอยู่ หรือผู้ใช้ไม่มีสิทธิ์เข้าถึงไฟล์ที่ต้องการ ดูข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาดได้ใน ส่วนจัดการข้อผิดพลาด ของเอกสาร

ข้อมูลเมตาที่กำหนดเอง

คุณสามารถระบุข้อมูลเมตาที่กำหนดเองเป็น std::map ที่มีพร็อพเพอร์ตี้ std::string

std::map<std::string, std::string>* custom_metadata = metadata.custom_metadata();
custom_metadata->insert(std::make_pair("location", "Yosemite, CA, USA");
custom_metadata->insert(std::make_pair("activity", "Hiking");

คุณสามารถจัดเก็บข้อมูลเฉพาะของแอปสำหรับแต่ละไฟล์ในข้อมูลเมตาที่กำหนดเองได้ แต่เราขอแนะนำอย่างยิ่งให้ใช้ฐานข้อมูล (เช่น Firebase Realtime Database) เพื่อจัดเก็บและซิงค์ข้อมูลประเภทนี้

พร็อพเพอร์ตี้ข้อมูลเมตาของไฟล์

ดูรายการพร็อพเพอร์ตี้ข้อมูลเมตาทั้งหมดในไฟล์ได้ที่ด้านล่าง

พร็อพเพอร์ตี้ ประเภท เขียนได้
bucket const char* ไม่
generation const char* ไม่
metageneration const char* ไม่
full_path const char* ไม่
name const char* ไม่
size int64_t ไม่
time_created int64_t ไม่
updated int64_t ไม่
cache_control const char* ใช่
content_disposition const char* ใช่
content_encoding const char* ใช่
content_language const char* ใช่
content_type const char* ใช่
download_urls std::vector<std::string> ไม่
custom_metadata std::map<std::string, std::string> ใช่

ขั้นตอนถัดไป

การอัปโหลด ดาวน์โหลด และอัปเดตไฟล์เป็นสิ่งสำคัญ เช่นเดียวกับการลบไฟล์ มาดูวิธี ลบไฟล์ จาก Cloud Storage กัน