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

Page updated May 16, 2024

Download files

To further customize your in-app experience, you can use the getUrl or downloadData API from the Amplify Library for Storage.

Note: Refer to the Transfer Acceleration documentation to learn how to enable transfer acceleration for storage APIs.

Get or download file from a URL

With the getUrl API, you can get a presigned URL which is valid for 900 seconds or 15 minutes by default. You can use this URL to create a download link for users to click on. The expiresAt property is a Date object that represents the time at which the URL will expire.

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)
);

Download to a file

Use the downloadFile API to download the file locally on the client.

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)
);

Monitor download progress

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())
);

Query transfers

When an upload or download operation is requested using the Amplify Android library, the request is first persisted in the local SQLite Database and then queued for execution. You can query the transfer operation queued in the local database using the transfer ID returned by an upload or download API. Get-Transfer API could retrieve a pending transfer previously en-queued and enable attaching a listener to receive updates on progress change, on-error or on-success, or pause, cancel or resume it.

Amplify.Storage.getTransfer("TRANSFER_ID",
operation -> {
Log.i("MyAmplifyApp", "Current State" + operation.getTransferState());
// set listener to receive updates
operation.setOnProgress( progress -> {});
operation.setOnSuccess( result -> {});
operation.setOnError(error -> {});
// possible actions
operation.pause();
operation.resume();
operation.start();
operation.cancel();
},
{
error -> Log.e("MyAmplifyApp", "Failed to query transfer", error)
}
);
Amplify.Storage.getTransfer("TRANSFER_ID",
{ operation ->
Log.i("MyAmplifyApp", "Current State" + operation.transferState)
// set listener to receive updates
operation.setOnProgress { }
operation.setOnSuccess { }
operation.setOnError { }
// possible actions
operation.pause()
operation.resume()
operation.start()
operation.cancel()
},
{
Log.e("MyAmplifyApp", "Failed to query transfer", it)
}
)
try {
val operation = Amplify.Storage.getTransfer("TRANSFER_ID")
Log.i("MyAmplifyApp", "Current State" + operation.transferState)
// set listener to receive updates
operation.setOnProgress { }
operation.setOnSuccess { }
operation.setOnError { }
// possible actions
operation.pause()
operation.resume()
operation.start()
operation.cancel()
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Failed to query transfer", error)
}
RxAmplify.Storage.getTransfer("TRANSFER_ID")
.subscribe(
operation -> {
Log.i("MyAmplifyApp", "Current State" + operation.getTransferState());
// set listener to receive updates
operation.setOnProgress( progress -> {});
operation.setOnSuccess( result -> {});
operation.setOnError(error -> {});
// possible actions
operation.pause();
operation.resume();
operation.start();
operation.cancel();
},
error -> Log.e("MyAmplifyApp", "Failed to query transfer", error);
);