Use File Metadata with Cloud Storage for Unity

After uploading a file to Cloud Storage reference, you can also get and update the file metadata, for example to 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 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
Firebase.Storage.StorageReference forest_ref = storage_ref.Child("images/forest.jpg");

// Get metadata properties
forest_ref.GetMetadataAsync().ContinueWith(task => {
        if (!task.IsFaulted && !task.IsCancelled) {
            Firebase.Storage.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
Firebase.Storage.StorageReference forest_ref = storage_ref.child("images/forest.jpg");

// Create file metadata to update
var new_metadata = new Firebase.Storage.MetadataChange();
new_metadata.CacheControl = "public,max-age=300";
new_metadata.ContentType = "image/jpeg";

// Update metadata properties
forest_ref.UpdateMetadata(new_metadata).ContinueWith(task => {
        if (!task.IsFaulted && !task.IsCancelled) {
            // access the updated meta data
            Firebase.Storage.StorageMetadata meta = task.Result;
        }
    });

You can delete writable metadata properties by passing the empty string:

// Create file metadata to update
var new_metadata = new Firebase.Storage.MetadataChange();
new_metadata.ContentType = "";

// Update metadata properties
forest_ref.UpdateMetadataAsync(new_metadata).ContinueWith(task => {
        if (!task.IsFaulted && !task.IsCancelled) {
            Firebase.Storage.StorageMetadata meta = task.Result;
            // meta.ContentType should be an empty string now
        }
    });

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 as a Dictionary<string, string>.

var new_metadata = new Firebase.Storage.MetadataChange {
  CustomMetadata = new Dictionary {
     {"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 Modifyable 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 NO
CustomMetadataKeys IEnumerable YES

Next Steps

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.

Send feedback about...

Need help? Visit our support page.