Apple 플랫폼에서 Cloud Storage로 파일 메타데이터 사용

Cloud Storage 참조로 파일을 업로드한 후 콘텐츠 유형 등 파일 메타데이터를 가져오거나 업데이트할 수 있습니다. 또한 추가 파일 메타데이터로 커스텀 키-값 쌍을 저장할 수 있습니다.

파일 메타데이터 가져오기

파일 메타데이터는 name, size, contentType(통칭 MIME 유형) 등의 일반적인 속성뿐 아니라 contentDispositiontimeCreated 등의 비일반적 속성도 포함합니다. metadataWithCompletion: 메서드를 사용하여 Cloud Storage 참조에서 이러한 메타데이터를 가져올 수 있습니다.

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 실시간 데이터베이스 같은 데이터베이스를 사용하는 것이 좋습니다.

파일 메타데이터 속성

파일 메타데이터 속성의 전체 목록은 다음과 같습니다.

속성 유형 쓰기 가능
bucket 문자열 No
generation 문자열 No
metageneration 문자열 No
fullPath 문자열 No
name 문자열 No
size Int64 No
timeCreated Date No
updated Date No
md5Hash 문자열 Yes
cacheControl 문자열 Yes
contentDisposition 문자열 Yes
contentEncoding 문자열 Yes
contentLanguage 문자열 Yes
contentType 문자열 Yes
customMetadata [String: String] Yes

파일 업로드, 다운로드 및 업데이트도 중요하지만 파일을 삭제할 수도 있어야 합니다. Cloud Storage에서 파일을 삭제하는 방법을 알아보세요.