Upload files with Cloud Storage on Flutter
Notice
This page is archived and might not reflect the latest version of the FlutterFire plugins. You can find the latest information on firebase.google.com:
https://firebase.google.com/docs/storage/flutter/upload-files
Cloud Storage for Firebase allows you to quickly and easily upload files to a Cloud Storage bucket provided and managed by Firebase.
note
By default, a Cloud Storage bucket requires Firebase Authentication to perform any action on the bucket's data or files. You can change your Firebase Security Rules for Cloud Storage to allow unauthenticated access. Since Firebase and your project's default App Engine app share this bucket, configuring public access may make newly uploaded App Engine files publicly accessible, as well. Be sure to restrict access to your Cloud Storage bucket again when you set up Authentication.
#
Upload FilesTo upload a file to Cloud Storage, you first create a reference to the full path of the file, including the file name.
Once you've created an appropriate reference, you then call the
putFile()
, putString()
, or putData()
method to upload the file
to Cloud Storage.
You cannot upload data with a reference to the root of your Cloud Storage bucket. Your reference must point to a child URL.
#
Upload from a fileTo upload a file, you must first get the absolute path to its on-device
location. For example, if a file exists within the application's documents
directory, use the official path_provider
package to generate a file path and pass it to putFile()
:
#
Upload from a StringYou can upload data as a raw, base64
, base64url
, or data_url
encoded
string using the putString()
method. For example, to upload a text string
encoded as a Data URL:
#
Uploading raw dataYou can upload lower-level typed data in the form of a Uint8List
for those cases where uploading a string or File
is not practical. In this
case, call the putData()
method with your data:
#
Get a download URLAfter uploading a file, you can get a URL to download the file by calling
the getDownloadUrl()
method on the Reference
:
#
Add File MetadataYou can also include metadata when you upload files.
This metadata contains typical file metadata properties such as contentType
(commonly referred to as MIME type). The putFile()
method
automatically infers the MIME type from the File
extension, but you can
override the auto-detected type by specifying contentType
in the metadata. If
you do not provide a contentType
and Cloud Storage cannot infer a
default from the file extension, Cloud Storage uses
application/octet-stream
. See Use File Metadata.
#
Manage UploadsIn addition to starting uploads, you can pause, resume, and cancel uploads using
the pause()
, resume()
, and cancel()
methods. Pause and resume events
raise pause
and progress
state changes respectively. Canceling an
upload causes the upload to fail with an error indicating that the
upload was canceled.
#
Monitor Upload ProgressYou can listen to a task's event stream to handle success, failure, progress, or pauses in your upload task:
Event Type | Typical Usage |
---|---|
TaskState.running | Emitted periodically as data is transferred and can be used to populate an upload/download indicator. |
TaskState.paused | Emitted any time the task is paused. |
TaskState.success | Emitted when the task has successfully completed. |
TaskState.canceled | Emitted any time the task is canceled. |
TaskState.error | Emitted when the upload has failed. This can happen due to network timeouts, authorization failures, or if you cancel the task. |
#
Error HandlingThere 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. You can find more information about errors in the Handle Errors section of the docs.
#
Full ExampleA full example of an upload with progress monitoring and error handling is shown below:
Now that you've uploaded files, let's learn how to download them from Cloud Storage.