Android에서 Cloud Storage로 파일 메타데이터 사용
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
Cloud Storage 참조로 파일을 업로드한 후 파일 메타데이터를 가져오거나 업데이트할 수 있습니다. 예를 들어 콘텐츠 유형을 확인 또는 업데이트할 수 있습니다.
또한 추가 파일 메타데이터로 커스텀 키-값 쌍을 저장할 수 있습니다.
파일 메타데이터는 name
, size
, contentType
(통칭 MIME 유형) 등의 일반적인 속성뿐 아니라 contentDisposition
및 timeCreated
등의 비일반적 속성도 포함합니다. getMetadata()
메서드를 사용하여 Cloud Storage 참조에서 이러한 메타데이터를 가져올 수 있습니다.
Kotlin
// Create a storage reference from our app
val storageRef = storage.reference
// Get reference to the file
val forestRef = storageRef.child("images/forest.jpg")
forestRef.metadata.addOnSuccessListener { metadata ->
// Metadata now contains the metadata for 'images/forest.jpg'
}.addOnFailureListener {
// Uh-oh, an error occurred!
}
Java
// Create a storage reference from our app
StorageReference storageRef = storage.getReference();
// Get reference to the file
StorageReference forestRef = storageRef.child("images/forest.jpg");
forestRef.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
@Override
public void onSuccess(StorageMetadata storageMetadata) {
// Metadata now contains the metadata for 'images/forest.jpg'
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Uh-oh, an error occurred!
}
});
파일 업로드가 완료된 후 언제든지 updateMetadata()
메서드를 사용하여 파일 메타데이터를 업데이트할 수 있습니다. 업데이트할 수 있는 속성의 종류는 전체 목록을 참조하세요. 메타데이터에 지정된 속성만 업데이트되며 나머지 속성은 그대로 유지됩니다.
Kotlin
// Create a storage reference from our app
val storageRef = storage.reference
// Get reference to the file
val forestRef = storageRef.child("images/forest.jpg")
// Create file metadata including the content type
val metadata = storageMetadata {
contentType = "image/jpg"
setCustomMetadata("myCustomProperty", "myValue")
}
// Update metadata properties
forestRef.updateMetadata(metadata).addOnSuccessListener { updatedMetadata ->
// Updated metadata is in updatedMetadata
}.addOnFailureListener {
// Uh-oh, an error occurred!
}
Java
// Create a storage reference from our app
StorageReference storageRef = storage.getReference();
// Get reference to the file
StorageReference forestRef = storageRef.child("images/forest.jpg");
// Create file metadata including the content type
StorageMetadata metadata = new StorageMetadata.Builder()
.setContentType("image/jpg")
.setCustomMetadata("myCustomProperty", "myValue")
.build();
// Update metadata properties
forestRef.updateMetadata(metadata)
.addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
@Override
public void onSuccess(StorageMetadata storageMetadata) {
// Updated metadata is in storageMetadata
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Uh-oh, an error occurred!
}
});
null
을 전달하여 쓰기 가능한 메타데이터 속성을 삭제할 수 있습니다.
Kotlin
// Create file metadata with property to delete
val metadata = storageMetadata {
contentType = null
}
// Delete the metadata property
forestRef.updateMetadata(metadata).addOnSuccessListener { updatedMetadata ->
// updatedMetadata.contentType should be null
}.addOnFailureListener {
// Uh-oh, an error occurred!
}
Java
// Create file metadata with property to delete
StorageMetadata metadata = new StorageMetadata.Builder()
.setContentType(null)
.build();
// Delete the metadata property
forestRef.updateMetadata(metadata)
.addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
@Override
public void onSuccess(StorageMetadata storageMetadata) {
// metadata.contentType should be null
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Uh-oh, an error occurred!
}
});
오류 처리
메타데이터를 가져오거나 업데이트할 때 오류가 발생하는 데에는 파일이 없거나 파일에 대한 액세스 권한이 없는 경우 등 다양한 이유가 있습니다. 오류에 대한 자세한 내용은 문서의 오류 처리 섹션을 참조하세요.
StorageMetadata.Builder
클래스의 setCustomMetadata()
메서드를 사용하여 커스텀 메타데이터를 지정할 수 있습니다.
Kotlin
val metadata = storageMetadata {
setCustomMetadata("location", "Yosemite, CA, USA")
setCustomMetadata("activity", "Hiking")
}
Java
StorageMetadata metadata = new StorageMetadata.Builder()
.setCustomMetadata("location", "Yosemite, CA, USA")
.setCustomMetadata("activity", "Hiking")
.build();
커스텀 메타데이터에 각 파일의 앱별 데이터를 저장할 수 있지만 이러한 유형의 데이터를 저장하고 동기화할 때는 Firebase Realtime Database와 같은 데이터베이스를 사용하는 것이 좋습니다.
파일 메타데이터 속성의 전체 목록은 다음과 같습니다.
속성 getter |
유형 |
setter 존재 여부 |
getBucket |
String |
아니요 |
getGeneration |
String |
아니요 |
getMetadataGeneration |
String |
아니요 |
getPath |
String |
아니요 |
getName |
String |
아니요 |
getSizeBytes |
long |
아니요 |
getCreationTimeMillis |
long |
아니요 |
getUpdatedTimeMillis |
long |
아니요 |
getMd5Hash |
String |
아니요 |
getCacheControl |
String |
예 |
getContentDisposition |
String |
예 |
getContentEncoding |
String |
예 |
getContentLanguage |
String |
예 |
getContentType |
String |
예 |
getCustomMetadata |
String |
예 |
getCustomMetadataKeys |
Set<String> |
아니요 |
파일 업로드, 다운로드 및 업데이트도 중요하지만 파일을 삭제할 수도 있어야 합니다. Cloud Storage에서 파일을 삭제하는 방법을 알아보세요.
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2025-08-21(UTC)
[null,null,["최종 업데이트: 2025-08-21(UTC)"],[],[],null,["\u003cbr /\u003e\n\nAfter uploading a file to Cloud Storage reference, you can also get\nand update the file metadata, for example to view or update the content type.\nFiles can 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. \n\nKotlin \n\n```kotlin\n// Create a storage reference from our app\nval storageRef = storage.reference\n\n// Get reference to the file\nval forestRef = storageRef.child(\"images/forest.jpg\")\n``` \n\n```kotlin\nforestRef.metadata.addOnSuccessListener { metadata -\u003e\n // Metadata now contains the metadata for 'images/forest.jpg'\n}.addOnFailureListener {\n // Uh-oh, an error occurred!\n}https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/storage/app/src/main/java/com/google/firebase/referencecode/storage/kotlin/StorageActivity.kt#L345-L349\n```\n\nJava \n\n```java\n// Create a storage reference from our app\nStorageReference storageRef = storage.getReference();\n\n// Get reference to the file\nStorageReference forestRef = storageRef.child(\"images/forest.jpg\");\n``` \n\n```java\nforestRef.getMetadata().addOnSuccessListener(new OnSuccessListener\u003cStorageMetadata\u003e() {\n @Override\n public void onSuccess(StorageMetadata storageMetadata) {\n // Metadata now contains the metadata for 'images/forest.jpg'\n }\n}).addOnFailureListener(new OnFailureListener() {\n @Override\n public void onFailure(@NonNull Exception exception) {\n // Uh-oh, an error occurred!\n }\n});https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/storage/app/src/main/java/com/google/firebase/referencecode/storage/StorageActivity.java#L437-L447\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. \n\nKotlin \n\n```kotlin\n// Create a storage reference from our app\nval storageRef = storage.reference\n\n// Get reference to the file\nval forestRef = storageRef.child(\"images/forest.jpg\")\n``` \n\n```kotlin\n// Create file metadata including the content type\nval metadata = storageMetadata {\n contentType = \"image/jpg\"\n setCustomMetadata(\"myCustomProperty\", \"myValue\")\n}\n\n// Update metadata properties\nforestRef.updateMetadata(metadata).addOnSuccessListener { updatedMetadata -\u003e\n // Updated metadata is in updatedMetadata\n}.addOnFailureListener {\n // Uh-oh, an error occurred!\n}https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/storage/app/src/main/java/com/google/firebase/referencecode/storage/kotlin/StorageActivity.kt#L353-L364\n```\n\nJava \n\n```java\n// Create a storage reference from our app\nStorageReference storageRef = storage.getReference();\n\n// Get reference to the file\nStorageReference forestRef = storageRef.child(\"images/forest.jpg\");\n``` \n\n```java\n// Create file metadata including the content type\nStorageMetadata metadata = new StorageMetadata.Builder()\n .setContentType(\"image/jpg\")\n .setCustomMetadata(\"myCustomProperty\", \"myValue\")\n .build();\n\n// Update metadata properties\nforestRef.updateMetadata(metadata)\n .addOnSuccessListener(new OnSuccessListener\u003cStorageMetadata\u003e() {\n @Override\n public void onSuccess(StorageMetadata storageMetadata) {\n // Updated metadata is in storageMetadata\n }\n })\n .addOnFailureListener(new OnFailureListener() {\n @Override\n public void onFailure(@NonNull Exception exception) {\n // Uh-oh, an error occurred!\n }\n });https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/storage/app/src/main/java/com/google/firebase/referencecode/storage/StorageActivity.java#L451-L470\n```\n\nYou can delete writable metadata properties by passing `null`: \n\nKotlin \n\n```kotlin\n// Create file metadata with property to delete\nval metadata = storageMetadata {\n contentType = null\n}\n\n// Delete the metadata property\nforestRef.updateMetadata(metadata).addOnSuccessListener { updatedMetadata -\u003e\n // updatedMetadata.contentType should be null\n}.addOnFailureListener {\n // Uh-oh, an error occurred!\n}https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/storage/app/src/main/java/com/google/firebase/referencecode/storage/kotlin/StorageActivity.kt#L374-L384\n```\n\nJava \n\n```java\n// Create file metadata with property to delete\nStorageMetadata metadata = new StorageMetadata.Builder()\n .setContentType(null)\n .build();\n\n// Delete the metadata property\nforestRef.updateMetadata(metadata)\n .addOnSuccessListener(new OnSuccessListener\u003cStorageMetadata\u003e() {\n @Override\n public void onSuccess(StorageMetadata storageMetadata) {\n // metadata.contentType should be null\n }\n })\n .addOnFailureListener(new OnFailureListener() {\n @Override\n public void onFailure(@NonNull Exception exception) {\n // Uh-oh, an error occurred!\n }\n });https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/storage/app/src/main/java/com/google/firebase/referencecode/storage/StorageActivity.java#L480-L498\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/android/handle-errors)\nsection of the docs.\n\nCustom Metadata\n\nYou can specify custom metadata using the `setCustomMetadata()` method in the\n`StorageMetadata.Builder` class. \n\nKotlin \n\n```kotlin\nval metadata = storageMetadata {\n setCustomMetadata(\"location\", \"Yosemite, CA, USA\")\n setCustomMetadata(\"activity\", \"Hiking\")\n}https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/storage/app/src/main/java/com/google/firebase/referencecode/storage/kotlin/StorageActivity.kt#L390-L393\n```\n\nJava \n\n```java\nStorageMetadata metadata = new StorageMetadata.Builder()\n .setCustomMetadata(\"location\", \"Yosemite, CA, USA\")\n .setCustomMetadata(\"activity\", \"Hiking\")\n .build();https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/storage/app/src/main/java/com/google/firebase/referencecode/storage/StorageActivity.java#L504-L507\n```\n\nYou can store app-specific data for each file in custom metadata, but we highly\nrecommend using a database (such as the\n[Firebase Realtime Database](/docs/database/android/start))\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 Getter | Type | Setter Exists |\n|-------------------------|---------------|---------------|\n| `getBucket` | `String` | NO |\n| `getGeneration` | `String` | NO |\n| `getMetadataGeneration` | `String` | NO |\n| `getPath` | `String` | NO |\n| `getName` | `String` | NO |\n| `getSizeBytes` | `long` | NO |\n| `getCreationTimeMillis` | `long` | NO |\n| `getUpdatedTimeMillis` | `long` | NO |\n| `getMd5Hash` | `String` | NO |\n| `getCacheControl` | `String` | YES |\n| `getContentDisposition` | `String` | YES |\n| `getContentEncoding` | `String` | YES |\n| `getContentLanguage` | `String` | YES |\n| `getContentType` | `String` | YES |\n| `getCustomMetadata` | `String` | YES |\n| `getCustomMetadataKeys` | `Set\u003cString\u003e` | NO |\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/android/delete-files)\nfrom Cloud Storage."]]