Download files
To further customize your in-app experience, you can use the getUrl
or downloadData
API from the Amplify Library for Storage.
Get or download file from a URL
With the getUrl
API, you can get a presigned URL which is valid for 900 seconds or 15 minutes by default. You can use this URL to create a download link for users to click on. The expiresAt
property is a Date
object that represents the time at which the URL will expire.
When creating a downloadable URL, you can choose to check if the file exists by setting validateObjectExistence
to
true
in S3GetUrlPluginOptions
. If the file is inaccessible or does not exist, a StorageException
is thrown.
This allows you to check if an object exists during generating the presigned URL, which you can then use to download
that object.
Future<void> getDownloadUrl() async { try { final result = await Amplify.Storage.getUrl( path: const StoragePath.fromString('public/example.txt'), ).result; safePrint('url: ${result.url}'); } on StorageException catch (e) { safePrint(e.message); }}
Download to a file
You can download a file to a local directory using Amplify.Storage.downloadFile
.
You can use the path_provider package to create a local file in the user's documents directory where you can store the downloaded data.
import 'package:amplify_flutter/amplify_flutter.dart';import 'package:path_provider/path_provider.dart';
Future<void> downloadFile() async { final documentsDir = await getApplicationDocumentsDirectory(); final filepath = '${documentsDir.path}/example.txt'; try { final result = await Amplify.Storage.downloadFile( path: const StoragePath.fromString('public/example.txt'), localFile: AWSFile.fromPath(filepath), ).result; safePrint('Downloaded file is located at: ${result.localFile.path}'); } on StorageException catch (e) { safePrint(e.message); }}
On Web, the download process will be handled by the browser. You can provide the downloaded file name by specifying the path
parameter of AWSFile.fromPath
. E.g. this instructs the browser to download the file download.txt
.
import 'package:amplify_flutter/amplify_flutter.dart';
Future<void> downloadFile() async { try { final result = await Amplify.Storage.downloadFile( path: const StoragePath.fromString('public/example.txt'), localFile: AWSFile.fromPath('download.txt'), ).result; safePrint('Downloaded file: ${result.downloadedItem.path}'); } on StorageException catch (e) { safePrint(e.message); }}
Monitor download progress
final operation = Amplify.Storage.downloadData( path: const StoragePath.fromString('public/example.txt'), onProgress: (progress) { safePrint('fraction totalBytes: ${progress.totalBytes}'); safePrint('fraction transferredBytes: ${progress.transferredBytes}'); safePrint('fraction completed: ${progress.fractionCompleted}'); },);
Pause, resume, and cancel downloads
Future<void> upload() async { final operation = Amplify.Storage.downloadFile( localFile: AWSFile.fromPath('/path/to/local/file'), path: const StoragePath.fromString('public/example.txt'), );
// pause operation await operation.pause();
// resume operation await operation.resume();
// cancel operation await operation.cancel();}
API to download data in memory
You can download a file to in-memory buffer with Amplify.Storage.downloadData
:
Future<void> download() async { try { final result = await Amplify.Storage.downloadData( path: const StoragePath.fromString('public/example.txt'), ).result; safePrint('Downloaded data: ${result.bytes}'); } on StorageException catch (e) { safePrint(e.message); }}
More download options
Option | Type | Description |
---|---|---|
getProperties | boolean | Whether to retrieve properties for the downloaded object using theAmplify.Storage.getProperties() after the operation completes. When set to true the returned item will contain additional info such as metadata and content type. |
useAccelerateEndpoint | boolean | Whether to use accelerate endpoint. Read more at Transfer Acceleration |
bytesRange | S3DataBytesRange | The byte range to download from the object |
Example of downloadFile
with options
final operation = Amplify.Storage.downloadFile( path: const StoragePath.fromString('public/example.txt'), localFile: AWSFile.fromPath('/path/to/local/file.txt'), options: const StorageDownloadFileOptions( pluginOptions: S3DownloadFilePluginOptions( getProperties: true, useAccelerateEndpoint: true, ), ),);
Example of downloadData
with options
final operation = Amplify.Storage.downloadData( path: const StoragePath.fromString('public/example.txt'), options: StorageDownloadDataOptions( pluginOptions: S3DownloadDataPluginOptions( getProperties: true, useAccelerateEndpoint: true, bytesRange: S3DataBytesRange(start: 0, end: 100), ), ),);