Sử dụng siêu dữ liệu tệp với Cloud Storage trên các nền tảng của Apple

Sau khi tải một tệp lên Cloud Storage, bạn cũng có thể nhận 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. Các tệp cũng có thể lưu trữ các cặp khoá/giá trị tuỳ chỉnh cùng với siêu dữ liệu bổ sung của tệp.

Lấy siêu dữ liệu của tệp

Siêu dữ liệu tệp chứa các thuộc tính phổ biến như name, sizecontentType (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ư contentDispositiontimeCreated. Bạn có thể truy xuất siêu dữ liệu này từ một tệp tham chiếu Cloud Storage bằng phương thức metadataWithCompletion:.

// Create reference to the file whose metadata we want to retrieve
let forestRef = storageRef.child("images/forest.jpg")

// Get metadata properties
do {
  let metadata = try await forestRef.getMetadata()
} catch {
  // ...
}
    
// Create reference to the file whose metadata we want to retrieve
FIRStorageReference *forestRef = [storageRef child:@"images/forest.jpg"];

// Get metadata properties
[forestRef metadataWithCompletion:^(FIRStorageMetadata *metadata, NSError *error) {
  if (error != nil) {
    // Uh-oh, an error occurred!
  } else {
    // Metadata now contains the metadata for 'images/forest.jpg'
  }
}];
  

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

Bạn có thể cập nhật siêu dữ liệu của 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:withCompletion:. 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 đều không thay đổi.

// Create reference to the file whose metadata we want to change
let forestRef = storageRef.child("images/forest.jpg")

// Create file metadata to update
let newMetadata = StorageMetadata()
newMetadata.cacheControl = "public,max-age=300"
newMetadata.contentType = "image/jpeg"

// Update metadata properties
do {
  let updatedMetadata = try await forestRef.updateMetadata(newMetadata)
} catch {
  // ...
}
    
// Create reference to the file whose metadata we want to change
FIRStorageReference *forestRef = [storageRef child:@"images/forest.jpg"];

// Create file metadata to update
FIRStorageMetadata *newMetadata = [[FIRStorageMetadata alloc] init];
newMetadata.cacheControl = @"public,max-age=300";
newMetadata.contentType = @"image/jpeg";

// Update metadata properties
[forestRef updateMetadata:newMetadata completion:^(FIRStorageMetadata *metadata, NSError *error){
  if (error != nil) {
    // Uh-oh, an error occurred!
  } else {
    // Updated metadata for 'images/forest.jpg' is returned
  }
}];
  

Bạn có thể xoá các thuộc tính siêu dữ liệu có thể ghi bằng cách đặt các thuộc tính đó thành nil:

FIRStorageMetadata *newMetadata = [[FIRStorageMetadata alloc] init];
newMetadata.contentType = nil;

// Delete the metadata property
[forestRef updateMetadata:newMetadata completion:^(FIRStorageMetadata *metadata, NSError *error){
  if (error != nil) {
    // Uh-oh, an error occurred!
  } else {
    // metadata.contentType should be nil
  }
}];
let newMetadata = StorageMetadata()
newMetadata.contentType = nil

do {
  // Delete the metadata property
  let updatedMetadata = try await forestRef.updateMetadata(newMetadata)
} catch {
  // ...
}

Xử lý lỗi

Có một số lý do khiến lỗi có thể xảy ra khi nhận hoặc cập nhật siêu dữ liệu, bao gồm cả việ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ề các 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 một NSDictionary chứa các thuộc tính NSString.

let metadata = [
  "customMetadata": [
    "location": "Yosemite, CA, USA",
    "activity": "Hiking"
  ]
]
    
NSDictionary *metadata = @{
  @"customMetadata": @{
    @"location": @"Yosemite, CA, USA",
    @"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 cơ sở dữ liệu (chẳng hạn như Firebase Realtime Database) để 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

Sau đây là danh sách đầy đủ các thuộc tính siêu dữ liệu của một tệp:

Tài sản Loại Có thể ghi
bucket Chuỗi Không
generation Chuỗi Không
metageneration Chuỗi Không
fullPath Chuỗi Không
name Chuỗi Không
size Int64 Không
timeCreated Ngày Không
updated Ngày Không
md5Hash Chuỗi
cacheControl Chuỗi
contentDisposition Chuỗi
contentEncoding Chuỗi
contentLanguage Chuỗi
contentType Chuỗi
customMetadata [Chuỗi: Chuỗi]

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 việc có thể xoá tệp cũng quan trọng không kém. Hãy tìm hiểu cách xoá tệp khỏi Cloud Storage.