diff --git a/aws/pom.xml b/aws/pom.xml index 8d60240c87..c66c420fae 100644 --- a/aws/pom.xml +++ b/aws/pom.xml @@ -18,9 +18,41 @@ 1.3.0 1.1.0 2.8.0 + 1.11.154 + 4.12 + 2.8.9 + 3.8.0 + + + com.amazonaws + aws-java-sdk + ${aws-java-sdk.version} + + + + junit + junit + ${junit.version} + test + + + + org.mockito + mockito-core + ${mockito-core.version} + test + + + + org.assertj + assertj-core + ${assertj-core.version} + test + + com.amazonaws aws-lambda-java-core diff --git a/aws/src/main/java/com/baeldung/s3/AWSS3Service.java b/aws/src/main/java/com/baeldung/s3/AWSS3Service.java new file mode 100644 index 0000000000..792e41a188 --- /dev/null +++ b/aws/src/main/java/com/baeldung/s3/AWSS3Service.java @@ -0,0 +1,87 @@ +package com.baeldung.s3; + +import java.io.File; +import java.util.List; + +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3Client; +import com.amazonaws.services.s3.model.Bucket; +import com.amazonaws.services.s3.model.CopyObjectResult; +import com.amazonaws.services.s3.model.DeleteObjectsRequest; +import com.amazonaws.services.s3.model.DeleteObjectsResult; +import com.amazonaws.services.s3.model.ObjectListing; +import com.amazonaws.services.s3.model.PutObjectResult; +import com.amazonaws.services.s3.model.S3Object; + +public class AWSS3Service { + private final AmazonS3 s3client; + + public AWSS3Service() { + this(new AmazonS3Client() { + }); + } + + public AWSS3Service(AmazonS3 s3client) { + this.s3client = s3client; + } + + //is bucket exist? + public boolean doesBucketExist(String bucketName) { + return s3client.doesBucketExist(bucketName); + } + + //create a bucket + public Bucket createBucket(String bucketName) { + return s3client.createBucket(bucketName); + } + + //list all buckets + public List listBuckets() { + return s3client.listBuckets(); + } + + //delete a bucket + public void deleteBucket(String bucketName) { + s3client.deleteBucket(bucketName); + } + + //uploading object + public PutObjectResult putObject(String bucketName, String key, File file) { + return s3client.putObject(bucketName, key, file); + } + + //listing objects + public ObjectListing listObjects(String bucketName) { + return s3client.listObjects(bucketName); + } + + //get an object + public S3Object getObject(String bucketName, String objectKey) { + return s3client.getObject(bucketName, objectKey); + } + + //copying an object + public CopyObjectResult copyObject( + String sourceBucketName, + String sourceKey, + String destinationBucketName, + String destinationKey + ) { + return s3client.copyObject( + sourceBucketName, + sourceKey, + destinationBucketName, + destinationKey + ); + } + + //deleting an object + public void deleteObject(String bucketName, String objectKey) { + s3client.deleteObject(bucketName, objectKey); + } + + //deleting multiple Objects + public DeleteObjectsResult deleteObjects(DeleteObjectsRequest delObjReq) { + return s3client.deleteObjects(delObjReq); + } +} diff --git a/aws/src/main/java/com/baeldung/s3/S3Application.java b/aws/src/main/java/com/baeldung/s3/S3Application.java new file mode 100644 index 0000000000..fcbd7811b5 --- /dev/null +++ b/aws/src/main/java/com/baeldung/s3/S3Application.java @@ -0,0 +1,108 @@ +package com.baeldung.s3; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +import com.amazonaws.auth.AWSCredentials; +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.regions.Regions; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3ClientBuilder; +import com.amazonaws.services.s3.model.Bucket; +import com.amazonaws.services.s3.model.DeleteObjectsRequest; +import com.amazonaws.services.s3.model.ObjectListing; +import com.amazonaws.services.s3.model.S3Object; +import com.amazonaws.services.s3.model.S3ObjectInputStream; +import com.amazonaws.services.s3.model.S3ObjectSummary; + +public class S3Application { + + private static final AWSCredentials credentials; + private static String bucketName; + + static { + //put your accesskey and secretkey here + credentials = new BasicAWSCredentials( + "", + "" + ); + } + + public static void main(String[] args) throws IOException { + //set-up the client + AmazonS3 s3client = AmazonS3ClientBuilder + .standard() + .withCredentials(new AWSStaticCredentialsProvider(credentials)) + .withRegion(Regions.US_EAST_2) + .build(); + + AWSS3Service awsService = new AWSS3Service(s3client); + + bucketName = "baeldung-bucket"; + + //creating a bucket + if(awsService.doesBucketExist(bucketName)) { + System.out.println("Bucket name is not available." + + " Try again with a different Bucket name."); + return; + } + awsService.createBucket(bucketName); + + //list all the buckets + for(Bucket s : awsService.listBuckets() ) { + System.out.println(s.getName()); + } + + //deleting bucket + awsService.deleteBucket("baeldung-bucket-test2"); + + //uploading object + awsService.putObject( + bucketName, + "Document/hello.txt", + new File("/Users/user/Document/hello.txt") + ); + + //listing objects + ObjectListing objectListing = awsService.listObjects(bucketName); + for(S3ObjectSummary os : objectListing.getObjectSummaries()) { + System.out.println(os.getKey()); + } + + //downloading an object + S3Object s3object = awsService.getObject(bucketName, "Document/hello.txt"); + S3ObjectInputStream inputStream = s3object.getObjectContent(); + FileOutputStream fos = new FileOutputStream(new File("/Users/user/Desktop/hello.txt")); + + int read = 0; + byte[] bytes = new byte[1024]; + while ((read = inputStream.read(bytes)) != -1) { + fos.write(bytes, 0, read); + } + inputStream.close(); + fos.close(); + + //copying an object + awsService.copyObject( + "baeldung-bucket", + "picture/pic.png", + "baeldung-bucket2", + "Document/picture.png" + ); + + //deleting an object + awsService.deleteObject(bucketName, "Document/hello.txt"); + + //deleting multiple objects + String objkeyArr[] = { + "Document/hello2.txt", + "Document/picture.png" + }; + + DeleteObjectsRequest delObjReq = new DeleteObjectsRequest("baeldung-bucket") + .withKeys(objkeyArr); + awsService.deleteObjects(delObjReq); + } +} diff --git a/aws/src/test/java/com/baeldung/s3/AWSS3ServiceIntegrationTest.java b/aws/src/test/java/com/baeldung/s3/AWSS3ServiceIntegrationTest.java new file mode 100644 index 0000000000..d386704513 --- /dev/null +++ b/aws/src/test/java/com/baeldung/s3/AWSS3ServiceIntegrationTest.java @@ -0,0 +1,113 @@ +package com.baeldung.s3; + +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; + +import java.io.File; + +import org.junit.Before; +import org.junit.Test; + +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.model.CopyObjectResult; +import com.amazonaws.services.s3.model.DeleteObjectsRequest; +import com.amazonaws.services.s3.model.DeleteObjectsResult; +import com.amazonaws.services.s3.model.PutObjectResult; + +public class AWSS3ServiceIntegrationTest { + + private static final String BUCKET_NAME = "bucket_name"; + private static final String KEY_NAME = "key_name"; + private static final String BUCKET_NAME2 = "bucket_name2"; + private static final String KEY_NAME2 = "key_name2"; + + private AmazonS3 s3; + private AWSS3Service service; + + @Before + public void setUp() { + s3 = mock(AmazonS3.class); + service = new AWSS3Service(s3); + } + + @Test + public void whenInitializingAWSS3Service_thenNotNull() { + assertThat(new AWSS3Service()).isNotNull(); + } + + @Test + public void whenVerifyingIfS3BucketExist_thenCorrect() { + service.doesBucketExist(BUCKET_NAME); + verify(s3).doesBucketExist(BUCKET_NAME); + } + + @Test + public void whenVerifyingCreationOfS3Bucket_thenCorrect() { + service.createBucket(BUCKET_NAME); + verify(s3).createBucket(BUCKET_NAME); + } + + @Test + public void whenVerifyingListBuckets_thenCorrect() { + service.listBuckets(); + verify(s3).listBuckets(); + } + + @Test + public void whenDeletingBucket_thenCorrect() { + service.deleteBucket(BUCKET_NAME); + verify(s3).deleteBucket(BUCKET_NAME); + } + + @Test + public void whenVerifyingPutObject_thenCorrect() { + File file = mock(File.class); + PutObjectResult result = mock(PutObjectResult.class); + when(s3.putObject(anyString(), anyString(), (File) any())).thenReturn(result); + + assertThat(service.putObject(BUCKET_NAME, KEY_NAME, file)).isEqualTo(result); + verify(s3).putObject(BUCKET_NAME, KEY_NAME, file); + } + + @Test + public void whenVerifyingListObjects_thenCorrect() { + service.listObjects(BUCKET_NAME); + verify(s3).listObjects(BUCKET_NAME); + } + + @Test + public void whenVerifyingGetObject_thenCorrect() { + service.getObject(BUCKET_NAME, KEY_NAME); + verify(s3).getObject(BUCKET_NAME, KEY_NAME); + } + + @Test + public void whenVerifyingCopyObject_thenCorrect() { + CopyObjectResult result = mock(CopyObjectResult.class); + when(s3.copyObject(anyString(), anyString(), anyString(), anyString())).thenReturn(result); + + assertThat(service.copyObject(BUCKET_NAME, KEY_NAME, BUCKET_NAME2, KEY_NAME2)).isEqualTo(result); + verify(s3).copyObject(BUCKET_NAME, KEY_NAME, BUCKET_NAME2, KEY_NAME2); + } + + @Test + public void whenVerifyingDeleteObject_thenCorrect() { + service.deleteObject(BUCKET_NAME, KEY_NAME); + verify(s3).deleteObject(BUCKET_NAME, KEY_NAME); + } + + @Test + public void whenVerifyingDeleteObjects_thenCorrect() { + DeleteObjectsRequest request = mock(DeleteObjectsRequest.class); + DeleteObjectsResult result = mock(DeleteObjectsResult.class); + when(s3.deleteObjects((DeleteObjectsRequest)any())).thenReturn(result); + + assertThat(service.deleteObjects(request)).isEqualTo(result); + verify(s3).deleteObjects(request); + } + +}