將檔案上傳到 Cloud Storage 參考後,您還可以獲得和更新檔案元數據,例如更新內容類型。文件還可以儲存自訂鍵/值對以及其他文件元資料。
取得文件元數據
檔案元資料包含常見屬性,例如name
、 size
和contentType
(通常稱為 MIME 類型),以及一些不太常見的屬性(例如contentDisposition
和timeCreated
。可以使用metadataWithCompletion:
方法從 Cloud Storage 參考中檢索此元資料。
迅速
// Create reference to the file whose metadata we want to retrieve let forestRef = storageRef.child("images/forest.jpg") // Get metadata properties forestRef.getMetadata { metadata, error in if let error = error { // Uh-oh, an error occurred! } else { // Metadata now contains the metadata for 'images/forest.jpg' } }
Objective-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' } }];
更新檔案元數據
文件上傳完成後,您可以隨時使用updateMetadata:withCompletion:
方法更新文件元資料。有關可以更新哪些屬性的更多信息,請參閱完整列表。僅更新元資料中指定的屬性,所有其他屬性均保持不變。
迅速
// 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 forestRef.updateMetadata(newMetadata) { metadata, error in if let error = error { // Uh-oh, an error occurred! } else { // Updated metadata for 'images/forest.jpg' is returned } }
Objective-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 } }];
您可以透過將可寫元資料屬性設為nil
來刪除它們:
Objective-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 } }];
迅速
let newMetadata = StorageMetadata() newMetadata.contentType = nil // Delete the metadata property forestRef.updateMetadata(newMetadata) { metadata, error in if let error = error { // Uh-oh, an error occurred! } else { // metadata.contentType should be nil } }
處理錯誤
取得或更新元資料時可能發生錯誤的原因有很多,包括檔案不存在或使用者沒有存取所需檔案的權限。有關錯誤的更多資訊可以在文件的處理錯誤部分找到。
自訂元數據
您可以將自訂元資料指定為包含NSString
屬性的NSDictionary
。
迅速
let metadata = [ "customMetadata": [ "location": "Yosemite, CA, USA", "activity": "Hiking" ] ]
Objective-C
NSDictionary *metadata = @{ @"customMetadata": @{ @"location": @"Yosemite, CA, USA", @"activity": @"Hiking" } };
您可以在自訂元資料中儲存每個檔案的應用程式特定數據,但我們強烈建議使用資料庫(例如Firebase 即時資料庫)來儲存和同步此類資料。
文件元資料屬性
下面提供了文件元資料屬性的完整清單:
財產 | 類型 | 可寫 |
---|---|---|
bucket | 細繩 | 不 |
generation | 細繩 | 不 |
metageneration | 細繩 | 不 |
fullPath | 細繩 | 不 |
name | 細繩 | 不 |
size | 整數64 | 不 |
timeCreated | 日期 | 不 |
updated | 日期 | 不 |
md5Hash | 細繩 | 是的 |
cacheControl | 細繩 | 是的 |
contentDisposition | 細繩 | 是的 |
contentEncoding | 細繩 | 是的 |
contentLanguage | 細繩 | 是的 |
contentType | 細繩 | 是的 |
customMetadata | [字串:字串] | 是的 |
上傳、下載和更新檔案很重要,但刪除它們的能力也很重要。讓我們了解如何從雲端儲存中刪除檔案。