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

Page updated Apr 29, 2024

Upload files

Amplify Android v1 is deprecated as of June 1st, 2024. No new features or bug fixes will be added. Dependencies may become outdated and potentially introduce compatibility issues.

Please use the latest version (v2) of Amplify Library for Android to get started. Refer to the upgrade guide for instructions on upgrading your application to the latest version.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for Android, you can access the documentation here.

Amplify allows you to upload Files and InputStreams.

Upload InputStream

To upload data to S3 from an InputStream:

private void uploadInputStream() {
try {
InputStream exampleInputStream = getContentResolver().openInputStream(uri);
Amplify.Storage.uploadInputStream(
"ExampleKey",
exampleInputStream,
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),
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("ExampleKey", stream,
{ Log.i("MyAmplifyApp", "Successfully uploaded: ${it.key}") },
{ Log.e("MyAmplifyApp", "Upload failed", it) }
)
}
private suspend fun uploadInputStream(uri: Uri) {
val stream = contentResolver.openInputStream(uri)
val upload = Amplify.Storage.uploadInputStream("ExampleKey", stream)
try {
val result = upload.result()
Log.i("MyAmplifyApp", "Successfully uploaded: ${result.key}.")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Upload failed")
}
}
private void uploadInputStream() {
try {
InputStream exampleInputStream = getContentResolver().openInputStream(uri);
RxProgressAwareSingleOperation<StorageUploadInputStreamResult> rxUploadOperation =
RxAmplify.Storage.uploadInputStream("ExampleKey", exampleInputStream);
rxUploadOperation
.observeResult()
.subscribe(
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),
error -> Log.e("MyAmplifyApp", "Upload failed", error)
);
} catch (FileNotFoundException error) {
Log.e("MyAmplifyApp", "Could not find file to open for input stream.", error);
}
}

Upload files

To upload to S3 from a data object, specify the key and the file to be uploaded.

private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "ExampleKey");
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(
"ExampleKey",
exampleFile,
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),
storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
);
}
private fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "ExampleKey")
exampleFile.writeText("Example file contents")
Amplify.Storage.uploadFile("ExampleKey", exampleFile,
{ Log.i("MyAmplifyApp", "Successfully uploaded: ${it.key}") },
{ Log.e("MyAmplifyApp", "Upload failed", it) }
)
}
private suspend fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "ExampleKey")
exampleFile.writeText("Example file contents")
val upload = Amplify.Storage.uploadFile("ExampleKey", exampleFile)
try {
val result = upload.result()
Log.i("MyAmplifyApp", "Successfully uploaded: ${result.key}")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Upload failed", error)
}
}
private void uploadFile() {
File exampleFile = new File(getApplicationContext().getFilesDir(), "ExampleKey");
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("ExampleKey", exampleFile);
rxUploadOperation
.observeResult()
.subscribe(
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),
error -> Log.e("MyAmplifyApp", "Upload failed", error)
);
}

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(), "ExampleKey");
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(
"ExampleKey",
exampleFile,
StorageUploadFileOptions.defaultInstance(),
progress -> Log.i("MyAmplifyApp", "Fraction completed: " + progress.getFractionCompleted()),
result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),
storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
);
}
private fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "ExampleKey")
exampleFile.writeText("Example file contents")
val options = StorageUploadFileOptions.defaultInstance()
Amplify.Storage.uploadFile("ExampleKey", exampleFile, options,
{ Log.i("MyAmplifyApp", "Fraction completed: ${it.fractionCompleted}") },
{ Log.i("MyAmplifyApp", "Successfully uploaded: ${it.key}") },
{ Log.e("MyAmplifyApp", "Upload failed", it) }
)
}
private suspend fun uploadFile() {
val exampleFile = File(applicationContext.filesDir, "ExampleKey")
exampleFile.writeText("Example file contents")
val options = StorageUploadFileOptions.defaultInstance()
val upload = Amplify.Storage.uploadFile("ExampleKey", 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.key}")
} catch (error: StorageException) {
Log.e("MyAmplifyApp", "Upload failed", error)
}
progressJob.cancel()
}
RxProgressAwareSingleOperation<StorageUploadFileResult> upload =
RxAmplify.Storage.uploadFile("exampleKey", exampleFile);
upload
.observeProgress()
.subscribe(
progress -> Log.i("MyAmplifyApp", progress.getFractionCompleted())
);