Upload files
Amplify allows you to upload File
s and InputStream
s.
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 rxUploadOperation9 .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 rxUploadOperation16 .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
4upload5 .observeProgress()6 .subscribe(7 progress -> Log.i("MyAmplifyApp", progress.getFractionCompleted())8 );
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.
1private void uploadFile() {2 File exampleFile = new File(getApplicationContext().getFilesDir(), "ExampleKey");3 try {4 BufferedWriter writer = new BufferedWriter(new FileWriter(exampleFile));5 writer.append("Example file contents");6 writer.close();7 } catch (Exception exception) {8 Log.e("MyAmplifyApp", "Upload failed", exception);9 }10
11 // Create metadata12 Map<String, String> userMetadata = new HashMap<>();13 userMetadata.put("myKey", "myVal");14
15 // Configure upload options with metadata16 StorageUploadFileOptions options = StorageUploadFileOptions.builder()17 .metadata(userMetadata)18 .build();19
20 // Perform the upload21 Amplify.Storage.uploadFile(22 "ExampleKey",23 exampleFile,24 options,25 result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),26 error -> Log.e("MyAmplifyApp", "Upload failed", error)27 );28}
1fun uploadFile() {2 val exampleFile = File(applicationContext.filesDir, "ExampleFileName")3 exampleFile.writeText("Example file contents")4
5 // Create metadata6 val userMetadata: MutableMap<String, String> = HashMap()7 userMetadata["myKey"] = "myVal"8
9 // Configure upload options with metadata10 val options = StorageUploadFileOptions.builder()11 .metadata(userMetadata)12 .build()13
14 // Perform the upload15 Amplify.Storage.uploadFile(16 "ExampleKey",17 exampleFile,18 options,19 { result -> Log.i("MyAmplifyApp", "Successfully uploaded: ${result.key}") },20 { error -> Log.e("MyAmplifyApp", "Upload failed", error) }21 )22}
1fun uploadFile() {2 val exampleFile = File(applicationContext.filesDir, "ExampleFileName")3 exampleFile.writeText("Example file contents")4
5 // Create metadata6 val userMetadata: MutableMap<String, String> = HashMap()7 userMetadata["myKey"] = "myVal"8
9 // Configure upload options with metadata10 val options = StorageUploadFileOptions.builder()11 .metadata(userMetadata)12 .build()13
14 val upload = Amplify.Storage.uploadFile("ExampleKey", exampleFile, options)15 val progressJob = activityScope.async {16 upload.progress().collect {17 Log.i("MyAmplifyApp", "Fraction completed: ${it.fractionCompleted}")18 }19 }20 try {21 val result = upload.result()22 Log.i("MyAmplifyApp", "Successfully uploaded: ${result.key}")23 } catch (error: StorageException) {24 Log.e("MyAmplifyApp", "Upload failed", error)25 }26 progressJob.cancel()27}
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 Map<String, String> userMetadata = new HashMap<>();13 userMetadata.put("myKey", "myVal");14
15 StorageUploadFileOptions options = StorageUploadFileOptions.builder()16 .metadata(userMetadata)17 .build();18
19 RxStorageBinding.RxProgressAwareSingleOperation<StorageUploadFileResult> rxUploadOperation =20 RxAmplify.Storage.uploadFile("ExampleKey", exampleFile, options);21
22 rxUploadOperation23 .observeResult()24 .subscribe(25 result -> Log.i("MyAmplifyApp", "Successfully uploaded: " + result.getKey()),26 error -> Log.e("MyAmplifyApp", "Upload failed", error)27 );28}
MultiPart upload
Amplify will automatically perform a 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