Download files
There are two 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.
1import 'dart:io';2import 'package:path_provider/path_provider.dart';3
4Future<void> downloadFile() async {5 final documentsDir = await getApplicationDocumentsDirectory();6 final filepath = documentsDir.path + '/example.txt';7 final file = File(filepath);8
9 try {10 final result = await Amplify.Storage.downloadFile(11 key: 'ExampleKey',12 local: file,13 onProgress: (progress) {14 safePrint('Fraction completed: ${progress.getFractionCompleted()}');15 },16 );17 final contents = result.file.readAsStringSync();18 // Or you can reference the file that is created above19 // final contents = file.readAsStringSync();20 safePrint('Downloaded contents: $contents');21 } on StorageException catch (e) {22 safePrint('Error downloading file: $e');23 }24}
Generate a download URL
You can get a downloadable URL for the file in storage by its key using Amplify.Storage.getUrl
.
1Future<void> getDownloadUrl() async {2 try {3 final result = await Amplify.Storage.getUrl(key: 'ExampleKey');4 // NOTE: This code is only for demonstration5 // Your debug console may truncate the printed url string6 safePrint('Got URL: ${result.url}');7 } on StorageException catch (e) {8 safePrint('Error getting download URL: $e');9 }10}