Page updated Jan 16, 2024

Upload files

Amplify Android v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Android to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

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:

1private void uploadInputStream() {
2 try {
3 InputStream exampleInputStream = getContentResolver().openInputStream(uri);
4
5 Amplify.Storage.uploadInputStream(
6 "ExampleKey",
7 exampleInputStream,
8 result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),
9 storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
10 );
11 } catch (FileNotFoundException error) {
12 Log.e("MyAmplifyApp", "Could not find file to open for input stream.", error);
13 }
14}
1private fun uploadInputStream(uri: Uri) {
2 val stream = contentResolver.openInputStream(uri)
3
4 Amplify.Storage.uploadInputStream("ExampleKey", stream,
5 { Log.i("MyAmplifyApp", "Successfully uploaded: ${it.key}") },
6 { Log.e("MyAmplifyApp", "Upload failed", it) }
7 )
8}
1private suspend fun uploadInputStream(uri: Uri) {
2 val stream = contentResolver.openInputStream(uri)
3
4 val upload = Amplify.Storage.uploadInputStream("ExampleKey", stream)
5 try {
6 val result = upload.result()
7 Log.i("MyAmplifyApp", "Successfully uploaded: ${result.key}.")
8 } catch (error: StorageException) {
9 Log.e("MyAmplifyApp", "Upload failed")
10 }
11}
1private void uploadInputStream() {
2 try {
3 InputStream exampleInputStream = getContentResolver().openInputStream(uri);
4
5 RxProgressAwareSingleOperation<StorageUploadInputStreamResult> rxUploadOperation =
6 RxAmplify.Storage.uploadInputStream("ExampleKey", exampleInputStream);
7
8 rxUploadOperation
9 .observeResult()
10 .subscribe(
11 result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),
12 error -> Log.e("MyAmplifyApp", "Upload failed", error)
13 );
14 } catch (FileNotFoundException error) {
15 Log.e("MyAmplifyApp", "Could not find file to open for input stream.", error);
16 }
17}

Upload files

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

1private void uploadFile() {
2 File exampleFile = new File(getApplicationContext().getFilesDir(), "ExampleKey");
3
4 try {
5 BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
6 writer.append("Example file contents");
7 writer.close();
8 } catch (Exception exception) {
9 Log.e("MyAmplifyApp", "Upload failed", exception);
10 }
11
12 Amplify.Storage.uploadFile(
13 "ExampleKey",
14 exampleFile,
15 result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),
16 storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
17 );
18}
1private fun uploadFile() {
2 val exampleFile = File(applicationContext.filesDir, "ExampleKey")
3 exampleFile.writeText("Example file contents")
4
5 Amplify.Storage.uploadFile("ExampleKey", exampleFile,
6 { Log.i("MyAmplifyApp", "Successfully uploaded: ${it.key}") },
7 { Log.e("MyAmplifyApp", "Upload failed", it) }
8 )
9}
1private suspend fun uploadFile() {
2 val exampleFile = File(applicationContext.filesDir, "ExampleKey")
3 exampleFile.writeText("Example file contents")
4
5 val upload = Amplify.Storage.uploadFile("ExampleKey", exampleFile)
6 try {
7 val result = upload.result()
8 Log.i("MyAmplifyApp", "Successfully uploaded: ${result.key}")
9 } catch (error: StorageException) {
10 Log.e("MyAmplifyApp", "Upload failed", error)
11 }
12}
1private void uploadFile() {
2 File exampleFile = new File(getApplicationContext().getFilesDir(), "ExampleKey");
3
4 try {
5 BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
6 writer.append("Example file contents");
7 writer.close();
8 } catch (Exception exception) {
9 Log.e("MyAmplifyApp", "Upload failed", exception);
10 }
11
12 RxProgressAwareSingleOperation<StorageUploadFileResult> rxUploadOperation =
13 RxAmplify.Storage.uploadFile("ExampleKey", exampleFile);
14
15 rxUploadOperation
16 .observeResult()
17 .subscribe(
18 result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),
19 error -> Log.e("MyAmplifyApp", "Upload failed", error)
20 );
21}

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

1private void uploadFile() {
2 File exampleFile = new File(getApplicationContext().getFilesDir(), "ExampleKey");
3
4 try {
5 BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));
6 writer.append("Example file contents");
7 writer.close();
8 } catch (Exception exception) {
9 Log.e("MyAmplifyApp", "Upload failed", exception);
10 }
11
12 Amplify.Storage.uploadFile(
13 "ExampleKey",
14 exampleFile,
15 StorageUploadFileOptions.defaultInstance(),
16 progress -> Log.i("MyAmplifyApp", "Fraction completed: " + progress.getFractionCompleted()),
17 result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),
18 storageFailure -> Log.e("MyAmplifyApp", "Upload failed", storageFailure)
19 );
20}
1private fun uploadFile() {
2 val exampleFile = File(applicationContext.filesDir, "ExampleKey")
3 exampleFile.writeText("Example file contents")
4
5 val options = StorageUploadFileOptions.defaultInstance()
6 Amplify.Storage.uploadFile("ExampleKey", exampleFile, options,
7 { Log.i("MyAmplifyApp", "Fraction completed: ${it.fractionCompleted}") },
8 { Log.i("MyAmplifyApp", "Successfully uploaded: ${it.key}") },
9 { Log.e("MyAmplifyApp", "Upload failed", it) }
10 )
11}
1private suspend fun uploadFile() {
2 val exampleFile = File(applicationContext.filesDir, "ExampleKey")
3 exampleFile.writeText("Example file contents")
4
5 val options = StorageUploadFileOptions.defaultInstance()
6 val upload = Amplify.Storage.uploadFile("ExampleKey", exampleFile, options)
7 val progressJob = activityScope.async {
8 upload.progress().collect {
9 Log.i("MyAmplifyApp", "Fraction completed: ${it.fractionCompleted}")
10 }
11 }
12 try {
13 val result = upload.result()
14 Log.i("MyAmplifyApp", "Successfully uploaded: ${result.key}")
15 } catch (error: StorageException) {
16 Log.e("MyAmplifyApp", "Upload failed", error)
17 }
18 progressJob.cancel()
19}
1RxProgressAwareSingleOperation<StorageUploadFileResult> upload =
2 RxAmplify.Storage.uploadFile("exampleKey", exampleFile);
3
4upload
5 .observeProgress()
6 .subscribe(
7 progress -> Log.i("MyAmplifyApp", progress.getFractionCompleted())
8 );