0
0
Firebasecloud~5 mins

File metadata in Firebase - Commands & Configuration

Choose your learning style9 modes available
Introduction
File metadata stores extra information about files in Firebase Storage. It helps you keep track of details like file type, size, and custom tags without changing the file itself.
When you want to know the type of a file before downloading it to show the right icon.
When you need to store who uploaded a file or when it was uploaded.
When you want to add custom tags to files for easier searching later.
When you want to control file caching behavior by setting cache control headers.
When you want to update file information without re-uploading the whole file.
Commands
Uploads a file named photo.jpg to Firebase Storage and sets metadata for content type and a custom uploader tag.
Terminal
firebase storage:upload ./images/photo.jpg --metadata contentType=image/jpeg,customMetadata.uploader=alice
Expected OutputExpected
✔ Uploaded ./images/photo.jpg to gs://your-project.appspot.com/images/photo.jpg Metadata set: contentType=image/jpeg, customMetadata.uploader=alice
--metadata - Sets metadata properties like content type and custom tags during upload
Retrieves and shows the metadata of the uploaded photo.jpg file to verify the stored information.
Terminal
firebase storage:metadata images/photo.jpg
Expected OutputExpected
File: images/photo.jpg Content-Type: image/jpeg Custom Metadata: uploader: alice Size: 204800 bytes Updated: 2024-06-01T12:00:00Z
Updates the metadata of photo.jpg to add a description without changing the file content.
Terminal
firebase storage:metadata:update images/photo.jpg --metadata customMetadata.description='Vacation photo'
Expected OutputExpected
✔ Metadata updated for images/photo.jpg Custom Metadata: uploader: alice description: Vacation photo
--metadata - Updates or adds metadata properties without re-uploading the file
Checks the updated metadata to confirm the new description tag was added successfully.
Terminal
firebase storage:metadata images/photo.jpg
Expected OutputExpected
File: images/photo.jpg Content-Type: image/jpeg Custom Metadata: uploader: alice description: Vacation photo Size: 204800 bytes Updated: 2024-06-01T12:05:00Z
Key Concept

If you remember nothing else from this pattern, remember: file metadata lets you store and update extra information about files without changing the files themselves.

Common Mistakes
Trying to update metadata by re-uploading the whole file instead of using metadata update commands.
This wastes bandwidth and time because the file content does not need to change to update metadata.
Use the metadata update command to change metadata without re-uploading the file.
Not setting the correct content type metadata when uploading files.
Without the right content type, browsers or apps may not handle the file correctly, causing display or download issues.
Always set the contentType metadata to match the file type during upload.
Using unsupported characters or formats in custom metadata keys or values.
Firebase Storage requires metadata keys and values to follow specific rules; invalid formats cause errors.
Use only letters, numbers, hyphens, and underscores in keys and simple string values.
Summary
Upload files with metadata to store extra information like content type and custom tags.
Use metadata commands to view and update file metadata without re-uploading files.
Proper metadata helps apps handle files correctly and adds useful searchable information.