Sử dụng siêu dữ liệu tệp bằng Cloud Storage cho C++

Sau khi tải một tệp lên tệp tham chiếu trong Cloud Storage, bạn cũng có thể tải và cập nhật siêu dữ liệu của tệp, chẳng hạn như để cập nhật loại nội dung. Tệp cũng có thể lưu trữ các cặp khoá/giá trị tuỳ chỉnh với siêu dữ liệu tệp bổ sung.

Nhận siêu dữ liệu về tệp

Siêu dữ liệu tệp chứa các thuộc tính phổ biến như name, sizecontent_type (thường được gọi là loại MIME), ngoài một số thuộc tính ít phổ biến hơn như content_dispositiontime_created. Bạn có thể truy xuất siêu dữ liệu này từ tệp tham chiếu Cloud Storage bằng phương thức 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();
}

Cập nhật siêu dữ liệu tệp

Bạn có thể cập nhật siêu dữ liệu tệp bất cứ lúc nào sau khi quá trình tải tệp lên hoàn tất bằng cách sử dụng phương thức UpdateMetadata. Hãy tham khảo danh sách đầy đủ để biết thêm thông tin về những thuộc tính có thể được cập nhật. Chỉ những thuộc tính được chỉ định trong siêu dữ liệu mới được cập nhật, tất cả các thuộc tính khác sẽ không bị sửa đổi.

// 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();
}

Bạn có thể xoá các thuộc tính siêu dữ liệu có thể ghi bằng cách truyền chuỗi trống:

// 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();
}

Xử lý lỗi

Có một số lý do có thể khiến lỗi xảy ra khi tải hoặc cập nhật siêu dữ liệu, bao gồm cả tệp không tồn tại hoặc người dùng không có quyền truy cập vào tệp mong muốn. Bạn có thể xem thêm thông tin về lỗi trong phần Xử lý lỗi của tài liệu.

Siêu dữ liệu tuỳ chỉnh

Bạn có thể chỉ định siêu dữ liệu tuỳ chỉnh dưới dạng std::map chứa các thuộc tính std::string.

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

Bạn có thể lưu trữ dữ liệu dành riêng cho ứng dụng cho từng tệp trong siêu dữ liệu tuỳ chỉnh, nhưng bạn nên sử dụng một cơ sở dữ liệu (chẳng hạn như Cơ sở dữ liệu theo thời gian thực của Firebase) để lưu trữ và đồng bộ hoá loại dữ liệu này.

Thuộc tính siêu dữ liệu của tệp

Dưới đây là danh sách đầy đủ các thuộc tính siêu dữ liệu trên một tệp:

Tài sản Loại Có thể ghi
bucket ký tự const* KHÔNG
generation ký tự const* KHÔNG
metageneration ký tự const* KHÔNG
full_path ký tự const* KHÔNG
name ký tự const* KHÔNG
size int64_t KHÔNG
time_created int64_t KHÔNG
updated int64_t KHÔNG
cache_control ký tự const*
content_disposition ký tự const*
content_encoding ký tự const*
content_language ký tự const*
content_type ký tự const*
download_urls std::vector<std::string> KHÔNG
custom_metadata std::map<std::string, std::string>

Các bước tiếp theo

Việc tải lên, tải xuống và cập nhật tệp là rất quan trọng, nhưng khả năng xoá các tệp đó cũng là một việc quan trọng. Hãy tìm hiểu cách xoá tệp khỏi Cloud Storage.