Use file metadata with Cloud Storage on Android

After uploading a file to Cloud Storage reference, you can also get and update the file metadata, for example to view or update the content type. Files can also store custom key/value pairs with additional file metadata.

Get File Metadata

File metadata contains common properties such as name, size, and contentType (often referred to as MIME type) in addition to some less common ones like contentDisposition and timeCreated. This metadata can be retrieved from a Cloud Storage reference using the getMetadata() method.

Kotlin

// Create a storage reference from our app
valstorageRef=storage.reference
// Get reference to the file
valforestRef=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
StorageReferencestorageRef=storage.getReference();
// Get reference to the file
StorageReferenceforestRef=storageRef.child("images/forest.jpg");
forestRef.getMetadata().addOnSuccessListener(newOnSuccessListener<StorageMetadata>(){
@Override
publicvoidonSuccess(StorageMetadatastorageMetadata){
// Metadata now contains the metadata for 'images/forest.jpg'
}
}).addOnFailureListener(newOnFailureListener(){
@Override
publicvoidonFailure(@NonNullExceptionexception){
// Uh-oh, an error occurred!
}
});

Update File Metadata

You can update file metadata at any time after the file upload completes by using the updateMetadata() method. Refer to the full list for more information on what properties can be updated. Only the properties specified in the metadata are updated, all others are left unmodified.

Kotlin

// Create a storage reference from our app
valstorageRef=storage.reference
// Get reference to the file
valforestRef=storageRef.child("images/forest.jpg")
// Create file metadata including the content type
valmetadata=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
StorageReferencestorageRef=storage.getReference();
// Get reference to the file
StorageReferenceforestRef=storageRef.child("images/forest.jpg");
// Create file metadata including the content type
StorageMetadatametadata=newStorageMetadata.Builder()
.setContentType("image/jpg")
.setCustomMetadata("myCustomProperty","myValue")
.build();
// Update metadata properties
forestRef.updateMetadata(metadata)
.addOnSuccessListener(newOnSuccessListener<StorageMetadata>(){
@Override
publicvoidonSuccess(StorageMetadatastorageMetadata){
// Updated metadata is in storageMetadata
}
})
.addOnFailureListener(newOnFailureListener(){
@Override
publicvoidonFailure(@NonNullExceptionexception){
// Uh-oh, an error occurred!
}
});

You can delete writable metadata properties by passing null:

Kotlin

// Create file metadata with property to delete
valmetadata=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
StorageMetadatametadata=newStorageMetadata.Builder()
.setContentType(null)
.build();
// Delete the metadata property
forestRef.updateMetadata(metadata)
.addOnSuccessListener(newOnSuccessListener<StorageMetadata>(){
@Override
publicvoidonSuccess(StorageMetadatastorageMetadata){
// metadata.contentType should be null
}
})
.addOnFailureListener(newOnFailureListener(){
@Override
publicvoidonFailure(@NonNullExceptionexception){
// Uh-oh, an error occurred!
}
});

Handle Errors

There are a number of reasons why errors may occur on getting or updating metadata, including the file not existing, or the user not having permission to access the desired file. More information on errors can be found in the Handle Errors section of the docs.

Custom Metadata

You can specify custom metadata using the setCustomMetadata() method in the StorageMetadata.Builder class.

Kotlin

valmetadata=storageMetadata{
setCustomMetadata("location","Yosemite, CA, USA")
setCustomMetadata("activity","Hiking")
}

Java

StorageMetadatametadata=newStorageMetadata.Builder()
.setCustomMetadata("location","Yosemite, CA, USA")
.setCustomMetadata("activity","Hiking")
.build();

You can store app-specific data for each file in custom metadata, but we highly recommend using a database (such as the Firebase Realtime Database) to store and synchronize this type of data.

File Metadata Properties

A full list of metadata properties on a file is available below:

Property Getter Type Setter Exists
getBucket String NO
getGeneration String NO
getMetadataGeneration String NO
getPath String NO
getName String NO
getSizeBytes long NO
getCreationTimeMillis long NO
getUpdatedTimeMillis long NO
getMd5Hash String NO
getCacheControl String YES
getContentDisposition String YES
getContentEncoding String YES
getContentLanguage String YES
getContentType String YES
getCustomMetadata String YES
getCustomMetadataKeys Set<String> NO

Uploading, downloading, and updating files is important, but so is being able to remove them. Let's learn how to delete files from Cloud Storage.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年11月06日 UTC.