BAEL-787 AWS S3 with Java (#2179)

* Files - S3 Bucket Creation and Object Upload

S3 Bucket Creation and Object Upload Using the AWS SDK for Java.
Please note. This code is not readily plug-and-play type. User needs to put his own 'access key' and 'secret key'

* S3 Bucket Creation and Object Upload-dependencies

S3 Bucket Creation and Object Upload - Adding Dependencies

* Update S3Application.java

* Update S3Application.java

* Update S3Application.java

* Fixed Test Names

* Update S3Application.java

* Added more test cases

* Added more examples

* Delete AWSS3Service.java

* Delete S3Application.java

* Added more examples

* Added more test cases
This commit is contained in:
ramansahasi 2017-07-13 19:02:32 +05:30 committed by maibin
parent 78de955aac
commit 24b14e55b7
4 changed files with 340 additions and 0 deletions

View File

@ -18,9 +18,41 @@
<aws-lambda-java-events.version>1.3.0</aws-lambda-java-events.version>
<aws-lambda-java-core.version>1.1.0</aws-lambda-java-core.version>
<gson.version>2.8.0</gson.version>
<aws-java-sdk.version>1.11.154</aws-java-sdk.version>
<junit.version>4.12</junit.version>
<mockito-core.version>2.8.9</mockito-core.version>
<assertj-core.version>3.8.0</assertj-core.version>
</properties>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>${aws-java-sdk.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>

View File

@ -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<Bucket> 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);
}
}

View File

@ -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(
"<AWS accesskey>",
"<AWS secretkey>"
);
}
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);
}
}

View File

@ -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);
}
}