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

Page updated Dec 12, 2024

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

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