|
| 1 | +--- |
| 2 | +title: Download files with Cloud Storage on Flutter |
| 3 | +sidebar_label: Download files |
| 4 | +--- |
| 5 | + |
| 6 | +Cloud Storage for Firebase allows you to quickly and easily download |
| 7 | +files from a [Cloud Storage](//cloud.google.com/storage) |
| 8 | +bucket provided and managed by Firebase. |
| 9 | + |
| 10 | +:::note |
| 11 | +By default, a Cloud Storage bucket requires Firebase Authentication to |
| 12 | +perform any action on the bucket's data or files. You can |
| 13 | +[change your Firebase Security Rules for Cloud Storage](https://firebase.google.com/docs/storage/security/rules-conditions#public) |
| 14 | +to allow unauthenticated access. Since Firebase and your project's default |
| 15 | +App Engine app share this bucket, configuring public access may make newly |
| 16 | +uploaded App Engine files publicly accessible, as well. Be sure to restrict |
| 17 | +access to your Cloud Storage bucket again when you set up Authentication. |
| 18 | +::: |
| 19 | + |
| 20 | + |
| 21 | +## Create a Reference |
| 22 | + |
| 23 | +To download a file, first [create a Cloud Storage reference](create-reference) |
| 24 | +to the file you want to download. |
| 25 | + |
| 26 | +You can create a reference by appending child paths to the root of your |
| 27 | +Cloud Storage bucket, or you can create a reference from an existing |
| 28 | +`gs://` or `https://` URL referencing an object in Cloud Storage. |
| 29 | + |
| 30 | +```dart |
| 31 | +// Create a storage reference from our app |
| 32 | +final storageRef = FirebaseStorage.instance.ref(); |
| 33 | +
|
| 34 | +// Create a reference with an initial file path and name |
| 35 | +final pathReference = storageRef.child("images/stars.jpg"); |
| 36 | +
|
| 37 | +// Create a reference to a file from a Google Cloud Storage URI |
| 38 | +final gsReference = |
| 39 | + FirebaseStorage.instance.refFromURL("gs://YOUR_BUCKET/images/stars.jpg"); |
| 40 | +
|
| 41 | +// Create a reference from an HTTPS URL |
| 42 | +// Note that in the URL, characters are URL escaped! |
| 43 | +final httpsReference = FirebaseStorage.instance.refFromURL( |
| 44 | + "https://firebasestorage.googleapis.com/b/YOUR_BUCKET/o/images%20stars.jpg"); |
| 45 | +``` |
| 46 | + |
| 47 | + |
| 48 | +## Download Files |
| 49 | + |
| 50 | +Once you have a reference, you can download files from Cloud Storage |
| 51 | +by calling the `getData()` or `getStream()`. If you prefer to download the file |
| 52 | +with another library, you can get a download URL with `getDownloadUrl()`. |
| 53 | + |
| 54 | +### Download in memory |
| 55 | + |
| 56 | +Download the file to a `UInt8List` with the `getData()` method. This is the |
| 57 | +easiest way to download a file, but it must load the entire contents of |
| 58 | +your file into memory. If you request a file larger than your app's available |
| 59 | +memory, your app will crash. To protect against memory issues, `getData()` |
| 60 | +takes a maximum amount of bytes to download. Set the maximum size to something |
| 61 | +you know your app can handle, or use another download method. |
| 62 | + |
| 63 | +```dart |
| 64 | +final islandRef = storageRef.child("images/island.jpg"); |
| 65 | +
|
| 66 | +try { |
| 67 | + const oneMegabyte = 1024 * 1024; |
| 68 | + final Uint8List? data = await islandRef.getData(oneMegabyte); |
| 69 | + // Data for "images/island.jpg" is returned, use this as needed. |
| 70 | +} on FirebaseException catch (e) { |
| 71 | + // Handle any errors. |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### Download to a local file |
| 76 | + |
| 77 | +The `writeToFile()` method downloads a file directly to a local device. Use this if |
| 78 | +your users want to have access to the file while offline or to share the file in a |
| 79 | +different app. `writeToFile()` returns a `DownloadTask` which you can use to manage |
| 80 | +your download and monitor the status of the download. |
| 81 | + |
| 82 | +```dart |
| 83 | +final islandRef = storageRef.child("images/island.jpg"); |
| 84 | +
|
| 85 | +final appDocDir = await getApplicationDocumentsDirectory(); |
| 86 | +final filePath = "${appDocDir.absolute}/images/island.jpg"; |
| 87 | +final file = File(filePath); |
| 88 | +
|
| 89 | +final downloadTask = islandRef.writeToFile(file); |
| 90 | +downloadTask.snapshotEvents.listen((taskSnapshot) { |
| 91 | + switch (taskSnapshot.state) { |
| 92 | + case TaskState.running: |
| 93 | + // TODO: Handle this case. |
| 94 | + break; |
| 95 | + case TaskState.paused: |
| 96 | + // TODO: Handle this case. |
| 97 | + break; |
| 98 | + case TaskState.success: |
| 99 | + // TODO: Handle this case. |
| 100 | + break; |
| 101 | + case TaskState.canceled: |
| 102 | + // TODO: Handle this case. |
| 103 | + break; |
| 104 | + case TaskState.error: |
| 105 | + // TODO: Handle this case. |
| 106 | + break; |
| 107 | + } |
| 108 | +}); |
| 109 | +``` |
| 110 | + |
| 111 | +## Download Data via URL |
| 112 | + |
| 113 | +If you already have download infrastructure based around URLs, or just want |
| 114 | +a URL to share, you can get the download URL for a file by calling the |
| 115 | +`getDownloadURL()` method on a Cloud Storage reference. |
| 116 | + |
| 117 | +```dart |
| 118 | +final imageUrl = |
| 119 | + await storageRef.child("users/me/profile.png").getDownloadURL(); |
| 120 | +``` |
| 121 | + |
| 122 | +## Handle Errors |
| 123 | + |
| 124 | +There are a number of reasons why errors may occur on download, including the |
| 125 | +file not existing, or the user not having permission to access the desired file. |
| 126 | +More information on errors can be found in the [Handle Errors](handle-errors) |
| 127 | +section of the docs. |
| 128 | + |
| 129 | +## Full Example |
| 130 | + |
| 131 | +A full example of a download with error handling is shown below: |
| 132 | + |
| 133 | +```dart |
| 134 | +final islandRef = storageRef.child("images/island.jpg"); |
| 135 | +
|
| 136 | +final appDocDir = await getApplicationDocumentsDirectory(); |
| 137 | +final filePath = "${appDocDir.absolute}/images/island.jpg"; |
| 138 | +final file = File(filePath); |
| 139 | +
|
| 140 | +final downloadTask = islandRef.writeToFile(file); |
| 141 | +downloadTask.snapshotEvents.listen((taskSnapshot) { |
| 142 | + switch (taskSnapshot.state) { |
| 143 | + case TaskState.running: |
| 144 | + // TODO: Handle this case. |
| 145 | + break; |
| 146 | + case TaskState.paused: |
| 147 | + // TODO: Handle this case. |
| 148 | + break; |
| 149 | + case TaskState.success: |
| 150 | + // TODO: Handle this case. |
| 151 | + break; |
| 152 | + case TaskState.canceled: |
| 153 | + // TODO: Handle this case. |
| 154 | + break; |
| 155 | + case TaskState.error: |
| 156 | + // TODO: Handle this case. |
| 157 | + break; |
| 158 | + } |
| 159 | +}); |
| 160 | +``` |
| 161 | + |
| 162 | +You can also [get and update metadata](file-metadata) for files that are stored |
| 163 | +in Cloud Storage. |
0 commit comments