After uploading a file to a Cloud Storage reference, you can also get
and update the file metadata, for example to update the ContentType property.
Files can also store custom key-value pairs with additional file metadata.
Get file metadata
File metadata contains common properties such as Name, SizeBytes, and
ContentType (often referred to as MIME type) in addition to some
less common ones like ContentDisposition and CreationTimeMillis. This
metadata can be retrieved from a Cloud Storage reference using the
GetMetadataAsync method.
// Create reference to the file whose metadata we want to retrieve StorageReference forestRef = storageRef.Child("images/forest.jpg"); // Get metadata properties forestRef.GetMetadataAsync().ContinueWithOnMainThread(task => { if (!task.IsFaulted && !task.IsCanceled) { StorageMetadata meta = task.Result; // do stuff with meta } });
Update file metadata
You can update file metadata at any time after the file upload completes by
using the UpdateMetadataAsync method which takes a MetadataChange object.
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.
// Create reference to the file whose metadata we want to change StorageReference forestRef = storageRef.Child("images/forest.jpg"); // Create file metadata to update var newMetadata = new MetadataChange(); newMetadata.CacheControl = "public,max-age=300"; newMetadata.ContentType = "image/jpeg"; // Update metadata properties forestRef.UpdateMetadataAsync(newMetadata).ContinueWithOnMainThread(task => { if (!task.IsFaulted && !task.IsCanceled) { // access the updated meta data StorageMetadata meta = task.Result; } });
You can delete writable metadata properties by passing the empty string:
// Create file metadata to update var newMetadata = new MetadataChange(); newMetadata.ContentType = ""; // Update metadata properties forestRef.UpdateMetadataAsync(newMetadata).ContinueWithOnMainThread(task => { if (!task.IsFaulted && !task.IsCanceled) { StorageMetadata meta = task.Result; // meta.ContentType should be an empty string now } });
Error handling
There are a number of reasons why errors might occur on getting or updating metadata, including the file not existing, or the user not having permission to access the intended file. For more information about errors, see Handle errors.
Custom metadata
You can specify custom metadata as a Dictionary<string, string>.
var newMetadata = new MetadataChange { CustomMetadata = new Dictionary<string, string> { {"location", "Yosemite, CA, USA"}, {"activity", "Hiking"} } }; // UpdateMetadataAsync
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 | Type | Modifiable in MetadataChange |
|---|---|---|
Bucket |
string |
NO |
Generation |
string |
NO |
MetadataGeneration |
string |
NO |
Path |
string |
NO |
Name |
string |
NO |
SizeBytes |
long |
NO |
CreationTimeMillis |
long |
NO |
UpdatedTimeMillis |
long |
NO |
CacheControl |
string |
YES |
ContentDisposition |
string |
YES |
ContentEncoding |
string |
YES |
ContentLanguage |
string |
YES |
ContentType |
string |
YES |
DownloadUrl |
Uri |
NO |
DownloadUrls |
IList<Uri> |
NO |
CustomMetadataKeys |
IEnumerable<string> |
YES |
Next steps
Uploading, downloading, and updating files is important, but so is being able to remove them. Next, learn how to delete files from Cloud Storage.