After uploading a file to Firebase 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 timeCreated
. This metadata can be
retrieved from a Firebase Storage reference using
the metadataWithCompletion:
method.
Objective-C
// Create reference to the file whose metadata we want to retrieve FIRStorageReference *forestRef = [storageRef child:@"images/forest.jpg"]; // Get metadata properties [forestRef metadataWithCompletion:^(FIRStorageMetadata *metadata, NSError *error){ if (error != nil) { // Uh-oh, an error occurred! } else { // Metadata now contains the metadata for 'images/forest.jpg' } }];
Swift
// Create reference to the file whose metadata we want to retrieve var forestRef = storageRef.child("images/forest.jpg") // Get metadata properties forestRef.metadataWithCompletion { (metadata, error) -> Void in if (error != nil) { // Uh-oh, an error occurred! } else { // Metadata now contains the metadata for 'images/forest.jpg' } }
Update File Metadata
You can update file metadata at any time after the file upload completes by
using the updateMetadata:withCompletion:
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.
Objective-C
// Create reference to the file whose metadata we want to change FIRStorage *forestRef = [storageRef child:@"images/forest.jpg"]; // Create file metadata to update FIRStorageMetadata *newMetadata = [[FIRStorageMetadata alloc] init]; newMetadata.cacheControl = @"public,max-age=300"; newMetadata.contentType = @"image/jpeg"; // Update metadata properties [forestRef updateMetadata:newMetadata completion:^(FIRStorageMetadata *metadata, NSError *error){ if (error != nil) { // Uh-oh, an error occurred! } else { // Updated metadata for 'images/forest.jpg' is returned } }];
Swift
// Create reference to the file whose metadata we want to change var forestRef = storageRef.child("images/forest.jpg") // Create file metadata to update let newMetadata = FIRStorageMetadata() newMetadata.cacheControl = "public,max-age=300" newMetadata.contentType = "image/jpeg" // Update metadata properties forestRef.updateMetadata(metadata) { (metadata, error) -> Void in if (error != nil) { // Uh-oh, an error occurred! } else { // Updated metadata for 'images/forest.jpg' is returned } }
You can delete writable metadata properties by passing the empty string:
Objective-C
// Create file metadata with property to delete FIRStorageMetadata *newMetadata = [[FIRStorageMetadata alloc] init]; newMetadata.contentType = @"";// Delete the metadata property [forestRef updateMetadata:newMetadata completion:^(FIRStorageMetadata metadata, NSError error){ if (error != nil) { // Uh-oh, an error occurred! } else { // metadata.contentType should be nil } }];
Swift
// Create file metadata with property to delete let newMetadata = FIRStorageMetadata() newMetadata.contentType = ""// Delete the metadata property forestRef.updateMetadata(metadata) { (metadata, error) -> Void in if (error != nil) { // Uh-oh, an error occurred! } else { // metadata.contentType should be nil } }
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 an NSDictionary
containing NSString
properties.
Objective-C
NSDictionary *metadata = @{ @"customMetadata": @{ @"location": @"Yosemite, CA, USA", @"activity": @"Hiking" } }
Swift
var metadata = [ "customMetadata": [ "location": "Yosemite, CA, USA", "activity": "Hiking" ] ]
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 | Writable |
---|---|---|
bucket |
NSString | NO |
generation |
NSString | NO |
metageneration |
NSString | NO |
fullPath |
NSString | NO |
name |
NSString | NO |
size |
int64_t | NO |
timeCreated |
NSDate | NO |
updated |
NSDate | NO |
md5Hash |
NSString | YES |
cacheControl |
NSString | YES |
contentDisposition |
NSString | YES |
contentEncoding |
NSString | YES |
contentLanguage |
NSString | YES |
contentType |
NSString | YES |
downloadURLs |
NSArray<NSString> | NO |
customMetadata |
NSDictionary<NSString, NSString> | YES |
Uploading, downloading, and updating files is important, but so is being able to remove them. Let's learn how to delete files from Firebase Storage.