Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated May 1, 2024

Download files

The latest version of Amplify Storage supports specifying S3 objects as a paths.
We recommend using path instead of key to specify S3 objects.

Note: key parameter is deprecated and may be removed in next major version.

Download to file

With StoragePath

Amplify.Storage.downloadFile(
StoragePath.fromString("public/example"),
new File(getApplicationContext().getFilesDir() + "/download.txt"),
result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),
error -> Log.e("MyAmplifyApp", "Download Failure", error)
);
val file = File("${applicationContext.filesDir}/download.txt")
Amplify.Storage.downloadFile(StoragePath.fromString("public/example"), file,
{ Log.i("MyAmplifyApp", "Successfully downloaded: ${it.file.name}") },
{ Log.e("MyAmplifyApp", "Download Failure", it) }
)
try {
val file = File("${applicationContext.filesDir}/download.txt")
val download = Amplify.Storage.downloadFile(StoragePath.fromString("public/example"), file)
try {
val fileName = download.result().file.name
Log.i("MyAmplifyApp", "Successfully downloaded: $fileName")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Download Failure", error)
}
}
RxProgressAwareSingleOperation<StorageDownloadFileResult> download =
RxAmplify.Storage.downloadFile(
StoragePath.fromString("public/example"),
new File(getApplicationContext().getFilesDir() + "/download.txt"
);
download
.observeResult()
.subscribe(
result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),
error -> Log.e("MyAmplifyApp", "Download Failure", error)
);

With Key (Deprecated)

Amplify.Storage.downloadFile(
"ExampleKey",
new File(getApplicationContext().getFilesDir() + "/download.txt"),
result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),
error -> Log.e("MyAmplifyApp", "Download Failure", error)
);
val file = File("${applicationContext.filesDir}/download.txt")
Amplify.Storage.downloadFile("ExampleKey", file,
{ Log.i("MyAmplifyApp", "Successfully downloaded: ${it.file.name}") },
{ Log.e("MyAmplifyApp", "Download Failure", it) }
)
try {
val file = File("${applicationContext.filesDir}/download.txt")
val download = Amplify.Storage.downloadFile("ExampleKey", file)
try {
val fileName = download.result().file.name
Log.i("MyAmplifyApp", "Successfully downloaded: $fileName")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Download Failure", error)
}
}
RxProgressAwareSingleOperation<StorageDownloadFileResult> download =
RxAmplify.Storage.downloadFile(
"ExampleKey",
new File(getApplicationContext().getFilesDir() + "/download.txt"
);
download
.observeResult()
.subscribe(
result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),
error -> Log.e("MyAmplifyApp", "Download Failure", error)
);

Track download progress

To track progress of the download, use the downloadFile API that includes a progress listener callback.

With StoragePath

Amplify.Storage.downloadFile(
StoragePath.fromString("public/example"),
new File(getApplicationContext().getFilesDir() + "/download.txt"),
StorageDownloadFileOptions.defaultInstance(),
progress -> Log.i("MyAmplifyApp", "Fraction completed: " + progress.getFractionCompleted()),
result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),
error -> Log.e("MyAmplifyApp", "Download Failure", error)
);
val file = File("${applicationContext.filesDir}/download.txt")
val options = StorageDownloadFileOptions.defaultInstance()
Amplify.Storage.downloadFile(StoragePath.fromString("public/example"), file, options,
{ Log.i("MyAmplifyApp", "Fraction completed: ${it.fractionCompleted}") },
{ Log.i("MyAmplifyApp", "Successfully downloaded: ${it.file.name}") },
{ Log.e("MyAmplifyApp", "Download Failure", it) }
)
val file = File("${applicationContext.filesDir}/download.txt")
val options = StorageDownloadFileOptions.defaultInstance()
val download = Amplify.Storage.downloadFile(StoragePath.fromString("public/example"), file, options)
val progressJob = activityScope.async {
download.progress().collect { progress ->
Log.i("MyAmplifyApp", "Fraction completed: ${progress.fractionCompleted}")
}
}
try {
val fileName = download.result().file.name
Log.i("MyAmplifyApp", "Successfully downloaded: $fileName")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Download Failure", error)
}
progressJob.cancel()
RxProgressAwareSingleOperation<StorageDownloadFileResult> download =
RxAmplify.Storage.downloadFile(StoragePath.fromString("public/example"), localFile);
download
.observeProgress()
.subscribe(
progress -> Log.i("MyAmplifyApp", progress.getFractionCompleted())
);

With Key (Deprecated)

Amplify.Storage.downloadFile(
"ExampleKey",
new File(getApplicationContext().getFilesDir() + "/download.txt"),
StorageDownloadFileOptions.defaultInstance(),
progress -> Log.i("MyAmplifyApp", "Fraction completed: " + progress.getFractionCompleted()),
result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),
error -> Log.e("MyAmplifyApp", "Download Failure", error)
);
val file = File("${applicationContext.filesDir}/download.txt")
val options = StorageDownloadFileOptions.defaultInstance()
Amplify.Storage.downloadFile("ExampleKey", file, options,
{ Log.i("MyAmplifyApp", "Fraction completed: ${it.fractionCompleted}") },
{ Log.i("MyAmplifyApp", "Successfully downloaded: ${it.file.name}") },
{ Log.e("MyAmplifyApp", "Download Failure", it) }
)
val file = File("${applicationContext.filesDir}/download.txt")
val options = StorageDownloadFileOptions.defaultInstance()
val download = Amplify.Storage.downloadFile("ExampleKey", file, options)
val progressJob = activityScope.async {
download.progress().collect { progress ->
Log.i("MyAmplifyApp", "Fraction completed: ${progress.fractionCompleted}")
}
}
try {
val fileName = download.result().file.name
Log.i("MyAmplifyApp", "Successfully downloaded: $fileName")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Download Failure", error)
}
progressJob.cancel()
RxProgressAwareSingleOperation<StorageDownloadFileResult> download =
RxAmplify.Storage.downloadFile(remoteKey, localFile);
download
.observeProgress()
.subscribe(
progress -> Log.i("MyAmplifyApp", progress.getFractionCompleted())
);

Generate a download URL

You can also retrieve a URL for the object in storage:

With StoragePath

Amplify.Storage.getUrl(
StoragePath.fromString("public/example"),
result -> Log.i("MyAmplifyApp", "Successfully generated: " + result.getUrl()),
error -> Log.e("MyAmplifyApp", "URL generation failure", error)
);
Amplify.Storage.getUrl(
StoragePath.fromString("public/example"),
{ Log.i("MyAmplifyApp", "Successfully generated: ${it.url}") },
{ Log.e("MyAmplifyApp", "URL generation failure", it) }
)
try {
val url = Amplify.Storage.getUrl(StoragePath.fromString("public/example")).url
Log.i("MyAmplifyApp", "Successfully generated: $url")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "URL generation failure", error)
}
RxAmplify.Storage.getUrl(StoragePath.fromString("public/example")).subscribe(
result -> Log.i("MyAmplifyApp", "Successfully generated: " + result.getUrl()),
error -> Log.e("MyAmplifyApp", "URL generation failure", error)
);

With Key (Deprecated)

Amplify.Storage.getUrl(
"ExampleKey",
result -> Log.i("MyAmplifyApp", "Successfully generated: " + result.getUrl()),
error -> Log.e("MyAmplifyApp", "URL generation failure", error)
);
Amplify.Storage.getUrl(
"ExampleKey",
{ Log.i("MyAmplifyApp", "Successfully generated: ${it.url}") },
{ Log.e("MyAmplifyApp", "URL generation failure", it) }
)
try {
val url = Amplify.Storage.getUrl("ExampleKey").url
Log.i("MyAmplifyApp", "Successfully generated: $url")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "URL generation failure", error)
}
RxAmplify.Storage.getUrl("ExampleKey").subscribe(
result -> Log.i("MyAmplifyApp", "Successfully generated: " + result.getUrl()),
error -> Log.e("MyAmplifyApp", "URL generation failure", error)
);