將檔案上傳至「Cloud Storage」參照後,還能取得以下福利: 並更新檔案中繼資料,例如更新內容類型。檔案 也可以儲存自訂鍵/值組合,以及額外的檔案中繼資料。
取得檔案中繼資料
檔案中繼資料包含常用屬性,例如 name
、size
和
contentType
(通常稱為 MIME 類型)
contentDisposition
和 timeCreated
等常用屬性。中繼資料可以是
從 Cloud Storage 參照擷取
metadataWithCompletion:
方法。
Swift
// 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 { // ... }
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:
方法傳送物件要求詳情請參閱
完整清單,進一步瞭解哪些房源
可以更新系統只會更新中繼資料中指定的屬性。
其他則保持不變。
Swift
// 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 { // ... }
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 } }];
Swift
let newMetadata = StorageMetadata() newMetadata.contentType = nil do { // Delete the metadata property let updatedMetadata = try await forestRef.updateMetadata(newMetadata) } catch { // ... }
處理錯誤
發生錯誤的可能原因有很多, 中繼資料,包括不存在的檔案,或使用者沒有權限 來存取所需檔案如要進一步瞭解錯誤,請前往 處理錯誤 一節。
自訂中繼資料
您可以將自訂中繼資料指定為包含 NSString
的 NSDictionary
資源。
Swift
let metadata = [ "customMetadata": [ "location": "Yosemite, CA, USA", "activity": "Hiking" ] ]
Objective-C
NSDictionary *metadata = @{ @"customMetadata": @{ @"location": @"Yosemite, CA, USA", @"activity": @"Hiking" } };
您可以在自訂中繼資料內儲存每個檔案的應用程式專屬資料,但 建議您使用資料庫 (例如 Firebase Realtime Database),儲存並同步處理這種類型的 資料。
檔案中繼資料屬性
完整的檔案中繼資料屬性清單請見下方:
屬性 | 類型 | 可寫入 |
---|---|---|
bucket |
字串 | 否 |
generation |
字串 | 否 |
metageneration |
字串 | 否 |
fullPath |
字串 | 否 |
name |
字串 | 否 |
size |
Int64 | 否 |
timeCreated |
日期 | 否 |
updated |
日期 | 否 |
md5Hash |
字串 | 是 |
cacheControl |
字串 | 是 |
contentDisposition |
字串 | 是 |
contentEncoding |
字串 | 是 |
contentLanguage |
字串 | 是 |
contentType |
字串 | 是 |
customMetadata |
[字串:字串] | 是 |
上傳、下載及更新檔案很重要 移除。現在來瞭解如何 刪除檔案 出發地:Cloud Storage。