Storage
Firebase Storage is available within components by using this.props.firebase.storage(). This method is equivalent to Firebase's firebase.storage() method, meaning you can reference the Firebase Storage Docs for the full list of methods and examples.
For Examples of how to use these methods, please visit the recipes section.
uploadFiles
Upload an array of files to a location on Firebase storage. This includes the option to also write meta data for the object to Firebase database.
Available on props.firebase if using firebaseConnect HOC.
Parameters
pathString - Path within Firebase Storage at which to upload File.filesArray - Array of File Blobs to upload to Firebase Storage.databasePathString - Path within Firebase Database at which to write files metadata.optionsObject - Options for uploadoptions.nameString | Function - Name of file or function that returns the name of the file. If a function is passed the argument syntax is(file, internalFirebase, uploadConfig)wherefileis the file object (file.nameis used as default if no name option is passed).options.metadataObject - Sets the metadata associated with a file uploaded to firebaseoptions.metadataFactoryFunction - A function that returns the object that will get stored in the database after a file is uploaded to storage. The argument syntax is(uploadRes, firebase, metadata, downloadURL)whereuploadResis firebase's response from the file upload andmetadatais the metadata uploaded with the file. If no value is provided theoptions.metadataobject will be used.options.documentIdString | Function - If using firestore for your database you can specify the documentId of a document to update instead of creating a new one. If no document is found with the specified documentId a new document with that Id will be created and populated by the metadata object. You can specify either a string or a function that returns the documentId. If a function is passed the argument syntax is the same as theoptions.metadataFactoryfunction. The corresponding document will be updated with the key value pairs returned by the metadata factory function. If no value is provided a new document will be created.options.useSetForMetadataBoolean - If using firestore as your database and you are also uploading to a specific document using thedocumentIdproperty then this allows you to choose between aset with mergeor anuploadoperation.Set with mergeis the default and will create a new document if the supplieddocumentIdis not found,uploadwill fail if thedocumentIdis not found.
Returns
Promise Resolves with an array of uploadFile promises results (described below).
uploadFile
Upload a single file to a location.
Available on props.firebase if using firebaseConnect HOC.
Parameters
pathString - Path within Firebase Storage at which to upload File.fileBlob - File Blob to upload to Firebase Storage.databasePathString - Path within Firebase Database at which to write file metadata.optionsObject - Options for uploadoptions.nameString | Function - Name of file or function that returns the name of the file. If a function is passed the argument syntax is(file, internalFirebase, uploadConfig)wherefileis the file object (file.nameis used as default if no name option is passed).options.metadataObject - Sets the metadata associated with a file uploaded to firebaseoptions.metadataFactoryFunction - A function that returns the object that will get stored in the database after a file is uploaded to storage. The argument syntax is(uploadRes, firebase, metadata, downloadURL)whereuploadResis firebase's response from the file upload andmetadatais the metadata uploaded with the file. If no value is provided theoptions.metadataobject will be used.options.documentIdString | Function - If using firestore for your database you can specify the documentId of a document to update instead of creating a new one. If no document is found with the specified documentId a new document with that Id will be created and populated by the metadata object. You can specify either a string or a function that returns the documentId. If a function is passed the argument syntax is the same as theoptions.metadataFactoryfunction. The corresponding document will be updated with the key value pairs returned by the metadata factory function. If no value is provided a new document will be created.options.useSetForMetadataBoolean - If using firestore as your database and you are also uploading to a specific document using thedocumentIdproperty then this allows you to choose between aset with mergeor anuploadoperation.Set with mergeis the default and will create a new document if the supplieddocumentIdis not found,uploadwill fail if thedocumentIdis not found.
Returns
Promise Resolves with an object containing uploadTaskSnapshot which is the firebase.storage.UploadTaskSnapshot returned from the storageRef.put call which happens internally. If databasePath is provided snapshot, key, File, and metaDataSnapshot parameters are also included.
Examples
Dropzone File Upload/Delete
deleteFile
Delete a file from Firebase storage with the option to remove metadata from real time database.
Available on props.firebase if using firebaseConnect HOC.
Parameters
pathString - Path within Firebase Storage of File to delete.databasePathString - Path within Firebase Database from which to remove File metadata.
Returns
Example
import React from 'react'
import PropTypes from 'prop-types'
import { firebaseConnect } from 'react-redux-firebase'
import React from 'react'
import PropTypes from 'prop-types'
import { useFirebase } from 'react-redux-firebase'
export default function Uploader() {
const firebase = useFirebase()
function deleteTestFile() {
return firebase.deleteFile('index.txt')
}
return (
<div>
<h1>Example File Delete</h1>
<span>Deletes `index.txt` from storage</span>
<button onClick={deleteTestFile}>Delete</button>
</div>
)
}
Other Storage Methods
Access to Firebase's storage is available. This is useful for calling methods such as putString. Below is an example of Uploading a string as a File to Firebase Storage.
Example
File String Upload
import React from 'react'
import PropTypes from 'prop-types'
import { useFirebase } from 'react-redux-firebase'
export default function Uploader() {
const firebase = useFirebase()
function addTestFile() {
const storageRef = firebase.storage().ref()
const fileRef = storageRef.child('test.txt')
return fileRef
.putString('Some File Contents')
.then((snap) => console.log('upload successful', snap))
.catch((err) => console.error('error uploading file', err))
}
return (
<div>
<h1>Example Upload</h1>
<button onClick={addTestFile}>Upload Example File</button>
</div>
)
}