שימוש במטא-נתונים של קבצים ב-Cloud Storage בפלטפורמות של Apple

אחרי שמעלים קובץ לקובץ עזר של 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 {
  // ...
}

טיפול בשגיאות

יש כמה סיבות לכך שעשויות להתרחש שגיאות בקבלה או בעדכון של מטא-נתונים, כולל העובדה שהקובץ לא קיים או שהמשתמש לא קיבל הרשאה לגשת לקובץ הרצוי. מידע נוסף על שגיאות זמין בקטע טיפול בשגיאות במסמכים.

מטא-נתונים בהתאמה אישית

אפשר לציין מטא-נתונים בהתאמה אישית כ-NSDictionary שמכיל את NSString נכסים.

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 String לא
generation String לא
metageneration String לא
fullPath String לא
name String לא
size Int64 לא
timeCreated תאריך לא
updated תאריך לא
md5Hash String כן
cacheControl String כן
contentDisposition String כן
contentEncoding String כן
contentLanguage String כן
contentType String כן
customMetadata [מחרוזת: מחרוזת] כן

חשוב להעלות, להוריד ולעדכן קבצים, אבל זו גם היכולת כדי להסיר אותן. עכשיו נלמד איך למחוק קבצים מ-Cloud Storage.