Firebase Storage allows developers to quickly and easily upload files to a Google Cloud Storage bucket provided and managed by Firebase.
Since the default Google App Engine app and Firebase share this bucket, configuring public access may make newly uploaded App Engine files publicly accessible as well. Be sure to restrict access to your Storage bucket again when you set up authentication.
Upload Files
To upload a file to Firebase Storage, you first create a reference to the full path of the file, including the file name.
// Create a root reference
var storageRef = firebase.storage().ref();
// Create a reference to 'mountains.jpg'
var mountainsRef = storageRef.child('mountains.jpg');
// Create a reference to 'images/mountains.jpg'
var mountainImagesRef = storageRef.child('images/mountains.jpg');
// While the file names are the same, the references point to different files
mountainsRef.name === mountainImagesRef.name // true
mountainsRef.fullPath === mountainImagesRef.fullPath // false
Upload from a Blob or File
Once you've created an appropriate reference, you then call the put() method.
put() takes files via the JavaScript
File and
Blob APIs and uploads
them to Firebase Storage.
var file = ... // use the Blob or File API
ref.put(file).then(function(snapshot) {
console.log('Uploaded a blob or file!');
});
Upload from a Byte Array
In addition to the File and Blob types, put() can also upload a
Uint8Array to Firebase Storage.
// Uint8Array
var bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]);
ref.put(bytes).then(function(snapshot) {
console.log('Uploaded an array!');
});
Upload from a String
If a Blob, File, or Uint8Array isn't available, you can use the
putString() method to upload a raw, base64, or base64url encoded
string to Firebase Storage.
// Raw string is the default if no format is provided
var message = 'This is my message.';
ref.putString(message).then(function(snapshot) {
console.log('Uploaded a raw string!');
});
// Base64 formatted string
var message = '5b6p5Y+344GX44G+44GX44Gf77yB44GK44KB44Gn44Go44GG77yB';
ref.putString(message, 'base64').then(function(snapshot) {
console.log('Uploaded a base64 string!');
});
// Base64url formatted string
var message = '5b6p5Y-344GX44G-44GX44Gf77yB44GK44KB44Gn44Go44GG77yB';
ref.putString(message, 'base64url').then(function(snapshot) {
console.log('Uploaded a base64url string!');
});
put() and putString() both return an UploadTask which you can use as a
promise, or use to manage and monitor the status of the upload.
Since the reference defines the full path to the file, make sure that you are uploading to a non-empty path.
Add File Metadata
When uploading a file, you can also specify metadata for that file.
This metadata contains typical file metadata properties such as name, size,
and contentType (commonly referred to as MIME type). Firebase Storage
automatically infers the content type from the file extension where the file is
stored on disk, but if you specify a contentType in the metadata it will
override the auto-detected type. If no contentType metadata is specified and
the file doesn't have a file extension, Firebase Storage defaults to the
type application/octet-stream. More information on file metadata can be found
in the Use File Metadata
section.
// Create file metadata including the content type
var metadata = {
contentType: 'image/jpeg',
};
// Upload the file and metadata
var uploadTask = storageRef.child('images/mountains.jpg').put(file, metadata);
Manage Uploads
In addition to starting uploads, you can pause, resume, and cancel uploads using
the pause(), resume(), and cancel() methods. Calling pause() or
resume() will raise pause or running state changes. Calling the
cancel() method results in the upload failing and returning an error
indicating that the upload was canceled.
// Upload the file and metadata
var uploadTask = storageRef.child('images/mountains.jpg').put(file);
// Pause the upload
uploadTask.pause();
// Resume the upload
uploadTask.resume();
// Cancel the upload
uploadTask.cancel();
Monitor Upload Progress
While uploading, the upload task may raise progress events in
the state_changed observer, such as:
| Event Type | Typical Usage |
|---|---|
running |
This event fires when the task starts or resumes uploading, and is often
used in conjunction with the pause event. |
progress |
This event fires any time data is uploaded to Firebase Storage, and can be used to populate an upload progress indicator. |
pause |
This event fires any time the upload is paused, and is often used in
conjunction with the running event. |
When an event occurs, a TaskSnapshot object is passed back. This snapshot
is an immutable view of the task at the time the event occurred.
This object contains the following properties:
| Property | Type | Description |
|---|---|---|
bytesTransferred |
Number |
The total number of bytes that have been transferred when this snapshot was taken. |
totalBytes |
Number |
The total number of bytes expected to be uploaded. |
state |
firebase.storage.TaskState |
Current state of the upload. |
metadata |
firebaseStorage.Metadata |
Before upload completes, the metadata sent to the server. After upload completes, the metadata the server sent back. |
task |
firebaseStorage.UploadTask |
The task this is a snapshot of, which can be used to `pause`, `resume`, or `cancel` the task. |
ref |
firebaseStorage.Reference |
The reference this task came from. |
These changes in state, combined with the properties of the TaskSnapshot
provide a simple yet powerful way to monitor upload events.
var uploadTask = storageRef.child('images/rivers.jpg').put(file);
// Register three observers:
// 1. 'state_changed' observer, called any time the state changes
// 2. Error observer, called on failure
// 3. Completion observer, called on successful completion
uploadTask.on('state_changed', function(snapshot){
// Observe state change events such as progress, pause, and resume
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED: // or 'paused'
console.log('Upload is paused');
break;
case firebase.storage.TaskState.RUNNING: // or 'running'
console.log('Upload is running');
break;
}
}, function(error) {
// Handle unsuccessful uploads
}, function() {
// Handle successful uploads on complete
// For instance, get the download URL: https://firebasestorage.googleapis.com/...
var downloadURL = uploadTask.snapshot.downloadURL;
});
Error Handling
There are a number of reasons why errors may occur on upload, including the local file not existing, or the user not having permission to upload the desired file. More information on errors can be found in the Handle Errors section of the docs.
Full Example
A full example of an upload with progress monitoring and error handling is shown below:
// File or Blob named mountains.jpg
var file = ...
// Create the file metadata
var metadata = {
contentType: 'image/jpeg'
};
// Upload file and metadata to the object 'images/mountains.jpg'
var uploadTask = storageRef.child('images/' + file.name).put(file, metadata);
// Listen for state changes, errors, and completion of the upload.
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
function(snapshot) {
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.log('Upload is ' + progress + '% done');
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED: // or 'paused'
console.log('Upload is paused');
break;
case firebase.storage.TaskState.RUNNING: // or 'running'
console.log('Upload is running');
break;
}
}, function(error) {
switch (error.code) {
case 'storage/unauthorized':
// User doesn't have permission to access the object
break;
case 'storage/canceled':
// User canceled the upload
break;
...
case 'storage/unknown':
// Unknown error occurred, inspect error.serverResponse
break;
}
}, function() {
// Upload completed successfully, now we can get the download URL
var downloadURL = uploadTask.snapshot.downloadURL;
});
Now that you've uploaded files, let's learn how to download them from Firebase Storage.

