Sau khi tải tệp lên tài liệu tham khảo Cloud Storage, bạn cũng có thể lấy 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 khóa/giá trị tùy chỉnh với siêu dữ liệu tệp bổ sung.
Nhận siêu dữ liệu tệp
Siêu dữ liệu tệp chứa các thuộc tính phổ biến như name
, size
và contentType
(thường được gọi là loại MIME) bên cạnh một số thuộc tính ít phổ biến hơn như contentDisposition
và timeCreated
. Siêu dữ liệu này có thể được truy xuất từ tham chiếu Cloud Storage bằng phương thức metadataWithCompletion:
Nhanh
// 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 { // ... }
Mục tiêu-C
// 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 tệp
Bạn có thể cập nhật siêu dữ liệu tệp bất kỳ 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:
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ỉ các 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 không được sửa đổi.
Nhanh
// 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 { // ... }
Mục tiêu-C
// 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ể xóa các thuộc tính siêu dữ liệu có thể ghi bằng cách đặt chúng thành nil
:
Mục tiêu-C
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 } }];
Nhanh
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ả 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ể tìm 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 tùy chỉnh
Bạn có thể chỉ định siêu dữ liệu tùy chỉnh làm NSDictionary
chứa các thuộc tính NSString
.
Nhanh
let metadata = [ "customMetadata": [ "location": "Yosemite, CA, USA", "activity": "Hiking" ] ]
Mục tiêu-C
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 tùy chỉnh nhưng chúng tôi khuyên bạn nên sử dụng cơ sở dữ liệu (chẳng hạn như Cơ sở dữ liệu thời gian thực Firebase ) để lưu trữ và đồng bộ hóa loại dữ liệu này.
Thuộc tính siêu dữ liệu 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 | Kiểu | Có thể ghi |
---|---|---|
bucket | Sợi dây | KHÔNG |
generation | Sợi dây | KHÔNG |
metageneration | Sợi dây | KHÔNG |
fullPath | Sợi dây | KHÔNG |
name | Sợi dây | KHÔNG |
size | Int64 | KHÔNG |
timeCreated | Ngày | KHÔNG |
updated | Ngày | KHÔNG |
md5Hash | Sợi dây | Đúng |
cacheControl | Sợi dây | Đúng |
contentDisposition | Sợi dây | Đúng |
contentEncoding | Sợi dây | Đúng |
contentLanguage | Sợi dây | Đúng |
contentType | Sợi dây | Đúng |
customMetadata | [Chuỗi: Chuỗi] | Đúng |
Tải lên, tải xuống và cập nhật tệp là quan trọng nhưng việc có thể xóa chúng cũng quan trọng không kém. Hãy tìm hiểu cách xóa tệp khỏi Cloud Storage.