Download files
Download to file
If you uploaded the data using the key ExampleKey
, you can retrieve the data using Amplify.Storage.downloadFile
.
1Amplify.Storage.downloadFile(2 "ExampleKey",3 new File(getApplicationContext().getFilesDir() + "/download.txt"),4 result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),5 error -> Log.e("MyAmplifyApp", "Download Failure", error)6);
1val file = File("${applicationContext.filesDir}/download.txt")2Amplify.Storage.downloadFile("ExampleKey", file,3 { Log.i("MyAmplifyApp", "Successfully downloaded: ${it.file.name}") },4 { Log.e("MyAmplifyApp", "Download Failure", it) }5)
1try {2 val file = File("${applicationContext.filesDir}/download.txt")3 val download = Amplify.Storage.downloadFile("ExampleKey", file)4 try {5 val fileName = download.result().file.name6 Log.i("MyAmplifyApp", "Successfully downloaded: $fileName")7 } catch (error: StorageException) {8 Log.e("MyAmplifyApp", "Download Failure", error)9 }10}
1RxProgressAwareSingleOperation<StorageDownloadFileResult> download =2 RxAmplify.Storage.downloadFile(3 "ExampleKey",4 new File(getApplicationContext().getFilesDir() + "/download.txt"5 );6
7download8 .observeResult()9 .subscribe(10 result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),11 error -> Log.e("MyAmplifyApp", "Download Failure", error)12 );
Track download progress
To track progress of the download, use the downloadFile
API that includes a progress listener callback.
1Amplify.Storage.downloadFile(2 "ExampleKey",3 new File(getApplicationContext().getFilesDir() + "/download.txt"),4 StorageDownloadFileOptions.defaultInstance(),5 progress -> Log.i("MyAmplifyApp", "Fraction completed: " + progress.getFractionCompleted()),6 result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),7 error -> Log.e("MyAmplifyApp", "Download Failure", error)8);
1val file = File("${applicationContext.filesDir}/download.txt")2val options = StorageDownloadFileOptions.defaultInstance()3Amplify.Storage.downloadFile("ExampleKey", file, options,4 { Log.i("MyAmplifyApp", "Fraction completed: ${it.fractionCompleted}") },5 { Log.i("MyAmplifyApp", "Successfully downloaded: ${it.file.name}") },6 { Log.e("MyAmplifyApp", "Download Failure", it) }7)
1val file = File("${applicationContext.filesDir}/download.txt")2val options = StorageDownloadFileOptions.defaultInstance()3val download = Amplify.Storage.downloadFile("ExampleKey", file, options)4val progressJob = activityScope.async {5 download.progress().collect { progress ->6 Log.i("MyAmplifyApp", "Fraction completed: ${progress.fractionCompleted}")7 }8}9try {10 val fileName = download.result().file.name11 Log.i("MyAmplifyApp", "Successfully downloaded: $fileName")12} catch (error: StorageException) {13 Log.e("MyAmplifyApp", "Download Failure", error)14}15progressJob.cancel()
1RxProgressAwareSingleOperation<StorageDownloadFileResult> download =2 RxAmplify.Storage.downloadFile(remoteKey, localFile);3
4download5 .observeProgress()6 .subscribe(7 progress -> Log.i("MyAmplifyApp", progress.getFractionCompleted())8 );
Generate a download URL
You can also retrieve a URL for the object in storage:
1Amplify.Storage.getUrl(2 "ExampleKey",3 result -> Log.i("MyAmplifyApp", "Successfully generated: " + result.getUrl()),4 error -> Log.e("MyAmplifyApp", "URL generation failure", error)5);
1Amplify.Storage.getUrl(2 "ExampleKey",3 { Log.i("MyAmplifyApp", "Successfully generated: ${it.url}") },4 { Log.e("MyAmplifyApp", "URL generation failure", it) }5)
1try {2 val url = Amplify.Storage.getUrl("ExampleKey").url3 Log.i("MyAmplifyApp", "Successfully generated: $url")4} catch (error: StorageException) {5 Log.e("MyAmplifyApp", "URL generation failure", error)6}
1RxAmplify.Storage.getUrl("ExampleKey").subscribe(2 result -> Log.i("MyAmplifyApp", "Successfully generated: " + result.getUrl()),3 error -> Log.e("MyAmplifyApp", "URL generation failure", error)4);