Download files
There are three ways of getting data that was previously uploaded:
Download File
You can download 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); }}
Download data
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); }}
Download Progress
To track progress of the download, use the progress listener callback.
final operation = Amplify.Storage.downloadFile( localFile: AWSFile.fromPath('/path/to/local/file.txt'), path: const StoragePath.fromString('public/example.txt'), onProgress: (progress) { safePrint('fraction totalBytes: ${progress.totalBytes}'); safePrint('fraction transferredBytes: ${progress.transferredBytes}'); safePrint('fraction completed: ${progress.fractionCompleted}'); });
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}'); },);
Control of Download Operations
A call to Amplify.Storage.downloadFile
or Amplify.Storage.downloadData
returns a reference to the operation that is performing the upload.
To cancel the upload (for example, in response to the user pressing a Cancel button), simply call .cancel()
on the returned upload operation. Download operations also allows for the operation to be paused and resumed.
Future<void> download() async { final operation = Amplify.Storage.downloadFile( localFile: AWSFile.fromPath('/path/to/local/file.txt'), path: const StoragePath.fromString('public/example.txt'), );
// pause operation await operation.pause();
// resume operation await operation.resume();
// cancel operation await operation.cancel();}
Future<void> download() async { final operation = Amplify.Storage.downloadData( path: const StoragePath.fromString('public/example.txt'), );
// pause operation await operation.pause();
// resume operation await operation.resume();
// cancel operation await operation.cancel();}
Generate a download URL
You can get a downloadable URL for the file in storage by its path.
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'), options: const StorageGetUrlOptions( pluginOptions: S3GetUrlPluginOptions( validateObjectExistence: true, expiresIn: Duration(days: 1), ), ), ).result; safePrint('url: ${result.url}'); } on StorageException catch (e) { safePrint(e.message); }}