ใช้ข้อมูลเมตาของไฟล์กับ 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 กัน