Cloud Storage 참조에 파일을 업로드한 후 파일 메타데이터를 가져오거나 업데이트할 수도 있습니다(예: 콘텐츠 유형 업데이트). 파일은 추가 파일 메타데이터와 함께 사용자 지정 키/값 쌍을 저장할 수도 있습니다.
파일 메타데이터 가져오기
파일 메타데이터에는 name
, size
, contentType
(종종 MIME 유형이라고도 함)과 같은 일반적인 속성과 contentDisposition
및 timeCreated
같은 덜 일반적인 속성이 포함되어 있습니다. 이 메타데이터는 getMetadata()
메서드를 사용하여 Cloud Storage 참조에서 검색할 수 있습니다. getMetadata()
는 전체 메타데이터를 포함하는 Promise
를 반환하거나 Promise
가 거부하는 경우 오류를 반환합니다.
Web version 9
import { getStorage, ref, getMetadata } from "firebase/storage"; // Create a reference to the file whose metadata we want to retrieve const storage = getStorage(); const forestRef = ref(storage, 'images/forest.jpg'); // Get metadata properties getMetadata(forestRef) .then((metadata) => { // Metadata now contains the metadata for 'images/forest.jpg' }) .catch((error) => { // Uh-oh, an error occurred! });
Web version 8
// Create a reference to the file whose metadata we want to retrieve var forestRef = storageRef.child('images/forest.jpg'); // Get metadata properties forestRef.getMetadata() .then((metadata) => { // Metadata now contains the metadata for 'images/forest.jpg' }) .catch((error) => { // Uh-oh, an error occurred! });
파일 메타데이터 업데이트
파일 업로드가 완료된 후 updateMetadata()
메서드를 사용하여 언제든지 파일 메타데이터를 업데이트할 수 있습니다. 업데이트할 수 있는 속성에 대한 자세한 내용은 전체 목록 을 참조하십시오. 메타데이터에 지정된 속성만 업데이트되고 다른 모든 속성은 수정되지 않은 상태로 유지됩니다. updateMetadata()
는 전체 메타데이터를 포함하는 Promise
를 반환하거나 Promise
가 거부하는 경우 오류를 반환합니다.
Web version 9
import { getStorage, ref, updateMetadata } from "firebase/storage"; // Create a reference to the file whose metadata we want to change const storage = getStorage(); const forestRef = ref(storage, 'images/forest.jpg'); // Create file metadata to update const newMetadata = { cacheControl: 'public,max-age=300', contentType: 'image/jpeg' }; // Update metadata properties updateMetadata(forestRef, newMetadata) .then((metadata) => { // Updated metadata for 'images/forest.jpg' is returned in the Promise }).catch((error) => { // Uh-oh, an error occurred! });
Web version 8
// Create a reference to the file whose metadata we want to change var forestRef = storageRef.child('images/forest.jpg'); // Create file metadata to update var newMetadata = { cacheControl: 'public,max-age=300', contentType: 'image/jpeg' }; // Update metadata properties forestRef.updateMetadata(newMetadata) .then((metadata) => { // Updated metadata for 'images/forest.jpg' is returned in the Promise }).catch((error) => { // Uh-oh, an error occurred! });
null
로 설정하여 메타데이터 속성을 삭제할 수 있습니다.
Web version 9
import { getStorage, ref, updateMetadata } from "firebase/storage"; const storage = getStorage(); const forestRef = ref(storage, 'images/forest.jpg'); // Create file metadata with property to delete const deleteMetadata = { contentType: null }; // Delete the metadata property updateMetadata(forestRef, deleteMetadata) .then((metadata) => { // metadata.contentType should be null }).catch((error) => { // Uh-oh, an error occurred! });
Web version 8
// Create file metadata with property to delete var deleteMetadata = { contentType: null }; // Delete the metadata property forestRef.updateMetadata(deleteMetadata) .then((metadata) => { // metadata.contentType should be null }).catch((error) => { // Uh-oh, an error occurred! });
오류 처리
존재하지 않는 파일 또는 원하는 파일에 대한 액세스 권한이 없는 사용자를 포함하여 메타데이터를 가져오거나 업데이트할 때 오류가 발생할 수 있는 여러 가지 이유가 있습니다. 오류에 대한 자세한 내용은 문서의 오류 처리 섹션에서 찾을 수 있습니다.
맞춤 메타데이터
String
속성을 포함하는 개체로 사용자 지정 메타데이터를 지정할 수 있습니다.
Web version 9
const metadata = { customMetadata: { 'location': 'Yosemite, CA, USA', 'activity': 'Hiking' } };
Web version 8
var metadata = { customMetadata: { 'location': 'Yosemite, CA, USA', 'activity': 'Hiking' } };
각 파일에 대한 추가 앱별 데이터를 저장하기 위해 사용자 지정 메타데이터를 사용할 수 있지만 데이터베이스(예: Firebase 실시간 데이터베이스 )를 사용하여 이러한 유형의 데이터를 저장하고 동기화하는 것이 좋습니다.
파일 메타데이터 속성
파일의 전체 메타데이터 속성 목록은 아래에서 확인할 수 있습니다.
속성 | 유형 | 쓰기 가능 |
---|---|---|
bucket | 끈 | 아니요 |
generation | 끈 | 아니요 |
metageneration | 끈 | 아니요 |
fullPath | 끈 | 아니요 |
name | 끈 | 아니요 |
size | 숫자 | 아니요 |
timeCreated | 끈 | 아니요 |
updated | 끈 | 아니요 |
md5Hash | 끈 | 업로드 시 YES, updateMetadata 시 NO |
cacheControl | 끈 | 예 |
contentDisposition | 끈 | 예 |
contentEncoding | 끈 | 예 |
contentLanguage | 끈 | 예 |
contentType | 끈 | 예 |
customMetadata | 문자열->문자열 매핑을 포함하는 객체 | 예 |
파일을 업로드, 다운로드 및 업데이트하는 것도 중요하지만 파일을 제거할 수 있는 것도 중요합니다. Cloud Storage에서 파일을 삭제 하는 방법을 알아보겠습니다.