After uploading a file to a Cloud Storage reference, you can also get
and update the file metadata, for example to update the content_type
property. 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
content_type (often referred to as MIME type) in addition to some less common
ones like content_disposition and time_created. This metadata can be
retrieved from a Cloud Storage reference using the GetMetadata
method.
// Create reference to the file whose metadata you want to retrieve StorageReference forest_ref = storage_ref.Child("images/forest.jpg"); // Get metadata properties Futurefuture = forest_ref.GetMetadata(); // Wait for Future to complete... if (future.Error() != firebase::storage::kErrorNone) { // Uh-oh, an error occurred! } else { // You can now retrieve the metadata for 'images/forest.jpg' Metadata* metadata = future.Result(); }
Update file metadata
You can update file metadata at any time after the file upload completes by
using the UpdateMetadata method. For more information about what properties
can be updated, see the full list. Only the
properties specified in the metadata are updated; all others are left
unmodified.
// Create reference to the file whose metadata you want to change firebase::storage::StorageReference forest_ref = storage_ref.child("images/forest.jpg"); // Create file metadata to update Metadata new_metadata; newMetadata.set_cache_control("public,max-age=300"); newMetadata.set_content_type("image/jpeg"); // Update metadata properties Futurefuture = forest_ref.UpdateMetadata(new_metadata); // Wait for Future to complete... if (future.Error() != firebase::storage::kErrorNone) { // Uh-oh, an error occurred! } else { // You can now retrieve the updated metadata for 'images/forest.jpg' Metadata* metadata = future.Result(); }
You can delete writable metadata properties by passing the empty string:
// Create file metadata with property to delete StorageMetadata new_metadata; new_metadata.set_content_type(""); // Delete the metadata property Futurefuture = forest_ref.UpdateMetadata(new_metadata); // Wait for Future to complete... if (future.Error() != 0) { // Uh-oh, an error occurred! } else { // metadata.content_type() should be an empty string Metadata* metadata = future.Result(); }
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 an std::map containing std::string
properties.
std::map<std::string, std::string>* custom_metadata = metadata.custom_metadata(); custom_metadata->insert(std::make_pair("location", "Yosemite, CA, USA"); custom_metadata->insert(std::make_pair("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 |
const char* | NO |
generation |
const char* | NO |
metageneration |
const char* | NO |
full_path |
const char* | NO |
name |
const char* | NO |
size |
int64_t | NO |
time_created |
int64_t | NO |
updated |
int64_t | NO |
cache_control |
const char* | YES |
content_disposition |
const char* | YES |
content_encoding |
const char* | YES |
content_language |
const char* | YES |
content_type |
const char* | YES |
download_urls |
std::vector<std::string> | NO |
custom_metadata |
std::map<std::string, std::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.