From d8d0f2544f85b5d144074013290b2b933eae8ad5 Mon Sep 17 00:00:00 2001 From: giuliano Date: Mon, 15 Nov 2021 10:33:26 -0300 Subject: [PATCH 1/4] Create test for generatePresignedUrl method --- .../com/ensolvers/fox/s3/S3ServiceTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/modules/fox-s3/src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java b/modules/fox-s3/src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java index 2e80578..21a6a18 100644 --- a/modules/fox-s3/src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java +++ b/modules/fox-s3/src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java @@ -96,4 +96,27 @@ public void testS3() throws Exception { keys = service.list(bucket, folderName); assertFalse(keys.isEmpty()); } + + @Test + public void shouldGeneratePresignedUrl() { + String bucketName = "foxtest"; + String keyName = "t1"; + Long secondsToExpire = 20L; + String fileName = "f1"; + + AmazonS3Client client = (AmazonS3Client) AmazonS3ClientBuilder.standard() + .withEndpointConfiguration(localstack.getEndpointConfiguration(LocalStackContainer.Service.S3)) + .withCredentials(localstack.getDefaultCredentialsProvider()).build(); + + S3Service service = new S3Service(client); + + String presignedUrl = service.generatePresignedUrl(bucketName, keyName, secondsToExpire, fileName); + + assertNotNull(presignedUrl); + assertFalse(presignedUrl.isEmpty()); + assertFalse(presignedUrl.isBlank()); + + System.out.print("\n\npresignedUrl: " + presignedUrl + "\n\n"); + + } } From cbceb78e61160e86468f96c2d923cd41c28c6113 Mon Sep 17 00:00:00 2001 From: giuliano Date: Mon, 15 Nov 2021 13:16:29 -0300 Subject: [PATCH 2/4] Fix slf4j error --- modules/fox-s3/pom.xml | 6 +++ .../java/com/ensolvers/fox/s3/S3Service.java | 3 +- .../com/ensolvers/fox/s3/S3ServiceTest.java | 50 +++++++++---------- pom.xml | 8 ++- 4 files changed, 39 insertions(+), 28 deletions(-) diff --git a/modules/fox-s3/pom.xml b/modules/fox-s3/pom.xml index 070d3e4..b6f6120 100644 --- a/modules/fox-s3/pom.xml +++ b/modules/fox-s3/pom.xml @@ -65,6 +65,12 @@ org.slf4j slf4j-api + + + org.slf4j + slf4j-simple + + diff --git a/modules/fox-s3/src/main/java/com/ensolvers/fox/s3/S3Service.java b/modules/fox-s3/src/main/java/com/ensolvers/fox/s3/S3Service.java index 99ebf57..70d383c 100644 --- a/modules/fox-s3/src/main/java/com/ensolvers/fox/s3/S3Service.java +++ b/modules/fox-s3/src/main/java/com/ensolvers/fox/s3/S3Service.java @@ -67,7 +67,8 @@ public String generatePresignedUrl(String bucketName, String keyName, Long secon generatePresignedUrlRequest.setResponseHeaders(responseHeaders); URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest); - logger.info(LOG_PREFIX + "[END] Generating a presigned URL"); + + logger.info(LOG_PREFIX + "[END] Generating a presigned URL: " + url); return url.toString(); } diff --git a/modules/fox-s3/src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java b/modules/fox-s3/src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java index 21a6a18..caa4053 100644 --- a/modules/fox-s3/src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java +++ b/modules/fox-s3/src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java @@ -23,6 +23,7 @@ import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import java.io.*; +import java.nio.charset.StandardCharsets; import java.util.List; import org.apache.commons.io.FileUtils; import org.junit.jupiter.api.Test; @@ -38,6 +39,10 @@ */ @Testcontainers public class S3ServiceTest { + // Test constants + private final String BUCKET_NAME = "foxtest"; + private final String KEY = "t1"; + private final String FILENAME = "ensolversfox"; @Container public LocalStackContainer localstack = new LocalStackContainer(DockerImageName.parse("localstack/localstack:0.11.3")) @@ -45,10 +50,9 @@ public class S3ServiceTest { @Test public void testS3() throws Exception { - String bucket = "foxtest"; String testData = "this is a sample test data"; - String key = "t1"; String folderName = "f1"; + String fileSuffix = ".txt"; AmazonS3Client client = (AmazonS3Client) AmazonS3ClientBuilder.standard() .withEndpointConfiguration(localstack.getEndpointConfiguration(LocalStackContainer.Service.S3)) @@ -56,53 +60,50 @@ public void testS3() throws Exception { S3Service service = new S3Service(client); // prepares the bucket - client.createBucket(bucket); + client.createBucket(BUCKET_NAME); // read non existent file - File file = service.get(bucket, key); + File file = service.get(BUCKET_NAME, KEY); assertNull(file); // no fail - service.delete(bucket, key); + service.delete(BUCKET_NAME, KEY); // write file in1 root context - File f = File.createTempFile("ensolversfox", ".txt"); - FileUtils.writeStringToFile(f, testData, "UTF8"); - service.put(bucket, key, f); + File f = File.createTempFile(FILENAME, fileSuffix); + FileUtils.writeStringToFile(f, testData, StandardCharsets.UTF_8); + service.put(BUCKET_NAME, KEY, f); f.delete(); - // read existant file - file = service.get(bucket, key); + // read existent file + file = service.get(BUCKET_NAME, KEY); assertNotNull(file); - String contents = FileUtils.readFileToString(file, "UTF8"); + String contents = FileUtils.readFileToString(file, StandardCharsets.UTF_8); assertEquals(testData, contents); // no fail - service.delete(bucket, key); + service.delete(BUCKET_NAME, KEY); - file = service.get(bucket, key); + file = service.get(BUCKET_NAME, KEY); assertNull(file); - List keys = service.list(bucket, folderName); + List keys = service.list(BUCKET_NAME, folderName); assertTrue(keys.isEmpty()); // write file in folder - f = File.createTempFile("ensolversfox", ".txt"); - FileUtils.writeStringToFile(f, testData, "UTF8"); - service.put(bucket, folderName + "/" + key, f); + f = File.createTempFile(FILENAME, fileSuffix); + FileUtils.writeStringToFile(f, testData, StandardCharsets.UTF_8); + service.put(BUCKET_NAME, folderName + "/" + KEY, f); f.delete(); // now folder shouldn't be empty - keys = service.list(bucket, folderName); + keys = service.list(BUCKET_NAME, folderName); assertFalse(keys.isEmpty()); } @Test public void shouldGeneratePresignedUrl() { - String bucketName = "foxtest"; - String keyName = "t1"; - Long secondsToExpire = 20L; - String fileName = "f1"; + Long secondsToExpire = 60L; AmazonS3Client client = (AmazonS3Client) AmazonS3ClientBuilder.standard() .withEndpointConfiguration(localstack.getEndpointConfiguration(LocalStackContainer.Service.S3)) @@ -110,13 +111,10 @@ public void shouldGeneratePresignedUrl() { S3Service service = new S3Service(client); - String presignedUrl = service.generatePresignedUrl(bucketName, keyName, secondsToExpire, fileName); + String presignedUrl = service.generatePresignedUrl(BUCKET_NAME, KEY, secondsToExpire, FILENAME); assertNotNull(presignedUrl); assertFalse(presignedUrl.isEmpty()); assertFalse(presignedUrl.isBlank()); - - System.out.print("\n\npresignedUrl: " + presignedUrl + "\n\n"); - } } diff --git a/pom.xml b/pom.xml index 3db3f46..89d51eb 100644 --- a/pom.xml +++ b/pom.xml @@ -152,6 +152,12 @@ slf4j-api ${slf4j.version} + + + org.slf4j + slf4j-simple + ${slf4j.version} + commons-io @@ -376,4 +382,4 @@ modules/fox-spring - \ No newline at end of file + From 08fa1133439fc9dd861e9218650b09566989189b Mon Sep 17 00:00:00 2001 From: giuliano Date: Mon, 15 Nov 2021 13:24:05 -0300 Subject: [PATCH 3/4] Minor change --- .../src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/fox-s3/src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java b/modules/fox-s3/src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java index caa4053..4ed6c44 100644 --- a/modules/fox-s3/src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java +++ b/modules/fox-s3/src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java @@ -23,7 +23,6 @@ import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import java.io.*; -import java.nio.charset.StandardCharsets; import java.util.List; import org.apache.commons.io.FileUtils; import org.junit.jupiter.api.Test; @@ -53,6 +52,7 @@ public void testS3() throws Exception { String testData = "this is a sample test data"; String folderName = "f1"; String fileSuffix = ".txt"; + String charset = "UTF8"; AmazonS3Client client = (AmazonS3Client) AmazonS3ClientBuilder.standard() .withEndpointConfiguration(localstack.getEndpointConfiguration(LocalStackContainer.Service.S3)) @@ -71,14 +71,14 @@ public void testS3() throws Exception { // write file in1 root context File f = File.createTempFile(FILENAME, fileSuffix); - FileUtils.writeStringToFile(f, testData, StandardCharsets.UTF_8); + FileUtils.writeStringToFile(f, testData, charset); service.put(BUCKET_NAME, KEY, f); f.delete(); // read existent file file = service.get(BUCKET_NAME, KEY); assertNotNull(file); - String contents = FileUtils.readFileToString(file, StandardCharsets.UTF_8); + String contents = FileUtils.readFileToString(file, charset); assertEquals(testData, contents); // no fail @@ -92,7 +92,7 @@ public void testS3() throws Exception { // write file in folder f = File.createTempFile(FILENAME, fileSuffix); - FileUtils.writeStringToFile(f, testData, StandardCharsets.UTF_8); + FileUtils.writeStringToFile(f, testData, charset); service.put(BUCKET_NAME, folderName + "/" + KEY, f); f.delete(); From 1e5b5b84f7e8d8f9cb27f77eb13df38015b6d6bc Mon Sep 17 00:00:00 2001 From: giuliano Date: Mon, 15 Nov 2021 13:32:07 -0300 Subject: [PATCH 4/4] Format code --- .../java/com/ensolvers/fox/s3/S3Service.java | 385 ++++++++++-------- .../com/ensolvers/fox/s3/S3ServiceTest.java | 167 ++++---- 2 files changed, 296 insertions(+), 256 deletions(-) diff --git a/modules/fox-s3/src/main/java/com/ensolvers/fox/s3/S3Service.java b/modules/fox-s3/src/main/java/com/ensolvers/fox/s3/S3Service.java index 70d383c..6084eb2 100644 --- a/modules/fox-s3/src/main/java/com/ensolvers/fox/s3/S3Service.java +++ b/modules/fox-s3/src/main/java/com/ensolvers/fox/s3/S3Service.java @@ -38,181 +38,212 @@ */ public class S3Service { - private static Logger logger = LoggerFactory.getLogger(S3Service.class); - private static String LOG_PREFIX = "[AWS-S3-STORAGE]"; - - private final AmazonS3Client s3Client; - - public S3Service(AmazonS3Client s3Client) { - this.s3Client = s3Client; - } - - /** - * Generates a temporary URL to download a file from a private S3 bucket - * - * @param bucketName the bucket - * @param keyName the path to the file - * @param secondsToExpire the expiration time (in seconds) - * @param fileName the filename to be downloaded with - * - * @return the temporary URL to download the object - */ - public String generatePresignedUrl(String bucketName, String keyName, Long secondsToExpire, String fileName) { - logger.info(LOG_PREFIX + "[START] Generating a presigned URL"); - GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, keyName) - .withExpiration(new Date(new Date().getTime() + secondsToExpire * 1000)); - // Generates a header with the name for the file to be downloaded with - ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides(); - responseHeaders.setContentDisposition("attachment; filename =\"" + fileName + "\""); - generatePresignedUrlRequest.setResponseHeaders(responseHeaders); - - URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest); - - logger.info(LOG_PREFIX + "[END] Generating a presigned URL: " + url); - return url.toString(); - } - - /** - * Sets the contents of file into the bucketName/keyName - * - * @param bucketName the bucket - * @param keyName the path to the file - * @param file the file to be uploaded - */ - public void put(String bucketName, String keyName, File file) { - try { - logger.info(LOG_PREFIX + "[START] Uploading a new object to S3 from a file"); - - PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, file); - s3Client.putObject(putObjectRequest); - - logger.info(LOG_PREFIX + "[END] Uploading a new object to S3 from a file"); - } catch (AmazonServiceException ase) { - logger.error(LOG_PREFIX + " Caught an AmazonServiceException, which " + "means your request made it " - + "to Amazon S3, but was rejected with an error response" + " for some reason.", ase); - } catch (AmazonClientException ace) { - logger.error(LOG_PREFIX + " Caught an AmazonClientException, which " + "means the client encountered " - + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network.", - ace); - } - } - - /** - * Sets the contents of the MultipartFile into the bucketName/keyName This - * overload of the put method simplifies the image uploading process to S3 - * - * @param bucketName the bucket - * @param keyName the path to the file - */ - public String put(String bucketName, String keyName, InputStream inputStream, long size, boolean isPublicRead) { - logger.info("{}[START] Uploading a new object to S3 from a file", LOG_PREFIX); - - var objectMetadata = new ObjectMetadata(); - objectMetadata.setContentLength(size); - var request = new PutObjectRequest(bucketName, keyName, inputStream, objectMetadata); - - if (isPublicRead) { - request.setCannedAcl(CannedAccessControlList.PublicRead); - } - - s3Client.putObject(request); - - logger.info("{}[END] Uploading a new object to S3 from a file", LOG_PREFIX); - - return String.format("https://%s.s3.amazonaws.com/%s", bucketName, keyName); - } - - /** - * Gets the contents of the file in bucketName/keyName - * - * @param bucketName the bucketName - * @param keyName the keyName - * - * @return returns a local copy of the file in a temp directory - */ - public File get(String bucketName, String keyName) { - try { - logger.info(LOG_PREFIX + "[START] Getting data of object of S3"); - - GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, keyName); - S3Object s3Object = s3Client.getObject(getObjectRequest); - - logger.info(LOG_PREFIX + "[END] Getting data of object of S3"); - - File tmpFile = File.createTempFile("fox", "s3"); - S3ObjectInputStream inputStream = s3Object.getObjectContent(); - FileOutputStream fileOutputStream = new FileOutputStream(tmpFile); - - IOUtils.copy(inputStream, fileOutputStream); - IOUtils.closeQuietly(inputStream, null); - IOUtils.closeQuietly(fileOutputStream, null); - - return tmpFile; - } catch (AmazonServiceException ase) { - logger.error(LOG_PREFIX + " Caught an AmazonServiceException, which " + "means your request made it " - + "to Amazon S3, but was rejected with an error response" + " for some reason.", ase); - } catch (AmazonClientException ace) { - logger.error(LOG_PREFIX + " Caught an AmazonClientException, which " + "means the client encountered " - + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network.", - ace); - } catch (FileNotFoundException e) { - logger.error(LOG_PREFIX + " not found", e); - } catch (IOException e) { - logger.error(LOG_PREFIX + " io error", e); - } - - return null; - } - - /** - * Delete the object in bucketName/keyName - * - * @param bucketName the bucketName - * @param keyName the keyName - */ - public void delete(String bucketName, String keyName) { - logger.info(LOG_PREFIX + "[START] Deleting a object of S3"); - - try { - DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucketName, keyName); - s3Client.deleteObject(deleteObjectRequest); - - logger.info(LOG_PREFIX + "[END] Deleting a object of S3"); - } catch (AmazonServiceException ase) { - logger.error(LOG_PREFIX + " Caught an AmazonServiceException." + "Error Message: " + ase.getMessage() + "HTTP Status Code: " - + ase.getStatusCode() + "AWS Error Code: " + ase.getErrorCode() + "Error Type: " + ase.getErrorType() - + "Request ID: " + ase.getRequestId()); - } catch (AmazonClientException ace) { - logger.error(LOG_PREFIX + " Caught an AmazonClientException." + "Error Message: " + ace.getMessage()); - } - } - - /** - * List all the files in buckey under the folderKey, returning the list of file - * names - * - * @param bucket the name of the buckey - * @param folderKey the name of the container - * - * @return the list of file names - */ - public List list(String bucket, String folderKey) { - ListObjectsV2Request request = new ListObjectsV2Request().withBucketName(bucket).withPrefix(folderKey); - - ListObjectsV2Result result; - List keys = new ArrayList<>(); - do { - result = this.s3Client.listObjectsV2(request); - - for (S3ObjectSummary objectSummary : result.getObjectSummaries()) { - keys.add(objectSummary.getKey()); - } - - String token = result.getNextContinuationToken(); - request.setContinuationToken(token); - - } while (result.isTruncated()); - - return keys; - } + private static Logger logger = LoggerFactory.getLogger(S3Service.class); + private static String LOG_PREFIX = "[AWS-S3-STORAGE]"; + + private final AmazonS3Client s3Client; + + public S3Service(AmazonS3Client s3Client) { + this.s3Client = s3Client; + } + + /** + * Generates a temporary URL to download a file from a private S3 bucket + * + * @param bucketName the bucket + * @param keyName the path to the file + * @param secondsToExpire the expiration time (in seconds) + * @param fileName the filename to be downloaded with + * @return the temporary URL to download the object + */ + public String generatePresignedUrl( + String bucketName, String keyName, Long secondsToExpire, String fileName) { + logger.info(LOG_PREFIX + "[START] Generating a presigned URL"); + GeneratePresignedUrlRequest generatePresignedUrlRequest = + new GeneratePresignedUrlRequest(bucketName, keyName) + .withExpiration(new Date(new Date().getTime() + secondsToExpire * 1000)); + // Generates a header with the name for the file to be downloaded with + ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides(); + responseHeaders.setContentDisposition("attachment; filename =\"" + fileName + "\""); + generatePresignedUrlRequest.setResponseHeaders(responseHeaders); + + URL url = s3Client.generatePresignedUrl(generatePresignedUrlRequest); + + logger.info(LOG_PREFIX + "[END] Generating a presigned URL: " + url); + return url.toString(); + } + + /** + * Sets the contents of file into the bucketName/keyName + * + * @param bucketName the bucket + * @param keyName the path to the file + * @param file the file to be uploaded + */ + public void put(String bucketName, String keyName, File file) { + try { + logger.info(LOG_PREFIX + "[START] Uploading a new object to S3 from a file"); + + PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, file); + s3Client.putObject(putObjectRequest); + + logger.info(LOG_PREFIX + "[END] Uploading a new object to S3 from a file"); + } catch (AmazonServiceException ase) { + logger.error( + LOG_PREFIX + + " Caught an AmazonServiceException, which " + + "means your request made it " + + "to Amazon S3, but was rejected with an error response" + + " for some reason.", + ase); + } catch (AmazonClientException ace) { + logger.error( + LOG_PREFIX + + " Caught an AmazonClientException, which " + + "means the client encountered " + + "an internal error while trying to " + + "communicate with S3, " + + "such as not being able to access the network.", + ace); + } + } + + /** + * Sets the contents of the MultipartFile into the bucketName/keyName This overload of the put + * method simplifies the image uploading process to S3 + * + * @param bucketName the bucket + * @param keyName the path to the file + */ + public String put( + String bucketName, String keyName, InputStream inputStream, long size, boolean isPublicRead) { + logger.info("{}[START] Uploading a new object to S3 from a file", LOG_PREFIX); + + var objectMetadata = new ObjectMetadata(); + objectMetadata.setContentLength(size); + var request = new PutObjectRequest(bucketName, keyName, inputStream, objectMetadata); + + if (isPublicRead) { + request.setCannedAcl(CannedAccessControlList.PublicRead); + } + + s3Client.putObject(request); + + logger.info("{}[END] Uploading a new object to S3 from a file", LOG_PREFIX); + + return String.format("https://%s.s3.amazonaws.com/%s", bucketName, keyName); + } + + /** + * Gets the contents of the file in bucketName/keyName + * + * @param bucketName the bucketName + * @param keyName the keyName + * @return returns a local copy of the file in a temp directory + */ + public File get(String bucketName, String keyName) { + try { + logger.info(LOG_PREFIX + "[START] Getting data of object of S3"); + + GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, keyName); + S3Object s3Object = s3Client.getObject(getObjectRequest); + + logger.info(LOG_PREFIX + "[END] Getting data of object of S3"); + + File tmpFile = File.createTempFile("fox", "s3"); + S3ObjectInputStream inputStream = s3Object.getObjectContent(); + FileOutputStream fileOutputStream = new FileOutputStream(tmpFile); + + IOUtils.copy(inputStream, fileOutputStream); + IOUtils.closeQuietly(inputStream, null); + IOUtils.closeQuietly(fileOutputStream, null); + + return tmpFile; + } catch (AmazonServiceException ase) { + logger.error( + LOG_PREFIX + + " Caught an AmazonServiceException, which " + + "means your request made it " + + "to Amazon S3, but was rejected with an error response" + + " for some reason.", + ase); + } catch (AmazonClientException ace) { + logger.error( + LOG_PREFIX + + " Caught an AmazonClientException, which " + + "means the client encountered " + + "an internal error while trying to " + + "communicate with S3, " + + "such as not being able to access the network.", + ace); + } catch (FileNotFoundException e) { + logger.error(LOG_PREFIX + " not found", e); + } catch (IOException e) { + logger.error(LOG_PREFIX + " io error", e); + } + + return null; + } + + /** + * Delete the object in bucketName/keyName + * + * @param bucketName the bucketName + * @param keyName the keyName + */ + public void delete(String bucketName, String keyName) { + logger.info(LOG_PREFIX + "[START] Deleting a object of S3"); + + try { + DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucketName, keyName); + s3Client.deleteObject(deleteObjectRequest); + + logger.info(LOG_PREFIX + "[END] Deleting a object of S3"); + } catch (AmazonServiceException ase) { + logger.error( + LOG_PREFIX + + " Caught an AmazonServiceException." + + "Error Message: " + + ase.getMessage() + + "HTTP Status Code: " + + ase.getStatusCode() + + "AWS Error Code: " + + ase.getErrorCode() + + "Error Type: " + + ase.getErrorType() + + "Request ID: " + + ase.getRequestId()); + } catch (AmazonClientException ace) { + logger.error( + LOG_PREFIX + " Caught an AmazonClientException." + "Error Message: " + ace.getMessage()); + } + } + + /** + * List all the files in buckey under the folderKey, returning the list of file names + * + * @param bucket the name of the buckey + * @param folderKey the name of the container + * @return the list of file names + */ + public List list(String bucket, String folderKey) { + ListObjectsV2Request request = + new ListObjectsV2Request().withBucketName(bucket).withPrefix(folderKey); + + ListObjectsV2Result result; + List keys = new ArrayList<>(); + do { + result = this.s3Client.listObjectsV2(request); + + for (S3ObjectSummary objectSummary : result.getObjectSummaries()) { + keys.add(objectSummary.getKey()); + } + + String token = result.getNextContinuationToken(); + request.setContinuationToken(token); + + } while (result.isTruncated()); + + return keys; + } } diff --git a/modules/fox-s3/src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java b/modules/fox-s3/src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java index 4ed6c44..b5a5522 100644 --- a/modules/fox-s3/src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java +++ b/modules/fox-s3/src/test/java/com/ensolvers/fox/s3/S3ServiceTest.java @@ -38,83 +38,92 @@ */ @Testcontainers public class S3ServiceTest { - // Test constants - private final String BUCKET_NAME = "foxtest"; - private final String KEY = "t1"; - private final String FILENAME = "ensolversfox"; - - @Container - public LocalStackContainer localstack = new LocalStackContainer(DockerImageName.parse("localstack/localstack:0.11.3")) - .withServices(LocalStackContainer.Service.S3); - - @Test - public void testS3() throws Exception { - String testData = "this is a sample test data"; - String folderName = "f1"; - String fileSuffix = ".txt"; - String charset = "UTF8"; - - AmazonS3Client client = (AmazonS3Client) AmazonS3ClientBuilder.standard() - .withEndpointConfiguration(localstack.getEndpointConfiguration(LocalStackContainer.Service.S3)) - .withCredentials(localstack.getDefaultCredentialsProvider()).build(); - - S3Service service = new S3Service(client); - // prepares the bucket - client.createBucket(BUCKET_NAME); - - // read non existent file - File file = service.get(BUCKET_NAME, KEY); - assertNull(file); - - // no fail - service.delete(BUCKET_NAME, KEY); - - // write file in1 root context - File f = File.createTempFile(FILENAME, fileSuffix); - FileUtils.writeStringToFile(f, testData, charset); - service.put(BUCKET_NAME, KEY, f); - f.delete(); - - // read existent file - file = service.get(BUCKET_NAME, KEY); - assertNotNull(file); - String contents = FileUtils.readFileToString(file, charset); - assertEquals(testData, contents); - - // no fail - service.delete(BUCKET_NAME, KEY); - - file = service.get(BUCKET_NAME, KEY); - assertNull(file); - - List keys = service.list(BUCKET_NAME, folderName); - assertTrue(keys.isEmpty()); - - // write file in folder - f = File.createTempFile(FILENAME, fileSuffix); - FileUtils.writeStringToFile(f, testData, charset); - service.put(BUCKET_NAME, folderName + "/" + KEY, f); - f.delete(); - - // now folder shouldn't be empty - keys = service.list(BUCKET_NAME, folderName); - assertFalse(keys.isEmpty()); - } - - @Test - public void shouldGeneratePresignedUrl() { - Long secondsToExpire = 60L; - - AmazonS3Client client = (AmazonS3Client) AmazonS3ClientBuilder.standard() - .withEndpointConfiguration(localstack.getEndpointConfiguration(LocalStackContainer.Service.S3)) - .withCredentials(localstack.getDefaultCredentialsProvider()).build(); - - S3Service service = new S3Service(client); - - String presignedUrl = service.generatePresignedUrl(BUCKET_NAME, KEY, secondsToExpire, FILENAME); - - assertNotNull(presignedUrl); - assertFalse(presignedUrl.isEmpty()); - assertFalse(presignedUrl.isBlank()); - } + // Test constants + private final String BUCKET_NAME = "foxtest"; + private final String KEY = "t1"; + private final String FILENAME = "ensolversfox"; + + @Container + public LocalStackContainer localstack = + new LocalStackContainer(DockerImageName.parse("localstack/localstack:0.11.3")) + .withServices(LocalStackContainer.Service.S3); + + @Test + public void testS3() throws Exception { + String testData = "this is a sample test data"; + String folderName = "f1"; + String fileSuffix = ".txt"; + String charset = "UTF8"; + + AmazonS3Client client = + (AmazonS3Client) + AmazonS3ClientBuilder.standard() + .withEndpointConfiguration( + localstack.getEndpointConfiguration(LocalStackContainer.Service.S3)) + .withCredentials(localstack.getDefaultCredentialsProvider()) + .build(); + + S3Service service = new S3Service(client); + // prepares the bucket + client.createBucket(BUCKET_NAME); + + // read non existent file + File file = service.get(BUCKET_NAME, KEY); + assertNull(file); + + // no fail + service.delete(BUCKET_NAME, KEY); + + // write file in1 root context + File f = File.createTempFile(FILENAME, fileSuffix); + FileUtils.writeStringToFile(f, testData, charset); + service.put(BUCKET_NAME, KEY, f); + f.delete(); + + // read existent file + file = service.get(BUCKET_NAME, KEY); + assertNotNull(file); + String contents = FileUtils.readFileToString(file, charset); + assertEquals(testData, contents); + + // no fail + service.delete(BUCKET_NAME, KEY); + + file = service.get(BUCKET_NAME, KEY); + assertNull(file); + + List keys = service.list(BUCKET_NAME, folderName); + assertTrue(keys.isEmpty()); + + // write file in folder + f = File.createTempFile(FILENAME, fileSuffix); + FileUtils.writeStringToFile(f, testData, charset); + service.put(BUCKET_NAME, folderName + "/" + KEY, f); + f.delete(); + + // now folder shouldn't be empty + keys = service.list(BUCKET_NAME, folderName); + assertFalse(keys.isEmpty()); + } + + @Test + public void shouldGeneratePresignedUrl() { + Long secondsToExpire = 60L; + + AmazonS3Client client = + (AmazonS3Client) + AmazonS3ClientBuilder.standard() + .withEndpointConfiguration( + localstack.getEndpointConfiguration(LocalStackContainer.Service.S3)) + .withCredentials(localstack.getDefaultCredentialsProvider()) + .build(); + + S3Service service = new S3Service(client); + + String presignedUrl = service.generatePresignedUrl(BUCKET_NAME, KEY, secondsToExpire, FILENAME); + + assertNotNull(presignedUrl); + assertFalse(presignedUrl.isEmpty()); + assertFalse(presignedUrl.isBlank()); + } }