From a526bed5c299685dfedc3f3f30d36dfe4db3b92c Mon Sep 17 00:00:00 2001 From: Harshil Sharma Date: Thu, 15 Mar 2018 17:41:12 +0530 Subject: [PATCH] #BAEL-1549 Completed implementation of multipart upload (#3816) --- aws/pom.xml | 2 +- .../java/com/baeldung/s3/MultipartUpload.java | 56 +++++++++++++++++ .../com/baeldung/s3/MultipartUploadTest.java | 61 +++++++++++++++++++ 3 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 aws/src/main/java/com/baeldung/s3/MultipartUpload.java create mode 100644 aws/src/test/java/com/baeldung/s3/MultipartUploadTest.java diff --git a/aws/pom.xml b/aws/pom.xml index c66c420fae..33fc3b0cba 100644 --- a/aws/pom.xml +++ b/aws/pom.xml @@ -18,7 +18,7 @@ 1.3.0 1.1.0 2.8.0 - 1.11.154 + 1.11.290 4.12 2.8.9 3.8.0 diff --git a/aws/src/main/java/com/baeldung/s3/MultipartUpload.java b/aws/src/main/java/com/baeldung/s3/MultipartUpload.java new file mode 100644 index 0000000000..711046c112 --- /dev/null +++ b/aws/src/main/java/com/baeldung/s3/MultipartUpload.java @@ -0,0 +1,56 @@ +package com.baeldung.s3; + +import com.amazonaws.AmazonClientException; +import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; +import com.amazonaws.event.ProgressListener; +import com.amazonaws.regions.Regions; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3ClientBuilder; +import com.amazonaws.services.s3.model.PutObjectRequest; +import com.amazonaws.services.s3.transfer.TransferManager; +import com.amazonaws.services.s3.transfer.TransferManagerBuilder; +import com.amazonaws.services.s3.transfer.Upload; + +import java.io.File; +import java.util.concurrent.Executors; + +public class MultipartUpload { + + public static void main(String[] args) throws Exception { + String existingBucketName = "baeldung-bucket"; + String keyName = "my-picture.jpg"; + String filePath = "documents/my-picture.jpg"; + + AmazonS3 amazonS3 = AmazonS3ClientBuilder + .standard() + .withCredentials(new DefaultAWSCredentialsProviderChain()) + .withRegion(Regions.DEFAULT_REGION) + .build(); + + int maxUploadThreads = 5; + + TransferManager tm = TransferManagerBuilder + .standard() + .withS3Client(amazonS3) + .withMultipartUploadThreshold((long) (5 * 1024 * 1024)) + .withExecutorFactory(() -> Executors.newFixedThreadPool(maxUploadThreads)) + .build(); + + ProgressListener progressListener = + progressEvent -> System.out.println("Transferred bytes: " + progressEvent.getBytesTransferred()); + + PutObjectRequest request = new PutObjectRequest(existingBucketName, keyName, new File(filePath)); + + request.setGeneralProgressListener(progressListener); + + Upload upload = tm.upload(request); + + try { + upload.waitForCompletion(); + System.out.println("Upload complete."); + } catch (AmazonClientException e) { + System.out.println("Error occurred while uploading file"); + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/aws/src/test/java/com/baeldung/s3/MultipartUploadTest.java b/aws/src/test/java/com/baeldung/s3/MultipartUploadTest.java new file mode 100644 index 0000000000..bc8d517a0e --- /dev/null +++ b/aws/src/test/java/com/baeldung/s3/MultipartUploadTest.java @@ -0,0 +1,61 @@ +package com.baeldung.s3; + +import com.amazonaws.event.ProgressListener; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.model.PutObjectRequest; +import com.amazonaws.services.s3.model.PutObjectResult; +import com.amazonaws.services.s3.transfer.TransferManager; +import com.amazonaws.services.s3.transfer.TransferManagerBuilder; +import com.amazonaws.services.s3.transfer.Upload; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.util.concurrent.Executors; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class MultipartUploadTest { + + private static final String BUCKET_NAME = "bucket_name"; + private static final String KEY_NAME = "picture.jpg"; + + private AmazonS3 amazonS3; + private TransferManager tm; + private ProgressListener progressListener; + + @Before + public void setup() { + amazonS3 = mock(AmazonS3.class); + tm = TransferManagerBuilder + .standard() + .withS3Client(amazonS3) + .withMultipartUploadThreshold((long) (5 * 1024 * 1025)) + .withExecutorFactory(() -> Executors.newFixedThreadPool(5)) + .build(); + progressListener = + progressEvent -> System.out.println("Transferred bytes: " + progressEvent.getBytesTransferred()); + } + + @Test + public void whenUploadingFileWithTransferManager_thenVerifyUploadRequested() { + File file = mock(File.class); + PutObjectResult s3Result = mock(PutObjectResult.class); + + when(amazonS3.putObject(anyString(), anyString(), (File) any())).thenReturn(s3Result); + when(file.getName()).thenReturn(KEY_NAME); + + PutObjectRequest request = new PutObjectRequest(BUCKET_NAME, KEY_NAME, file); + request.setGeneralProgressListener(progressListener); + + Upload upload = tm.upload(request); + + assertThat(upload).isNotNull(); + verify(amazonS3).putObject(request); + } +}