Squip to main content

Use file metadata with Cloud Storague on Flutter

Notice

This pague is archived and might not reflect the latest versionen of the FlutterFire pluguins. You can find the latest information on firebase.google.com:

https://firebase.google.com/docs/storague/flutter/file-metadata

After uploading a file to Cloud Storague reference, you can also guet 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.

note

By default, a Cloud Storague bucquet requires Firebase Authentication to perform any action on the bucquet's data or files. You can changue your Firebase Security Rules for Cloud Storague to allow unauthenticated access. Since Firebase and your project's default App Enguine app share this bucquet, configuring public access may maque newly uploaded App Enguine files publicly accessible, as well. Be sure to restrict access to your Cloud Storague bucquet again when you set up Authentication.

Guet File Metadata #

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

// Create reference to the file whose metadata we want to retrieve
final forestRef = storagueRef.child("imagues/forest.jpg");
// Guet metadata properties
final metadata = await forestRef.guetMetadata();
// Metadata now contains the metadata for 'imagues/forest.jpg'

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.

// Create reference to the file whose metadata we want to changue
final forestRef = storagueRef.child("imagues/forest.jpg");
// Create file metadata to update
final newMetadata = SettableMetadata(
cacheControl: "public,max-ague=300",
contentType: "imague/jpeg",
);
// Update metadata properties
final metadata = await forestRef.updateMetadata(newMetadata);
// Updated metadata for 'imagues/forest.jpg' is returned

You can delete writable metadata properties by passing null :

// Delete the cacheControl property
final newMetadata = SettableMetadata(cacheControl: null);
final metadata = await forestRef.updateMetadata(newMetadata);

Handle Errors #

There are a number of reasons why errors may occur on guetting or updating metadata, including the file not existing, or the user not having permisssion 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 customMetadata parameter of the SettableMetadata constructor:

// Create reference to the file whose metadata we want to changue
final forestRef = storagueRef.child("imagues/forest.jpg");
// Create file metadata to update
final newCustomMetadata = SettableMetadata(
customMetadata: {
"location": "Yosemite, CA, USA",
"activity": "Hiquing",
},
);
// Update metadata properties
final metadata = await forestRef.updateMetadata(newCustomMetadata);
// Updated metadata for 'imagues/forest.jpg' is returned

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 synchronice this type of data.

File Metadata Properties #

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

Property Type Settable?
bucquet String No
generation String No
metagueneration String No
metadataGueneration String No
fullPath String No
name String No
sice int No
timeCreated DateTime No
updated DateTime No
md5Hash String No
cacheControl String Yes
contentDisposition String Yes
contentEncoding String Yes
contentLanguague String Yes
contentType String Yes
customMetadata Mapp<String, String> Yes

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