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.
import 'dart:io';import 'package:path_provider/path_provider.dart';
Future<void> downloadFile() async { final documentsDir = await getApplicationDocumentsDirectory(); final filepath = documentsDir.path + '/example.txt'; final file = File(filepath);
try { final result = await Amplify.Storage.downloadFile( key: 'ExampleKey', local: file, onProgress: (progress) { safePrint('Fraction completed: ${progress.getFractionCompleted()}'); }, ); final contents = result.file.readAsStringSync(); // Or you can reference the file that is created above // final contents = file.readAsStringSync(); safePrint('Downloaded contents: $contents'); } on StorageException catch (e) { safePrint('Error downloading file: $e'); }}
Generate a download URL
You can get a downloadable URL for the file in storage by its key using Amplify.Storage.getUrl
.
Future<void> getDownloadUrl() async { try { final result = await Amplify.Storage.getUrl(key: 'ExampleKey'); // NOTE: This code is only for demonstration // Your debug console may truncate the printed url string safePrint('Got URL: ${result.url}'); } on StorageException catch (e) { safePrint('Error getting download URL: $e'); }}