Cloud Storage 참조에 파일을 업로드한 후 파일 메타데이터를 가져오거나 업데이트할 수 있습니다. 예를 들어 콘텐츠 유형을 업데이트할 수 있습니다. 또한 추가 파일 메타데이터로 커스텀 키-값 쌍을 저장할 수 있습니다.
파일 메타데이터 가져오기
파일 메타데이터는 name, size, contentType(통칭 MIME 유형) 등의 일반적인 속성뿐 아니라 contentDisposition 및 timeCreated 등의 비일반적 속성도 포함합니다. getMetadata() 메서드를 사용하여 Cloud Storage 참조에서 이러한 메타데이터를 가져올 수 있습니다. getMetadata()는 전체 메타데이터를 포함하는 Promise를 반환하거나 Promise가 거부되면 오류를 반환합니다.
Web
import{getStorage,ref,getMetadata}from"firebase/storage";// Create a reference to the file whose metadata we want to retrieveconststorage=getStorage();constforestRef=ref(storage,'images/forest.jpg');// Get metadata propertiesgetMetadata(forestRef).then((metadata)=>{// Metadata now contains the metadata for 'images/forest.jpg'}).catch((error)=>{// Uh-oh, an error occurred!});
// Create a reference to the file whose metadata we want to retrievevarforestRef=storageRef.child('images/forest.jpg');// Get metadata propertiesforestRef.getMetadata().then((metadata)=>{// Metadata now contains the metadata for 'images/forest.jpg'}).catch((error)=>{// Uh-oh, an error occurred!});
파일 업로드가 완료된 후 언제든지 updateMetadata() 메서드를 사용하여 파일 메타데이터를 업데이트할 수 있습니다. 업데이트할 수 있는 속성의 종류는 전체 목록을 참조하세요. 메타데이터에 지정된 속성만 업데이트되며 나머지 속성은 그대로 유지됩니다. updateMetadata()는 전체 메타데이터를 포함하는 Promise를 반환하거나 Promise가 거부되면 오류를 반환합니다.
Web
import{getStorage,ref,updateMetadata}from"firebase/storage";// Create a reference to the file whose metadata we want to changeconststorage=getStorage();constforestRef=ref(storage,'images/forest.jpg');// Create file metadata to updateconstnewMetadata={cacheControl:'public,max-age=300',contentType:'image/jpeg'};// Update metadata propertiesupdateMetadata(forestRef,newMetadata).then((metadata)=>{// Updated metadata for 'images/forest.jpg' is returned in the Promise}).catch((error)=>{// Uh-oh, an error occurred!});
// Create a reference to the file whose metadata we want to changevarforestRef=storageRef.child('images/forest.jpg');// Create file metadata to updatevarnewMetadata={cacheControl:'public,max-age=300',contentType:'image/jpeg'};// Update metadata propertiesforestRef.updateMetadata(newMetadata).then((metadata)=>{// Updated metadata for 'images/forest.jpg' is returned in the Promise}).catch((error)=>{// Uh-oh, an error occurred!});
import{getStorage,ref,updateMetadata}from"firebase/storage";conststorage=getStorage();constforestRef=ref(storage,'images/forest.jpg');// Create file metadata with property to deleteconstdeleteMetadata={contentType:null};// Delete the metadata propertyupdateMetadata(forestRef,deleteMetadata).then((metadata)=>{// metadata.contentType should be null}).catch((error)=>{// Uh-oh, an error occurred!});
// Create file metadata with property to deletevardeleteMetadata={contentType:null};// Delete the metadata propertyforestRef.updateMetadata(deleteMetadata).then((metadata)=>{// metadata.contentType should be null}).catch((error)=>{// Uh-oh, an error occurred!});
[null,null,["최종 업데이트: 2025-08-13(UTC)"],[],[],null,["\u003cbr /\u003e\n\nAfter uploading a file to Cloud Storage reference, you can also get\nor update the file metadata, for example to update the content type. Files\ncan also store custom key/value pairs with additional file metadata.\n| **Note:** By default, a Cloud Storage for Firebase bucket requires Firebase Authentication to perform any action on the bucket's data or files. You can change your Firebase Security Rules for Cloud Storage to [allow unauthenticated access for specific situations](/docs/storage/security/rules-conditions#public). However, for most situations, we strongly recommend [restricting access and setting up robust security rules](/docs/storage/security/get-started) (especially for production apps). Note that if you use Google App Engine and have a default Cloud Storage bucket with a name format of `*.appspot.com`, you may need to consider [how your security rules impact access to App Engine files](/docs/storage/gcp-integration#security-rules-and-app-engine-files).\n\nGet File Metadata\n\nFile metadata contains common properties such as `name`, `size`, and\n`contentType` (often referred to as MIME type) in addition to some less\ncommon ones like `contentDisposition` and `timeCreated`. This metadata can be\nretrieved from a Cloud Storage reference using\nthe `getMetadata()` method. `getMetadata()` returns a `Promise` containing the\ncomplete metadata, or an error if the `Promise` rejects. \n\nWeb \n\n```javascript\nimport { getStorage, ref, getMetadata } from \"firebase/storage\";\n\n// Create a reference to the file whose metadata we want to retrieve\nconst storage = getStorage();\nconst forestRef = ref(storage, 'images/forest.jpg');\n\n// Get metadata properties\ngetMetadata(forestRef)\n .then((metadata) =\u003e {\n // Metadata now contains the metadata for 'images/forest.jpg'\n })\n .catch((error) =\u003e {\n // Uh-oh, an error occurred!\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/storage-next/file-metadata/storage_get_metadata.js#L8-L21\n```\n\nWeb \n\n```javascript\n// Create a reference to the file whose metadata we want to retrieve\nvar forestRef = storageRef.child('images/forest.jpg');\n\n// Get metadata properties\nforestRef.getMetadata()\n .then((metadata) =\u003e {\n // Metadata now contains the metadata for 'images/forest.jpg'\n })\n .catch((error) =\u003e {\n // Uh-oh, an error occurred!\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/storage/file-metadata.js#L8-L18\n```\n\nUpdate File Metadata\n\nYou can update file metadata at any time after the file upload completes by\nusing the `updateMetadata()` method. Refer to the\n[full list](#file_metadata_properties) for more information on what properties\ncan be updated. Only the properties specified in the metadata are updated,\nall others are left unmodified. `updateMetadata()` returns a `Promise`\ncontaining the complete metadata, or an error if the `Promise` rejects. \n\nWeb \n\n```javascript\nimport { getStorage, ref, updateMetadata } from \"firebase/storage\";\n\n// Create a reference to the file whose metadata we want to change\nconst storage = getStorage();\nconst forestRef = ref(storage, 'images/forest.jpg');\n\n// Create file metadata to update\nconst newMetadata = {\n cacheControl: 'public,max-age=300',\n contentType: 'image/jpeg'\n};\n\n// Update metadata properties\nupdateMetadata(forestRef, newMetadata)\n .then((metadata) =\u003e {\n // Updated metadata for 'images/forest.jpg' is returned in the Promise\n }).catch((error) =\u003e {\n // Uh-oh, an error occurred!\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/storage-next/file-metadata/storage_update_metadata.js#L8-L26\n```\n\nWeb \n\n```javascript\n// Create a reference to the file whose metadata we want to change\nvar forestRef = storageRef.child('images/forest.jpg');\n\n// Create file metadata to update\nvar newMetadata = {\n cacheControl: 'public,max-age=300',\n contentType: 'image/jpeg'\n};\n\n// Update metadata properties\nforestRef.updateMetadata(newMetadata)\n .then((metadata) =\u003e {\n // Updated metadata for 'images/forest.jpg' is returned in the Promise\n }).catch((error) =\u003e {\n // Uh-oh, an error occurred!\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/storage/file-metadata.js#L26-L41\n```\n\nYou can delete a metadata property by setting it to `null`: \n\nWeb \n\n```javascript\nimport { getStorage, ref, updateMetadata } from \"firebase/storage\";\n\nconst storage = getStorage();\nconst forestRef = ref(storage, 'images/forest.jpg');\n\n// Create file metadata with property to delete\nconst deleteMetadata = {\n contentType: null\n};\n\n// Delete the metadata property\nupdateMetadata(forestRef, deleteMetadata)\n .then((metadata) =\u003e {\n // metadata.contentType should be null\n }).catch((error) =\u003e {\n // Uh-oh, an error occurred!\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/storage-next/file-metadata/storage_delete_metadata.js#L8-L24\n```\n\nWeb \n\n```javascript\n// Create file metadata with property to delete\nvar deleteMetadata = {\n contentType: null\n};\n\n// Delete the metadata property\nforestRef.updateMetadata(deleteMetadata)\n .then((metadata) =\u003e {\n // metadata.contentType should be null\n }).catch((error) =\u003e {\n // Uh-oh, an error occurred!\n });https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/storage/file-metadata.js#L50-L62\n```\n\nHandle Errors\n\nThere are a number of reasons why errors may occur on getting or updating\nmetadata, including the file not existing, or the user not having permission\nto access the desired file. More information on errors can be found in the\n[Handle Errors](/docs/storage/web/handle-errors)\nsection of the docs.\n\nCustom Metadata\n\nYou can specify custom metadata as an object containing `String` properties. \n\nWeb \n\n```javascript\nconst metadata = {\n customMetadata: {\n 'location': 'Yosemite, CA, USA',\n 'activity': 'Hiking'\n }\n};https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/snippets/storage-next/file-metadata/storage_custom_metadata.js#L8-L13\n```\n\nWeb \n\n```javascript\nvar metadata = {\n customMetadata: {\n 'location': 'Yosemite, CA, USA',\n 'activity': 'Hiking'\n }\n};https://github.com/firebase/snippets-web/blob/467eaa165dcbd9b3ab15711e76fa52237ba37f8b/storage/file-metadata.js#L68-L73\n```\n\nYou can use custom metadata for storing additional app specific data for each\nfile, but we highly recommend using a database (such as the\n[Firebase Realtime Database](/docs/database))\nto store and synchronize this type of data.\n\nFile Metadata Properties\n\nA full list of metadata properties on a file is available below:\n\n| Property | Type | Writable |\n|----------------------|--------------------------------------------|-------------------------------------|\n| `bucket` | string | NO |\n| `generation` | string | NO |\n| `metageneration` | string | NO |\n| `fullPath` | string | NO |\n| `name` | string | NO |\n| `size` | number | NO |\n| `timeCreated` | string | NO |\n| `updated` | string | NO |\n| `md5Hash` | string | YES on upload, NO on updateMetadata |\n| `cacheControl` | string | YES |\n| `contentDisposition` | string | YES |\n| `contentEncoding` | string | YES |\n| `contentLanguage` | string | YES |\n| `contentType` | string | YES |\n| `customMetadata` | Object containing string-\\\u003estring mappings | YES |\n\n| **Note:** at present, setting the `md5Hash` property on upload doesn't affect the upload, as hash verification is not yet implemented.\n\nUploading, downloading, and updating files is important, but so is being able\nto remove them. Let's learn how to\n[delete files](/docs/storage/web/delete-files)\nfrom Cloud Storage."]]