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

Upload files

Implement upload functionality

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

Upload from file

private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
Amplify.Storage.uploadFile(
StoragePath.fromString("public/example"),
exampleFile,
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
);
}
private fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile,
{ Log.i("MyAmplifyApp", "Successfully uploaded: ${it.path}") },
{ Log.e("MyAmplifyApp", "Upload failed", it) }
)
}
private suspend fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
val upload = Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile)
try {
val result = upload.result()
Log.i("MyAmplifyApp", "Successfully uploaded: ${result.path}")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Upload failed", error)
}
}
private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
RxProgressAwareSingleOperation<StorageUploadFileResult> rxUploadOperation =
RxAmplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile);
rxUploadOperation
.observeResult()
.subscribe(
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
error -> Log.e("MyAmplifyApp", "Upload failed", error)
);
}

Upload from Input Stream

private void uploadInputStream() {
try {
InputStream exampleInputStream = getContentResolver().openInputStream(uri);
Amplify.Storage.uploadInputStream(
StoragePath.fromString("public/example"),
exampleInputStream,
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
);
} catch (FileNotFoundException error) {
Log.e("MyAmplifyApp", "Could not find file to open for input stream.", error);
}
}
private fun uploadInputStream(uri: Uri) {
val stream = contentResolver.openInputStream(uri)
Amplify.Storage.uploadInputStream(StoragePath.fromString("public/example"), stream,
{ Log.i("MyAmplifyApp", "Successfully uploaded: ${it.path}") },
{ Log.e("MyAmplifyApp", "Upload failed", it) }
)
}
private suspend fun uploadInputStream(uri: Uri) {
val stream = contentResolver.openInputStream(uri)
val upload = Amplify.Storage.uploadInputStream(StoragePath.fromString("public/example"), stream)
try {
val result = upload.result()
Log.i("MyAmplifyApp", "Successfully uploaded: ${result.path}.")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Upload failed")
}
}
private void uploadInputStream() {
try {
InputStream exampleInputStream = getContentResolver().openInputStream(uri);
RxProgressAwareSingleOperation<StorageUploadInputStreamResult> rxUploadOperation =
RxAmplify.Storage.uploadInputStream(StoragePath.fromString("public/example"), exampleInputStream);
rxUploadOperation
.observeResult()
.subscribe(
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
error -> Log.e("MyAmplifyApp", "Upload failed", error)
);
} catch (FileNotFoundException error) {
Log.e("MyAmplifyApp", "Could not find file to open for input stream.", error);
}
}

Upload to a specified bucket

You can also perform an upload 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.

private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
StorageBucket secondBucket = StorageBucket.fromOutputs("secondBucket");
StorageUploadFileOptions options = StorageUploadFileOptions.builder().bucket(secondBucket).build();
Amplify.Storage.uploadFile(
StoragePath.fromString("public/example"),
exampleFile,
options,
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
);
}
private fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
val secondBucket = StorageBucket.fromOutputs("secondBucket")
val options = StorageUploadFileOptions.builder().bucket(secondBucket).build()
Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options,
{ Log.i("MyAmplifyApp", "Successfully uploaded: ${it.path}") },
{ Log.e("MyAmplifyApp", "Upload failed", it) }
)
}
private suspend fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
val secondBucket = StorageBucket.fromOutputs("secondBucket")
val options = StorageUploadFileOptions.builder().bucket(secondBucket).build()
val upload = Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options)
try {
val result = upload.result()
Log.i("MyAmplifyApp", "Successfully uploaded: ${result.path}")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Upload failed", error)
}
}
private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
StorageBucket secondBucket = StorageBucket.fromOutputs("secondBucket");
StorageUploadFileOptions options = StorageUploadFileOptions.builder().bucket(secondBucket).build();
RxProgressAwareSingleOperation<StorageUploadFileResult> rxUploadOperation =
RxAmplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options);
rxUploadOperation
.observeResult()
.subscribe(
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
error -> Log.e("MyAmplifyApp", "Upload failed", error)
);
}

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

private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
BucketInfo bucketInfo = new BucketInfo("second-bucket-name-from-console", "us-east-2");
StorageBucket secondBucket = StorageBucket.fromBucketInfo(bucketInfo);
StorageUploadFileOptions options = StorageUploadFileOptions.builder().bucket(secondBucket).build();
Amplify.Storage.uploadFile(
StoragePath.fromString("public/example"),
exampleFile,
options,
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
);
}
private fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
val bucketInfo = new BucketInfo("second-bucket-name-from-console", "us-east-2");
val secondBucket = StorageBucket.fromBucketInfo(bucketInfo);
val options = StorageUploadFileOptions.builder().bucket(secondBucket).build();
Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options,
{ Log.i("MyAmplifyApp", "Successfully uploaded: ${it.path}") },
{ Log.e("MyAmplifyApp", "Upload failed", it) }
)
}
private suspend fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
val bucketInfo = new BucketInfo("second-bucket-name-from-console", "us-east-2");
val secondBucket = StorageBucket.fromBucketInfo(bucketInfo);
val options = StorageUploadFileOptions.builder().bucket(secondBucket).build();
val upload = Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options)
try {
val result = upload.result()
Log.i("MyAmplifyApp", "Successfully uploaded: ${result.path}")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Upload failed", error)
}
}
private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
BucketInfo bucketInfo = new BucketInfo("second-bucket-name-from-console", "us-east-2");
StorageBucket secondBucket = StorageBucket.fromBucketInfo(bucketInfo);
StorageUploadFileOptions options = StorageUploadFileOptions.builder().bucket(secondBucket).build();
RxProgressAwareSingleOperation<StorageUploadFileResult> rxUploadOperation =
RxAmplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options);
rxUploadOperation
.observeResult()
.subscribe(
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
error -> Log.e("MyAmplifyApp", "Upload failed", error)
);
}

Monitor upload progress

To track progress of the upload, use the uploadFile API that includes a progress listener callback.

private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
Amplify.Storage.uploadFile(
StoragePath.fromString("public/example"),
exampleFile,
StorageUploadFileOptions.defaultInstance(),
progress -> Log.i("MyAmplifyApp", "Fraction completed: " + progress.getFractionCompleted()),
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
);
}
private fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
val options = StorageUploadFileOptions.defaultInstance()
Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options,
{ Log.i("MyAmplifyApp", "Fraction completed: ${it.fractionCompleted}") },
{ Log.i("MyAmplifyApp", "Successfully uploaded: ${it.path}") },
{ Log.e("MyAmplifyApp", "Upload failed", it) }
)
}
private suspend fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
val options = StorageUploadFileOptions.defaultInstance()
val upload = Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options)
val progressJob = activityScope.async {
upload.progress().collect {
Log.i("MyAmplifyApp", "Fraction completed: ${it.fractionCompleted}")
}
}
try {
val result = upload.result()
Log.i("MyAmplifyApp", "Successfully uploaded: ${result.path}")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Upload failed", error)
}
progressJob.cancel()
}
RxProgressAwareSingleOperation<StorageUploadFileResult> upload =
RxAmplify.Storage.uploadFile("example", exampleFile);
upload
.observeProgress()
.subscribe(
progress -> Log.i("MyAmplifyApp", progress.getFractionCompleted())
);

All upload options

OptionTypeDescription
metadataMap<String, String>Metadata for the object to store.
contentTypeStringThe standard MIME type describing the format of the object to store.
bucketStorageBucketThe bucket in which the object should be stored.
serverSideEncryptionServerSideEncryptionThe server side encryption algorithm.
useAccelerateEndpointbooleanFlag to determine whether to use acceleration endpoint.

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

Transfer with Object Metadata

To upload a file accompanied by metadata, utilize the StorageUploadFileOptions builder. Start by creating a hashMap object, then incorporate it into the StorageUploadFileOptions during the build process before passing it along to the upload function.

private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
// Create metadata
Map<String, String> userMetadata = new HashMap<>();
userMetadata.put("myKey", "myVal");
// Configure upload options with metadata
StorageUploadFileOptions options = StorageUploadFileOptions.builder()
.metadata(userMetadata)
.build();
// Perform the upload
Amplify.Storage.uploadFile(
StoragePath.fromString("public/example"),
exampleFile,
options,
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
error -> Log.e("MyAmplifyApp", "Upload failed", error)
);
}
fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
// Create metadata
val userMetadata: MutableMap<String, String> = HashMap()
userMetadata["myKey"] = "myVal"
// Configure upload options with metadata
val options = StorageUploadFileOptions.builder()
.metadata(userMetadata)
.build()
// Perform the upload
Amplify.Storage.uploadFile(
StoragePath.fromString("public/example"),
exampleFile,
options,
{ result -> Log.i("MyAmplifyApp", "Successfully uploaded: ${result.path}") },
{ error -> Log.e("MyAmplifyApp", "Upload failed", error) }
)
}
fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "example")
exampleFile.writeText("Example file contents")
// Create metadata
val userMetadata: MutableMap<String, String> = HashMap()
userMetadata["myKey"] = "myVal"
// Configure upload options with metadata
val options = StorageUploadFileOptions.builder()
.metadata(userMetadata)
.build()
val upload = Amplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options)
val progressJob = activityScope.async {
upload.progress().collect {
Log.i("MyAmplifyApp", "Fraction completed: ${it.fractionCompleted}")
}
}
try {
val result = upload.result()
Log.i("MyAmplifyApp", "Successfully uploaded: ${result.path}")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Upload failed", error)
}
progressJob.cancel()
}
private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "example");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
writer.append("Example file contents");
writer.close();
} catch (Exception exception) {
Log.e("MyAmplifyApp", "Upload failed", exception);
}
Map<String, String> userMetadata = new HashMap<>();
userMetadata.put("myKey", "myVal");
StorageUploadFileOptions options = StorageUploadFileOptions.builder()
.metadata(userMetadata)
.build();
RxStorageBinding.RxProgressAwareSingleOperation<StorageUploadFileResult> rxUploadOperation =
RxAmplify.Storage.uploadFile(StoragePath.fromString("public/example"), exampleFile, options);
rxUploadOperation
.observeResult()
.subscribe(
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getPath()),
error -> Log.e("MyAmplifyApp", "Upload failed", error)
);
}

Upload using a presigned URL

You can use the getUrl API with StorageAccessMethod.PUT to generate a presigned URL for uploading files directly to S3. This is useful when:

  • You need to integrate with third-party tools or libraries that only accept standard HTTP URL endpoints
  • You want to share a temporary upload link with another client or service
  • You need to upload from a context where the Amplify SDK is not available
AWSS3StorageGetPresignedUrlOptions options = AWSS3StorageGetPresignedUrlOptions.builder()
.method(StorageAccessMethod.PUT)
.expires(3600)
.build();
Amplify.Storage.getUrl(
StoragePath.fromString("public/uploads/photo.jpg"),
options,
result -> {
URL presignedUrl = result.getUrl();
Log.i("MyAmplifyApp", "Presigned upload URL: " + presignedUrl);
},
error -> Log.e("MyAmplifyApp", "Failed to generate URL", error)
);
val options = AWSS3StorageGetPresignedUrlOptions.builder()
.method(StorageAccessMethod.PUT)
.expires(3600)
.build()
Amplify.Storage.getUrl(
StoragePath.fromString("public/uploads/photo.jpg"),
options,
{ Log.i("MyAmplifyApp", "Presigned upload URL: ${it.url}") },
{ Log.e("MyAmplifyApp", "Failed to generate URL", it) }
)
val options = AWSS3StorageGetPresignedUrlOptions.builder()
.method(StorageAccessMethod.PUT)
.expires(3600)
.build()
try {
val result = Amplify.Storage.getUrl(
StoragePath.fromString("public/uploads/photo.jpg"),
options
)
Log.i("MyAmplifyApp", "Presigned upload URL: ${result.url}")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Failed to generate URL", error)
}
AWSS3StorageGetPresignedUrlOptions options = AWSS3StorageGetPresignedUrlOptions.builder()
.method(StorageAccessMethod.PUT)
.expires(3600)
.build();
RxAmplify.Storage.getUrl(StoragePath.fromString("public/uploads/photo.jpg"), options)
.subscribe(
result -> Log.i("MyAmplifyApp", "Presigned upload URL: " + result.getUrl()),
error -> Log.e("MyAmplifyApp", "Failed to generate URL", error)
);

Then use the presigned URL to upload the file with a standard HTTP PUT request:

val presignedUrl = result.url
val connection = presignedUrl.openConnection() as HttpURLConnection
connection.doOutput = true
connection.requestMethod = "PUT"
connection.setRequestProperty("Content-Type", "image/jpeg")
connection.outputStream.use { outputStream ->
outputStream.write(imageData)
}
val responseCode = connection.responseCode
Log.i("MyAmplifyApp", "Upload status: $responseCode")
connection.disconnect()

When StorageAccessMethod.PUT is specified, the validateObjectExistence option is ignored since the object may not exist yet.

Presigned URL upload options

OptionTypeDefaultDescription
methodStorageAccessMethodGETGET generates a download URL. PUT generates an upload URL.
expiresint18000Number of seconds before the URL expires.
bucketStorageBucketDefault bucket from Amplify configurationThe bucket in which the object is stored.
validateObjectExistencebooleanfalseWhether to check the object exists before generating the URL. Skipped when method is PUT.
useAccelerateEndpointbooleanfalseWhether to use the S3 Transfer Acceleration endpoint.

MultiPart upload

Amplify will automatically perform an Amazon S3 multipart upload for objects that are larger than 5MB. For more information about S3's multipart upload, see Uploading and copying objects using multipart upload