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

Page updated Sep 19, 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)
);

Check the existence of a file

When creating a downloadable URL, you can choose to check if the file exists by setting validateObjectExistence to true in AWSS3StorageGetPresignedUrlOptions. If the file is inaccessible or does not exist, a StorageException is thrown. This allows you to check if an object exists when generating the presigned URL, which you can then use to download that object.

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

All getURL options

OptionTypeDescription
bucketStorageBucketThe bucket in which the object is stored.
expiresIntegerNumber of seconds before the URL expires.
useAccelerateEndpointBooleanFlag to configure use of acceleration mode.
validateObjectExistenceBooleanFlag to check if the file exists.

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

Download from a specified bucket

You can also perform a download operation to a specific bucket by providing the bucket option. You can pass in a string representing the target bucket's assigned name in Amplify Backend.

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

Alternatively, you can also pass in an object by specifying the bucket name and region from the console.

BucketInfo bucketInfo = new BucketInfo("second-bucket-name-from-console", "us-east-2");
StorageBucket secondBucket = StorageBucket.fromBucketInfo(bucketInfo);
StorageDownloadFileOptions options = StorageDownloadFileOptions.builder().bucket(secondBucket).build();
Amplify.Storage.downloadFile(
StoragePath.fromString("public/example"),
new File(getApplicationContext().getFilesDir() + "/download.txt"),
options,
result -> Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName()),
error -> Log.e("MyAmplifyApp", "Download Failure", error)
);
val bucketInfo = BucketInfo("second-bucket-name-from-console", "us-east-2")
val secondBucket = StorageBucket.fromBucketInfo(bucketInfo)
val options = StorageDownloadFileOptions.builder().bucket(secondBucket).build()
val file = File("${applicationContext.filesDir}/download.txt")
Amplify.Storage.downloadFile(StoragePath.fromString("public/example"), file, options,
{ Log.i("MyAmplifyApp", "Successfully downloaded: ${it.file.name}") },
{ Log.e("MyAmplifyApp", "Download Failure", it) }
)
val bucketInfo = BucketInfo("second-bucket-name-from-console", "us-east-2")
val secondBucket = StorageBucket.fromBucketInfo(bucketInfo)
val options = StorageDownloadFileOptions.builder().bucket(secondBucket).build()
val file = File("${applicationContext.filesDir}/download.txt")
val download = Amplify.Storage.downloadFile(StoragePath.fromString("public/example"), file, options)
try {
val fileName = download.result().file.name
Log.i("MyAmplifyApp", "Successfully downloaded: $fileName")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Download Failure", error)
}
BucketInfo bucketInfo = new BucketInfo("second-bucket-name-from-console", "us-east-2");
StorageBucket secondBucket = StorageBucket.fromBucketInfo(bucketInfo);
StorageDownloadFileOptions options = StorageDownloadFileOptions.builder().bucket(secondBucket).build();
RxProgressAwareSingleOperation<StorageDownloadFileResult> download =
RxAmplify.Storage.downloadFile(
StoragePath.fromString("public/example"),
new File(getApplicationContext().getFilesDir() + "/download.txt"),
options,
);
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);
);