From 3f1d977bc32c1d8195c07588e48abbe2343d5acf Mon Sep 17 00:00:00 2001 From: scmacdon Date: Thu, 19 Sep 2024 11:53:00 -0400 Subject: [PATCH 01/10] update bucket names --- .../com/example/s3/batch/HelloS3Batch.java | 2 +- .../com/example/s3/batch/S3BatchScenario.java | 2 +- .../com/example/s3/scenario/S3Scenario.java | 20 +++++++++---------- .../src/main/resources/batch/job-manifest.csv | 8 ++++---- .../src/main/resources/batch/object-key-1.txt | 1 + .../src/main/resources/batch/object-key-2.txt | 1 + .../src/main/resources/batch/object-key-3.txt | 1 + .../src/main/resources/batch/object-key-4.txt | 1 + 8 files changed, 20 insertions(+), 16 deletions(-) create mode 100644 javav2/example_code/s3/src/main/resources/batch/object-key-1.txt create mode 100644 javav2/example_code/s3/src/main/resources/batch/object-key-2.txt create mode 100644 javav2/example_code/s3/src/main/resources/batch/object-key-3.txt create mode 100644 javav2/example_code/s3/src/main/resources/batch/object-key-4.txt diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/batch/HelloS3Batch.java b/javav2/example_code/s3/src/main/java/com/example/s3/batch/HelloS3Batch.java index 0d8cde36b09..cefdc581b96 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/batch/HelloS3Batch.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/batch/HelloS3Batch.java @@ -30,7 +30,7 @@ public static void main(String []args ) { System.err.println("List batch jobs failed: " + ex.getMessage()); return null; }) - .join(); // Wait for completion + .join(); } catch (CompletionException ex) { System.err.println("Failed to list batch jobs: " + ex.getMessage()); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/batch/S3BatchScenario.java b/javav2/example_code/s3/src/main/java/com/example/s3/batch/S3BatchScenario.java index 9cd4be0cce2..cfe7abac0aa 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/batch/S3BatchScenario.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/batch/S3BatchScenario.java @@ -54,7 +54,7 @@ public static void main(String[] args) throws IOException { System.out.println(DASHES); System.out.println("Setup the required bucket for this scenario."); waitForInputToContinue(scanner); - String bucketName = "x-" + UUID.randomUUID(); + String bucketName = "amazon-s3-demo-bucket-" + UUID.randomUUID(); actions.createBucket(bucketName); String reportBucketName = "arn:aws:s3:::"+bucketName; String manifestLocation = "arn:aws:s3:::"+bucketName+"/job-manifest.csv"; diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Scenario.java b/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Scenario.java index ac660ce94a9..6837bda696a 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Scenario.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Scenario.java @@ -53,16 +53,16 @@ public static void main(String[] args) throws IOException { toBucket - The name of the S3 bucket to which the object will be copied. """; - if (args.length != 4) { - logger.info(usage); - return; - } - - String bucketName = "scenario-" + UUID.randomUUID(); - String key = args[0]; - String objectPath = args[1]; - String savePath = args[2]; - String toBucket = args[3]; + // if (args.length != 4) { + // logger.info(usage); + // return; + // } + + String bucketName = "amazon-s3-demo-bucket-" + UUID.randomUUID(); + String key = "book.pdf" ; //args[0]; + String objectPath = "C:\\AWS\\book2.pdf" ; //args[1]; + String savePath = "C:\\AWS\\foo\\book33.pdf"; //args[2]; + String toBucket = "scottsitewise";// args[3]; logger.info(DASHES); logger.info("Welcome to the Amazon Simple Storage Service (S3) example scenario."); diff --git a/javav2/example_code/s3/src/main/resources/batch/job-manifest.csv b/javav2/example_code/s3/src/main/resources/batch/job-manifest.csv index 8d08f24a3b4..a04dbf146e9 100644 --- a/javav2/example_code/s3/src/main/resources/batch/job-manifest.csv +++ b/javav2/example_code/s3/src/main/resources/batch/job-manifest.csv @@ -1,4 +1,4 @@ -x-f8798006-c1ac-44b9-8372-ec2b4704faa7,object-key-1.txt -x-f8798006-c1ac-44b9-8372-ec2b4704faa7,object-key-2.txt -x-f8798006-c1ac-44b9-8372-ec2b4704faa7,object-key-3.txt -x-f8798006-c1ac-44b9-8372-ec2b4704faa7,object-key-4.txt +amazon-s3-demo-bucket-50270fac-5f8f-4b0e-9209-d80b7cc0760d,object-key-1.txt +amazon-s3-demo-bucket-50270fac-5f8f-4b0e-9209-d80b7cc0760d,object-key-2.txt +amazon-s3-demo-bucket-50270fac-5f8f-4b0e-9209-d80b7cc0760d,object-key-3.txt +amazon-s3-demo-bucket-50270fac-5f8f-4b0e-9209-d80b7cc0760d,object-key-4.txt diff --git a/javav2/example_code/s3/src/main/resources/batch/object-key-1.txt b/javav2/example_code/s3/src/main/resources/batch/object-key-1.txt new file mode 100644 index 00000000000..793aa682b06 --- /dev/null +++ b/javav2/example_code/s3/src/main/resources/batch/object-key-1.txt @@ -0,0 +1 @@ +This is a test \ No newline at end of file diff --git a/javav2/example_code/s3/src/main/resources/batch/object-key-2.txt b/javav2/example_code/s3/src/main/resources/batch/object-key-2.txt new file mode 100644 index 00000000000..793aa682b06 --- /dev/null +++ b/javav2/example_code/s3/src/main/resources/batch/object-key-2.txt @@ -0,0 +1 @@ +This is a test \ No newline at end of file diff --git a/javav2/example_code/s3/src/main/resources/batch/object-key-3.txt b/javav2/example_code/s3/src/main/resources/batch/object-key-3.txt new file mode 100644 index 00000000000..793aa682b06 --- /dev/null +++ b/javav2/example_code/s3/src/main/resources/batch/object-key-3.txt @@ -0,0 +1 @@ +This is a test \ No newline at end of file diff --git a/javav2/example_code/s3/src/main/resources/batch/object-key-4.txt b/javav2/example_code/s3/src/main/resources/batch/object-key-4.txt new file mode 100644 index 00000000000..793aa682b06 --- /dev/null +++ b/javav2/example_code/s3/src/main/resources/batch/object-key-4.txt @@ -0,0 +1 @@ +This is a test \ No newline at end of file From f8e6014a8124435631f290f33f84727e536a3d85 Mon Sep 17 00:00:00 2001 From: scmacdon Date: Tue, 24 Sep 2024 08:13:28 -0400 Subject: [PATCH 02/10] updated S3 examples to use valid bucket names --- .../s3/AbortMultipartUploadExamples.java | 82 ++-- .../com/example/s3/BasicOpsWithChecksums.java | 58 +-- .../com/example/s3/CheckObjectIntegrity.java | 398 ++++++++-------- .../com/example/s3/CopyObjectStorage.java | 35 +- .../com/example/s3/CreateAccessPoint.java | 41 +- .../com/example/s3/DeleteBucketPolicy.java | 1 - .../com/example/s3/DeleteMultiObjects.java | 38 +- .../s3/DeleteWebsiteConfiguration.java | 21 +- .../GeneratePresignedGetUrlAndRetrieve.java | 4 +- ...atePresignedUrlAndPutFileWithMetadata.java | 4 +- .../GeneratePresignedUrlAndUploadObject.java | 32 +- .../src/main/java/com/example/s3/GetAcl.java | 28 +- .../java/com/example/s3/GetBucketPolicy.java | 23 +- .../s3/GetDataResponseTransformer.java | 32 +- .../com/example/s3/GetObjectContentType.java | 27 +- .../com/example/s3/GetObjectPresignedUrl.java | 36 +- .../example/s3/GetObjectRestoreStatus.java | 23 +- .../java/com/example/s3/GetObjectTags.java | 30 +- .../java/com/example/s3/GetObjectUrl.java | 28 +- .../src/main/java/com/example/s3/HelloS3.java | 10 +- .../com/example/s3/KMSEncryptionExample.java | 63 ++- .../example/s3/LifecycleConfiguration.java | 325 ++++++------- .../com/example/s3/ListObjectsPaginated.java | 25 +- .../com/example/s3/ManagingObjectTags.java | 78 ++-- .../example/s3/PerformMultiPartUpload.java | 55 +-- .../java/com/example/s3/PutBucketLogging.java | 140 +++--- .../com/example/s3/PutObjectMetadata.java | 32 +- .../com/example/s3/PutObjectRetention.java | 38 +- .../java/com/example/s3/RestoreObject.java | 35 +- .../java/com/example/s3/S3BucketDeletion.java | 29 +- .../src/main/java/com/example/s3/S3Cors.java | 53 ++- .../com/example/s3/S3ObjectOperations.java | 430 +++++++++--------- .../java/com/example/s3/S3ZipExample.java | 49 +- .../src/main/java/com/example/s3/SetAcl.java | 43 +- .../java/com/example/s3/SetBucketPolicy.java | 30 +- .../example/s3/SetWebsiteConfiguration.java | 31 +- .../java/com/example/s3/VPCCreateJob.java | 209 ++++----- .../s3/lockscenario/S3ObjectLockWorkflow.java | 19 +- .../s3/transfermanager/DownloadFile.java | 2 +- .../transfermanager/DownloadToDirectory.java | 2 +- .../s3/transfermanager/ObjectCopy.java | 4 +- .../s3/transfermanager/UploadADirectory.java | 2 +- .../s3/transfermanager/UploadFile.java | 2 +- .../s3/transfermanager/UploadStream.java | 2 +- 44 files changed, 1346 insertions(+), 1303 deletions(-) diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/AbortMultipartUploadExamples.java b/javav2/example_code/s3/src/main/java/com/example/s3/AbortMultipartUploadExamples.java index d834e7f0c6e..47afd015831 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/AbortMultipartUploadExamples.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/AbortMultipartUploadExamples.java @@ -54,7 +54,7 @@ */ public class AbortMultipartUploadExamples { - static final String bucketName = "x-" + UUID.randomUUID(); + static final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); static final String key = UUID.randomUUID().toString(); static final String classPathFilePath = "/multipartUploadFiles/s3-userguide.pdf"; static final String filePath = getFullFilePath(classPathFilePath); @@ -80,8 +80,8 @@ public static void doAbortIncompleteMultipartUploadsFromList() { // snippet-start:[s3.java2.abort_upload_from_list] public static void abortIncompleteMultipartUploadsFromList() { ListMultipartUploadsRequest listMultipartUploadsRequest = ListMultipartUploadsRequest.builder() - .bucket(bucketName) - .build(); + .bucket(bucketName) + .build(); ListMultipartUploadsResponse response = s3Client.listMultipartUploads(listMultipartUploadsRequest); List uploads = response.uploads(); @@ -89,11 +89,11 @@ public static void abortIncompleteMultipartUploadsFromList() { AbortMultipartUploadRequest abortMultipartUploadRequest; for (MultipartUpload upload : uploads) { abortMultipartUploadRequest = AbortMultipartUploadRequest.builder() - .bucket(bucketName) - .key(upload.key()) - .expectedBucketOwner(accountId) - .uploadId(upload.uploadId()) - .build(); + .bucket(bucketName) + .key(upload.key()) + .expectedBucketOwner(accountId) + .uploadId(upload.uploadId()) + .build(); AbortMultipartUploadResponse abortMultipartUploadResponse = s3Client.abortMultipartUpload(abortMultipartUploadRequest); if (abortMultipartUploadResponse.sdkHttpResponse().isSuccessful()) { @@ -114,8 +114,8 @@ static void doAbortIncompleteMultipartUploadsOlderThan() { // snippet-start:[s3.java2.abort_upload_older_than] static void abortIncompleteMultipartUploadsOlderThan(Instant pointInTime) { ListMultipartUploadsRequest listMultipartUploadsRequest = ListMultipartUploadsRequest.builder() - .bucket(bucketName) - .build(); + .bucket(bucketName) + .build(); ListMultipartUploadsResponse response = s3Client.listMultipartUploads(listMultipartUploadsRequest); List uploads = response.uploads(); @@ -125,11 +125,11 @@ static void abortIncompleteMultipartUploadsOlderThan(Instant pointInTime) { logger.info("Found multipartUpload with upload ID [{}], initiated [{}]", upload.uploadId(), upload.initiated()); if (upload.initiated().isBefore(pointInTime)) { abortMultipartUploadRequest = AbortMultipartUploadRequest.builder() - .bucket(bucketName) - .key(upload.key()) - .expectedBucketOwner(accountId) - .uploadId(upload.uploadId()) - .build(); + .bucket(bucketName) + .key(upload.key()) + .expectedBucketOwner(accountId) + .uploadId(upload.uploadId()) + .build(); AbortMultipartUploadResponse abortMultipartUploadResponse = s3Client.abortMultipartUpload(abortMultipartUploadRequest); if (abortMultipartUploadResponse.sdkHttpResponse().isSuccessful()) { @@ -156,9 +156,9 @@ static void doAbortMultipartUploadUsingUploadId() { static void abortMultipartUploadUsingUploadId() { String uploadId = startUploadReturningUploadId(); AbortMultipartUploadResponse response = s3Client.abortMultipartUpload(b -> b - .uploadId(uploadId) - .bucket(bucketName) - .key(key)); + .uploadId(uploadId) + .bucket(bucketName) + .key(key)); if (response.sdkHttpResponse().isSuccessful()) { logger.info("Upload ID [{}] to bucket [{}] successfully aborted.", uploadId, bucketName); @@ -181,16 +181,16 @@ static void doAbortMultipartUploadsUsingLifecycleConfig() { // snippet-start:[s3.java2.abort_upload_using_lifecycle_config] static void abortMultipartUploadsUsingLifecycleConfig() { Collection lifeCycleRules = List.of(LifecycleRule.builder() - .abortIncompleteMultipartUpload(b -> b. - daysAfterInitiation(7)) - .status("Enabled") - .filter(SdkBuilder::build) // Filter element is required. - .build()); + .abortIncompleteMultipartUpload(b -> b. + daysAfterInitiation(7)) + .status("Enabled") + .filter(SdkBuilder::build) // Filter element is required. + .build()); // If the action is successful, the service sends back an HTTP 200 response with an empty HTTP body. PutBucketLifecycleConfigurationResponse response = s3Client.putBucketLifecycleConfiguration(b -> b - .bucket(bucketName) - .lifecycleConfiguration(b1 -> b1.rules(lifeCycleRules))); + .bucket(bucketName) + .lifecycleConfiguration(b1 -> b1.rules(lifeCycleRules))); if (response.sdkHttpResponse().isSuccessful()) { logger.info("Rule to abort incomplete multipart uploads added to bucket."); @@ -243,8 +243,8 @@ static void doMultipartUpload() { static String step1CreateMultipartUpload() { CreateMultipartUploadResponse createMultipartUploadResponse = s3Client.createMultipartUpload(b -> b - .bucket(bucketName) - .key(key)); + .bucket(bucketName) + .key(key)); return createMultipartUploadResponse.uploadId(); } @@ -262,20 +262,20 @@ static List step2UploadParts(String uploadId) { bb.flip(); // Swap position and limit before reading from the buffer. UploadPartRequest uploadPartRequest = UploadPartRequest.builder() - .bucket(bucketName) - .key(key) - .uploadId(uploadId) - .partNumber(partNumber) - .build(); + .bucket(bucketName) + .key(key) + .uploadId(uploadId) + .partNumber(partNumber) + .build(); UploadPartResponse partResponse = s3Client.uploadPart( - uploadPartRequest, - RequestBody.fromByteBuffer(bb)); + uploadPartRequest, + RequestBody.fromByteBuffer(bb)); CompletedPart part = CompletedPart.builder() - .partNumber(partNumber) - .eTag(partResponse.eTag()) - .build(); + .partNumber(partNumber) + .eTag(partResponse.eTag()) + .build(); completedParts.add(part); logger.info("Part {} upload", partNumber); @@ -292,10 +292,10 @@ static List step2UploadParts(String uploadId) { static void step3CompleteMultipartUpload(String uploadId, List completedParts) { s3Client.completeMultipartUpload(b -> b - .bucket(bucketName) - .key(key) - .uploadId(uploadId) - .multipartUpload(CompletedMultipartUpload.builder().parts(completedParts).build())); + .bucket(bucketName) + .key(key) + .uploadId(uploadId) + .multipartUpload(CompletedMultipartUpload.builder().parts(completedParts).build())); } static String startUploadReturningUploadId() { diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/BasicOpsWithChecksums.java b/javav2/example_code/s3/src/main/java/com/example/s3/BasicOpsWithChecksums.java index 83979861f3c..c290852aac0 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/BasicOpsWithChecksums.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/BasicOpsWithChecksums.java @@ -43,7 +43,7 @@ // snippet-start:[s3.java2.basicOpsWithChecksums.full] public class BasicOpsWithChecksums { static final S3Client s3Client = S3Client.create(); - static final String bucketName = "x-" + UUID.randomUUID(); + static final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); static final String key = UUID.randomUUID().toString(); private static final Logger logger = LoggerFactory.getLogger(BasicOpsWithChecksums.class); @@ -105,7 +105,7 @@ public void putObjectWithChecksum() { .bucket(bucketName) .key(key) .checksumAlgorithm(ChecksumAlgorithm.CRC32), - RequestBody.fromString("This is a test")); + RequestBody.fromString("This is a test")); } // snippet-end:[s3.java2.basicOpsWithChecksums.putObject] @@ -115,7 +115,7 @@ public GetObjectResponse getObjectWithChecksum() { .bucket(bucketName) .key(key) .checksumMode(ChecksumMode.ENABLED)) - .response(); + .response(); } // snippet-end:[s3.java2.basicOpsWithChecksums.getObject] @@ -127,7 +127,7 @@ public void putObjectWithPrecalculatedChecksum(String filePath) { .bucket(bucketName) .key(key) .checksumSHA256(checksum)), - RequestBody.fromFile(Paths.get(filePath))); + RequestBody.fromFile(Paths.get(filePath))); } // snippet-end:[s3.java2.basicOpsWithChecksums.putObjectPreCalc] @@ -135,12 +135,12 @@ public void putObjectWithPrecalculatedChecksum(String filePath) { public void multipartUploadWithChecksumTm(String filePath) { S3TransferManager transferManager = S3TransferManager.create(); UploadFileRequest uploadFileRequest = UploadFileRequest.builder() - .putObjectRequest(b -> b - .bucket(bucketName) - .key(key) - .checksumAlgorithm(ChecksumAlgorithm.SHA1)) - .source(Paths.get(filePath)) - .build(); + .putObjectRequest(b -> b + .bucket(bucketName) + .key(key) + .checksumAlgorithm(ChecksumAlgorithm.SHA1)) + .source(Paths.get(filePath)) + .build(); FileUpload fileUpload = transferManager.uploadFile(uploadFileRequest); fileUpload.completionFuture().join(); transferManager.close(); @@ -153,9 +153,9 @@ public void multipartUploadWithChecksumS3Client(String filePath) { // Initiate the multipart upload. CreateMultipartUploadResponse createMultipartUploadResponse = s3Client.createMultipartUpload(b -> b - .bucket(bucketName) - .key(key) - .checksumAlgorithm(algorithm)); // Checksum specified on initiation. + .bucket(bucketName) + .key(key) + .checksumAlgorithm(algorithm)); // Checksum specified on initiation. String uploadId = createMultipartUploadResponse.uploadId(); // Upload the parts of the file. @@ -172,22 +172,22 @@ public void multipartUploadWithChecksumS3Client(String filePath) { bb.flip(); // Swap position and limit before reading from the buffer. UploadPartRequest uploadPartRequest = UploadPartRequest.builder() - .bucket(bucketName) - .key(key) - .uploadId(uploadId) - .checksumAlgorithm(algorithm) // Checksum specified on each part. - .partNumber(partNumber) - .build(); + .bucket(bucketName) + .key(key) + .uploadId(uploadId) + .checksumAlgorithm(algorithm) // Checksum specified on each part. + .partNumber(partNumber) + .build(); UploadPartResponse partResponse = s3Client.uploadPart( - uploadPartRequest, - RequestBody.fromByteBuffer(bb)); + uploadPartRequest, + RequestBody.fromByteBuffer(bb)); CompletedPart part = CompletedPart.builder() - .partNumber(partNumber) - .checksumCRC32(partResponse.checksumCRC32()) // Provide the calculated checksum. - .eTag(partResponse.eTag()) - .build(); + .partNumber(partNumber) + .checksumCRC32(partResponse.checksumCRC32()) // Provide the calculated checksum. + .eTag(partResponse.eTag()) + .build(); completedParts.add(part); bb.clear(); @@ -200,10 +200,10 @@ public void multipartUploadWithChecksumS3Client(String filePath) { // Complete the multipart upload. s3Client.completeMultipartUpload(b -> b - .bucket(bucketName) - .key(key) - .uploadId(uploadId) - .multipartUpload(CompletedMultipartUpload.builder().parts(completedParts).build())); + .bucket(bucketName) + .key(key) + .uploadId(uploadId) + .multipartUpload(CompletedMultipartUpload.builder().parts(completedParts).build())); } // snippet-end:[s3.java2.basicOpsWithChecksums.multiPartS3Client] diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/CheckObjectIntegrity.java b/javav2/example_code/s3/src/main/java/com/example/s3/CheckObjectIntegrity.java index bab1a259c71..3d3522f8e85 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/CheckObjectIntegrity.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/CheckObjectIntegrity.java @@ -5,6 +5,7 @@ // snippet-start:[s3.java2.s3_object_check_integrity.main] // snippet-start:[s3.java2.s3_object_check_integrity.import] + import software.amazon.awssdk.core.ResponseInputStream; import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.regions.Region; @@ -24,6 +25,7 @@ import software.amazon.awssdk.services.s3.model.ObjectAttributes; import software.amazon.awssdk.services.s3.model.UploadPartRequest; import software.amazon.awssdk.services.s3.model.UploadPartResponse; + import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -41,232 +43,230 @@ /** * To run this AWS code example, ensure that you have setup your development * environment, including your AWS credentials. - * + *

* For information, see this documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CheckObjectIntegrity { - private final static int CHUNK_SIZE = 5 * 1024 * 1024; + private final static int CHUNK_SIZE = 5 * 1024 * 1024; - public static void main(String[] args) { - final String USAGE = """ - Usage: - \s + public static void main(String[] args) { + final String USAGE = """ + Usage: + \s - Where: - bucketName - the Amazon S3 bucket to upload an object into. - objectKey - the object to upload (for example, book.pdf). - objectPath - the path where the file is located (for example, C:/AWS/book2.pdf).\s - """; + Where: + bucketName - the Amazon S3 bucket to upload an object into. + objectKey - the object to upload (for example, book.pdf). + objectPath - the path where the file is located (for example, C:/AWS/book2.pdf).\s + """; - if (args.length != 3) { - System.out.println(USAGE); - System.exit(1); - } + if (args.length != 3) { + System.out.println(USAGE); + return; + } - String bucketName = args[0]; - String objectKey = args[1]; - String objectPath = args[2]; - System.out.println("Putting object " + objectKey + " into bucket " + bucketName - + " with checksum in algorithm sha256."); - System.out.println(" in bucket: " + bucketName); + String bucketName = args[0]; + String objectKey = args[1]; + String objectPath = args[2]; - Region region = Region.US_EAST_1; - S3Client s3 = S3Client.builder() - .region(region) - .build(); + System.out.println("Putting object " + objectKey + " into bucket " + bucketName + + " with checksum in algorithm sha256."); + System.out.println(" in bucket: " + bucketName); - putS3MultipartObjectBracketedByChecksum(s3, bucketName, objectKey, objectPath); - downloadS3MultipartObjectBracketedByChecksum(s3, bucketName, objectKey); - validateExistingFileAgainstS3Checksum(s3, bucketName, objectKey, objectPath); - } + Region region = Region.US_EAST_1; + S3Client s3 = S3Client.builder() + .region(region) + .build(); - public static void putS3MultipartObjectBracketedByChecksum(S3Client s3, String bucketName, String objectKey, - String objectPath) { - System.out.println("Starting uploading file with additional checksum."); - File file = new File(objectPath); - try (InputStream in = new FileInputStream(file)) { - CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest - .builder() - .bucket(bucketName) - .key(objectKey) - .checksumAlgorithm(ChecksumAlgorithm.SHA256) - .build(); + putS3MultipartObjectBracketedByChecksum(s3, bucketName, objectKey, objectPath); + downloadS3MultipartObjectBracketedByChecksum(s3, bucketName, objectKey); + validateExistingFileAgainstS3Checksum(s3, bucketName, objectKey, objectPath); + } - CreateMultipartUploadResponse createdUpload = s3 - .createMultipartUpload(createMultipartUploadRequest); - List completedParts = new ArrayList<>(); - int partNumber = 1; - byte[] buffer = new byte[CHUNK_SIZE]; - int read = in.read(buffer); + public static void putS3MultipartObjectBracketedByChecksum(S3Client s3, String bucketName, String objectKey, + String objectPath) { + System.out.println("Starting uploading file with additional checksum."); + File file = new File(objectPath); + try (InputStream in = new FileInputStream(file)) { + CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest.builder() + .bucket(bucketName) + .key(objectKey) + .checksumAlgorithm(ChecksumAlgorithm.SHA256) + .build(); - while (read != -1) { - UploadPartRequest uploadPartRequest = UploadPartRequest.builder() - .partNumber(partNumber) - .uploadId(createdUpload.uploadId()) - .bucket(bucketName) - .key(objectKey) - .checksumAlgorithm(ChecksumAlgorithm.SHA256) - .build(); + CreateMultipartUploadResponse createdUpload = s3.createMultipartUpload(createMultipartUploadRequest); + List completedParts = new ArrayList<>(); + int partNumber = 1; + byte[] buffer = new byte[CHUNK_SIZE]; + int read = in.read(buffer); - UploadPartResponse uploadedPart = s3.uploadPart(uploadPartRequest, - RequestBody.fromByteBuffer(ByteBuffer.wrap(buffer, 0, read))); - CompletedPart part = CompletedPart.builder() - .partNumber(partNumber) - .checksumSHA256(uploadedPart.checksumSHA256()) - .eTag(uploadedPart.eTag()).build(); - completedParts.add(part); - read = in.read(buffer); - partNumber++; - } + while (read != -1) { + UploadPartRequest uploadPartRequest = UploadPartRequest.builder() + .partNumber(partNumber) + .uploadId(createdUpload.uploadId()) + .bucket(bucketName) + .key(objectKey) + .checksumAlgorithm(ChecksumAlgorithm.SHA256) + .build(); - CompletedMultipartUpload completedMultipartUpload = CompletedMultipartUpload.builder() - .parts(completedParts).build(); - CompleteMultipartUploadResponse completedUploadResponse = s3.completeMultipartUpload( - CompleteMultipartUploadRequest.builder() - .bucket(bucketName) - .key(objectKey) - .uploadId(createdUpload.uploadId()) - .multipartUpload(completedMultipartUpload).build()); + UploadPartResponse uploadedPart = s3.uploadPart(uploadPartRequest, + RequestBody.fromByteBuffer(ByteBuffer.wrap(buffer, 0, read))); + CompletedPart part = CompletedPart.builder() + .partNumber(partNumber) + .checksumSHA256(uploadedPart.checksumSHA256()) + .eTag(uploadedPart.eTag()).build(); + completedParts.add(part); + read = in.read(buffer); + partNumber++; + } - } catch (IOException e) { - e.printStackTrace(); - } + CompletedMultipartUpload completedMultipartUpload = CompletedMultipartUpload.builder() + .parts(completedParts).build(); + CompleteMultipartUploadResponse completedUploadResponse = s3.completeMultipartUpload( + CompleteMultipartUploadRequest.builder() + .bucket(bucketName) + .key(objectKey) + .uploadId(createdUpload.uploadId()) + .multipartUpload(completedMultipartUpload).build()); + + } catch (IOException e) { + e.printStackTrace(); } + } - public static void downloadS3MultipartObjectBracketedByChecksum(S3Client s3, String bucketName, - String objectKey) { - System.out.println("Starting downloading file and doing validation"); - File file = new File("DOWNLOADED_" + objectKey); - try (OutputStream out = new FileOutputStream(file)) { - GetObjectAttributesResponse objectAttributes = s3.getObjectAttributes(GetObjectAttributesRequest - .builder().bucket(bucketName).key(objectKey) - .objectAttributes(ObjectAttributes.OBJECT_PARTS, ObjectAttributes.CHECKSUM) - .build()); - // Initialize the digester to calculate checksum of checksums. - MessageDigest sha256ChecksumOfChecksums = MessageDigest.getInstance("SHA-256"); + public static void downloadS3MultipartObjectBracketedByChecksum(S3Client s3, String bucketName, + String objectKey) { + System.out.println("Starting downloading file and doing validation"); + File file = new File("DOWNLOADED_" + objectKey); + try (OutputStream out = new FileOutputStream(file)) { + GetObjectAttributesResponse objectAttributes = s3.getObjectAttributes(GetObjectAttributesRequest.builder() + .bucket(bucketName) + .key(objectKey) + .objectAttributes(ObjectAttributes.OBJECT_PARTS, ObjectAttributes.CHECKSUM) + .build()); - // If you retrieve the object in parts, and set the ChecksumMode to enabled, the - // SDK will automatically validate the part checksum - for (int partNumber = 1; partNumber <= objectAttributes.objectParts() - .totalPartsCount(); partNumber++) { - MessageDigest sha256PartChecksum = MessageDigest.getInstance("SHA-256"); - ResponseInputStream response = s3 - .getObject(GetObjectRequest.builder() - .bucket(bucketName) - .key(objectKey) - .partNumber(partNumber) - .checksumMode(ChecksumMode.ENABLED).build()); - GetObjectResponse getObjectResponse = response.response(); - byte[] buffer = new byte[CHUNK_SIZE]; - int read = response.read(buffer); - while (read != -1) { - out.write(buffer, 0, read); - sha256PartChecksum.update(buffer, 0, read); - read = response.read(buffer); - } - byte[] sha256PartBytes = sha256PartChecksum.digest(); - sha256ChecksumOfChecksums.update(sha256PartBytes); + MessageDigest sha256ChecksumOfChecksums = MessageDigest.getInstance("SHA-256"); - String base64PartChecksum = Base64.getEncoder().encodeToString(sha256PartBytes); - String base64PartChecksumFromObjectAttributes = objectAttributes.objectParts().parts() - .get(partNumber - 1).checksumSHA256(); - if (!base64PartChecksum.equals(getObjectResponse.checksumSHA256()) - || !base64PartChecksum.equals(base64PartChecksumFromObjectAttributes)) { - throw new IOException("Part checksum didn't match for the part"); - } - System.out.println(partNumber + " " + base64PartChecksum); - } + for (int partNumber = 1; partNumber <= objectAttributes.objectParts().totalPartsCount(); partNumber++) { + MessageDigest sha256PartChecksum = MessageDigest.getInstance("SHA-256"); + ResponseInputStream response = s3.getObject( + GetObjectRequest.builder() + .bucket(bucketName) + .key(objectKey) + .partNumber(partNumber) + .checksumMode(ChecksumMode.ENABLED).build()); - // Before finalizing, do the final checksum validation. - String base64ChecksumOfChecksums = Base64.getEncoder() - .encodeToString(sha256ChecksumOfChecksums.digest()); - String base64ChecksumOfChecksumFromAttributes = objectAttributes.checksum().checksumSHA256(); - if (base64ChecksumOfChecksumFromAttributes != null - && !base64ChecksumOfChecksums.equals(base64ChecksumOfChecksumFromAttributes)) { - throw new IOException( - "Failed checksum validation for full object checksum of checksums"); - } - System.out.println("Checksum of checksums: " + base64ChecksumOfChecksumFromAttributes); - out.flush(); - } catch (IOException | NoSuchAlgorithmException e) { - // Cleanup bad file - file.delete(); - e.printStackTrace(); + GetObjectResponse getObjectResponse = response.response(); + byte[] buffer = new byte[CHUNK_SIZE]; + int read = response.read(buffer); + while (read != -1) { + out.write(buffer, 0, read); + sha256PartChecksum.update(buffer, 0, read); + read = response.read(buffer); } - } + byte[] sha256PartBytes = sha256PartChecksum.digest(); + sha256ChecksumOfChecksums.update(sha256PartBytes); - public static void validateExistingFileAgainstS3Checksum(S3Client s3, String bucketName, String objectKey, - String objectPath) { - System.out.println("Starting validating the locally persisted file."); - File file = new File(objectPath); - GetObjectAttributesResponse objectAttributes = s3.getObjectAttributes(GetObjectAttributesRequest - .builder().bucket(bucketName).key(objectKey) - .objectAttributes(ObjectAttributes.OBJECT_PARTS, ObjectAttributes.CHECKSUM).build()); - try (InputStream in = new FileInputStream(file)) { - MessageDigest sha256ChecksumOfChecksums = MessageDigest.getInstance("SHA-256"); - MessageDigest sha256Part = MessageDigest.getInstance("SHA-256"); - byte[] buffer = new byte[CHUNK_SIZE]; - int currentPart = 0; - int partBreak = Math.toIntExact(objectAttributes.objectParts().parts().get(currentPart).size()); - int totalRead = 0; - int read = in.read(buffer); - while (read != -1) { - totalRead += read; - if (totalRead >= partBreak) { - int difference = totalRead - partBreak; - byte[] partChecksum; - if (totalRead != partBreak) { - sha256Part.update(buffer, 0, read - difference); - partChecksum = sha256Part.digest(); - sha256ChecksumOfChecksums.update(partChecksum); - sha256Part.reset(); - sha256Part.update(buffer, read - difference, difference); - } else { - sha256Part.update(buffer, 0, read); - partChecksum = sha256Part.digest(); - sha256ChecksumOfChecksums.update(partChecksum); - sha256Part.reset(); - } - String base64PartChecksum = Base64.getEncoder().encodeToString(partChecksum); - if (!base64PartChecksum.equals(objectAttributes.objectParts().parts() - .get(currentPart).checksumSHA256())) { - throw new IOException( - "Part checksum didn't match what persisted in S3."); - } - currentPart++; - System.out.println(currentPart + " " + base64PartChecksum); - if (currentPart < objectAttributes.objectParts().totalPartsCount()) { - partBreak += objectAttributes.objectParts().parts().get(currentPart - 1) - .size(); - } - } else { - sha256Part.update(buffer, 0, read); - } - read = in.read(buffer); - } - if (currentPart != objectAttributes.objectParts().totalPartsCount()) { - currentPart++; - byte[] partChecksum = sha256Part.digest(); - sha256ChecksumOfChecksums.update(partChecksum); - String base64PartChecksum = Base64.getEncoder().encodeToString(partChecksum); - System.out.println(currentPart + " " + base64PartChecksum); - } + String base64PartChecksum = Base64.getEncoder().encodeToString(sha256PartBytes); + String base64PartChecksumFromObjectAttributes = objectAttributes.objectParts().parts() + .get(partNumber - 1).checksumSHA256(); + if (!base64PartChecksum.equals(getObjectResponse.checksumSHA256()) + || !base64PartChecksum.equals(base64PartChecksumFromObjectAttributes)) { + throw new IOException("Part checksum didn't match for the part"); + } + System.out.println(partNumber + " " + base64PartChecksum); + } + + String base64ChecksumOfChecksums = Base64.getEncoder() + .encodeToString(sha256ChecksumOfChecksums.digest()); + String base64ChecksumOfChecksumFromAttributes = objectAttributes.checksum().checksumSHA256(); + if (base64ChecksumOfChecksumFromAttributes != null + && !base64ChecksumOfChecksums.equals(base64ChecksumOfChecksumFromAttributes)) { + throw new IOException( + "Failed checksum validation for full object checksum of checksums"); + } + System.out.println("Checksum of checksums: " + base64ChecksumOfChecksumFromAttributes); + out.flush(); + } catch (IOException | NoSuchAlgorithmException e) { + file.delete(); + e.printStackTrace(); + } + } - String base64CalculatedChecksumOfChecksums = Base64.getEncoder() - .encodeToString(sha256ChecksumOfChecksums.digest()); - System.out.println("Calculated checksum of checksums: " + base64CalculatedChecksumOfChecksums); - System.out.println("S3 persisted checksum of checksums: " - + objectAttributes.checksum().checksumSHA256()); - if (!base64CalculatedChecksumOfChecksums.equals(objectAttributes.checksum().checksumSHA256())) { - throw new IOException("Full object checksum of checksums don't match S3"); - } + public static void validateExistingFileAgainstS3Checksum(S3Client s3, String bucketName, String objectKey, + String objectPath) { + System.out.println("Starting validating the locally persisted file."); + File file = new File(objectPath); + GetObjectAttributesResponse objectAttributes = s3.getObjectAttributes(GetObjectAttributesRequest.builder() + .bucket(bucketName) + .key(objectKey) + .objectAttributes(ObjectAttributes.OBJECT_PARTS, ObjectAttributes.CHECKSUM).build()); - } catch (IOException | NoSuchAlgorithmException e) { - e.printStackTrace(); + try (InputStream in = new FileInputStream(file)) { + MessageDigest sha256ChecksumOfChecksums = MessageDigest.getInstance("SHA-256"); + MessageDigest sha256Part = MessageDigest.getInstance("SHA-256"); + byte[] buffer = new byte[CHUNK_SIZE]; + int currentPart = 0; + int partBreak = Math.toIntExact(objectAttributes.objectParts().parts().get(currentPart).size()); + int totalRead = 0; + int read = in.read(buffer); + while (read != -1) { + totalRead += read; + if (totalRead >= partBreak) { + int difference = totalRead - partBreak; + byte[] partChecksum; + if (totalRead != partBreak) { + sha256Part.update(buffer, 0, read - difference); + partChecksum = sha256Part.digest(); + sha256ChecksumOfChecksums.update(partChecksum); + sha256Part.reset(); + sha256Part.update(buffer, read - difference, difference); + } else { + sha256Part.update(buffer, 0, read); + partChecksum = sha256Part.digest(); + sha256ChecksumOfChecksums.update(partChecksum); + sha256Part.reset(); + } + String base64PartChecksum = Base64.getEncoder().encodeToString(partChecksum); + if (!base64PartChecksum.equals(objectAttributes.objectParts().parts() + .get(currentPart).checksumSHA256())) { + throw new IOException("Part checksum didn't match what persisted in S3."); + } + currentPart++; + System.out.println(currentPart + " " + base64PartChecksum); + if (currentPart < objectAttributes.objectParts().totalPartsCount()) { + partBreak += objectAttributes.objectParts().parts().get(currentPart - 1).size(); + } + } else { + sha256Part.update(buffer, 0, read); } + read = in.read(buffer); + } + + if (currentPart != objectAttributes.objectParts().totalPartsCount()) { + currentPart++; + byte[] partChecksum = sha256Part.digest(); + sha256ChecksumOfChecksums.update(partChecksum); + String base64PartChecksum = Base64.getEncoder().encodeToString(partChecksum); + System.out.println(currentPart + " " + base64PartChecksum); + } + + String base64CalculatedChecksumOfChecksums = Base64.getEncoder() + .encodeToString(sha256ChecksumOfChecksums.digest()); + System.out.println("Calculated checksum of checksums: " + base64CalculatedChecksumOfChecksums); + System.out.println("S3 persisted checksum of checksums: " + + objectAttributes.checksum().checksumSHA256()); + if (!base64CalculatedChecksumOfChecksums.equals(objectAttributes.checksum().checksumSHA256())) { + throw new IOException("Full object checksum of checksums don't match S3"); + } + + } catch (IOException | NoSuchAlgorithmException e) { + e.printStackTrace(); } + } } + // snippet-end:[s3.java2.s3_object_check_integrity.main] diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/CopyObjectStorage.java b/javav2/example_code/s3/src/main/java/com/example/s3/CopyObjectStorage.java index 703e37e5e75..83166ef73dc 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/CopyObjectStorage.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/CopyObjectStorage.java @@ -5,6 +5,7 @@ // snippet-start:[s3.java2.copy_store.main] // snippet-start:[s3.java2.copy_store.import] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.CopyObjectRequest; @@ -14,9 +15,9 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ @@ -24,14 +25,14 @@ public class CopyObjectStorage { public static void main(String[] args) { final String usage = """ - Usage: - + Usage: + - Where: - objectKey - The name of the object (for example, book.pdf). - fromBucket - The S3 bucket name that contains the object (for example, bucket1). - toBucket - The S3 bucket to copy the object to (for example, bucket2). - """; + Where: + objectKey - The name of the object (for example, book.pdf). + fromBucket - The S3 bucket name that contains the object (for example, bucket1). + toBucket - The S3 bucket to copy the object to (for example, bucket2). + """; if (args.length != 3) { System.out.println(usage); @@ -43,8 +44,8 @@ public static void main(String[] args) { String toBucket = args[2]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); copyBucketObject(s3, fromBucket, objectKey, toBucket); s3.close(); @@ -52,12 +53,12 @@ public static void main(String[] args) { public static void copyBucketObject(S3Client s3, String fromBucket, String objectKey, String toBucket) { CopyObjectRequest copyReq = CopyObjectRequest.builder() - .sourceBucket(fromBucket) - .sourceKey(objectKey) - .storageClass("DEEP_ARCHIVE") - .destinationBucket(toBucket) - .destinationKey(objectKey) - .build(); + .sourceBucket(fromBucket) + .sourceKey(objectKey) + .storageClass("DEEP_ARCHIVE") + .destinationBucket(toBucket) + .destinationKey(objectKey) + .build(); try { s3.copyObject(copyReq); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/CreateAccessPoint.java b/javav2/example_code/s3/src/main/java/com/example/s3/CreateAccessPoint.java index 18583068081..c0285d253c1 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/CreateAccessPoint.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/CreateAccessPoint.java @@ -5,6 +5,7 @@ // snippet-start:[s3.java2.create_access_point.main] // snippet-start:[s3.java2.create_access_point.import] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3control.S3ControlClient; import software.amazon.awssdk.services.s3control.model.CreateAccessPointRequest; @@ -15,9 +16,9 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ @@ -25,14 +26,14 @@ public class CreateAccessPoint { public static void main(String[] args) { final String usage = """ - Usage: - + Usage: + - Where: - accountId - The account id that owns the Amazon S3 bucket.\s - bucketName - The Amazon S3 bucket name.\s - accessPointName - The access point name (for example, myaccesspoint).\s - """; + Where: + accountId - The account id that owns the Amazon S3 bucket.\s + bucketName - The Amazon S3 bucket name.\s + accessPointName - The access point name (for example, myaccesspoint).\s + """; if (args.length != 3) { System.out.println(usage); @@ -44,8 +45,8 @@ public static void main(String[] args) { String accessPointName = args[2]; Region region = Region.US_EAST_1; S3ControlClient s3ControlClient = S3ControlClient.builder() - .region(region) - .build(); + .region(region) + .build(); createSpecificAccessPoint(s3ControlClient, accountId, bucketName, accessPointName); deleteSpecificAccessPoint(s3ControlClient, accountId, accessPointName); @@ -54,13 +55,13 @@ public static void main(String[] args) { // This method creates an access point for the given Amazon S3 bucket. public static void createSpecificAccessPoint(S3ControlClient s3ControlClient, String accountId, String bucketName, - String accessPointName) { + String accessPointName) { try { CreateAccessPointRequest accessPointRequest = CreateAccessPointRequest.builder() - .accountId(accountId) - .bucket(bucketName) - .name(accessPointName) - .build(); + .accountId(accountId) + .bucket(bucketName) + .name(accessPointName) + .build(); s3ControlClient.createAccessPoint(accessPointRequest); System.out.println("The access point was created"); @@ -72,12 +73,12 @@ public static void createSpecificAccessPoint(S3ControlClient s3ControlClient, St } public static void deleteSpecificAccessPoint(S3ControlClient s3ControlClient, String accountId, - String accessPointName) { + String accessPointName) { try { DeleteAccessPointRequest deleteAccessPointRequest = DeleteAccessPointRequest.builder() - .name(accessPointName) - .accountId(accountId) - .build(); + .name(accessPointName) + .accountId(accountId) + .build(); s3ControlClient.deleteAccessPoint(deleteAccessPointRequest); System.out.println("The access point was deleted"); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/DeleteBucketPolicy.java b/javav2/example_code/s3/src/main/java/com/example/s3/DeleteBucketPolicy.java index 718939c5d03..cc751c1d538 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/DeleteBucketPolicy.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/DeleteBucketPolicy.java @@ -47,7 +47,6 @@ public static void main(String[] args) { s3.close(); } - // Delete the bucket policy. public static void deleteS3BucketPolicy(S3Client s3, String bucketName) { DeleteBucketPolicyRequest delReq = DeleteBucketPolicyRequest.builder() .bucket(bucketName) diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/DeleteMultiObjects.java b/javav2/example_code/s3/src/main/java/com/example/s3/DeleteMultiObjects.java index 0571624e250..b0ececa7497 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/DeleteMultiObjects.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/DeleteMultiObjects.java @@ -5,6 +5,7 @@ // snippet-start:[s3.java2.delete_many_objects.main] // snippet-start:[s3.java2.delete_many_objects.import] + import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; @@ -13,15 +14,16 @@ import software.amazon.awssdk.services.s3.model.Delete; import software.amazon.awssdk.services.s3.model.DeleteObjectsRequest; import software.amazon.awssdk.services.s3.model.S3Exception; + import java.util.ArrayList; // snippet-end:[s3.java2.delete_many_objects.import] /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ @@ -29,11 +31,11 @@ public class DeleteMultiObjects { public static void main(String[] args) { final String usage = """ - Usage: + Usage: - Where: - bucketName - the Amazon S3 bucket name. - """; + Where: + bucketName - the Amazon S3 bucket name. + """; if (args.length != 1) { System.out.println(usage); @@ -43,8 +45,8 @@ public static void main(String[] args) { String bucketName = args[0]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); deleteBucketObjects(s3, bucketName); s3.close(); @@ -59,13 +61,13 @@ public static void deleteBucketObjects(S3Client s3, String bucketName) { for (int i = 0; i < 3; i++) { String keyName = "delete object example " + i; objectId = ObjectIdentifier.builder() - .key(keyName) - .build(); + .key(keyName) + .build(); putOb = PutObjectRequest.builder() - .bucket(bucketName) - .key(keyName) - .build(); + .bucket(bucketName) + .key(keyName) + .build(); s3.putObject(putOb, RequestBody.fromString(keyName)); keys.add(objectId); @@ -75,14 +77,14 @@ public static void deleteBucketObjects(S3Client s3, String bucketName) { // Delete multiple objects in one request. Delete del = Delete.builder() - .objects(keys) - .build(); + .objects(keys) + .build(); try { DeleteObjectsRequest multiObjectDeleteRequest = DeleteObjectsRequest.builder() - .bucket(bucketName) - .delete(del) - .build(); + .bucket(bucketName) + .delete(del) + .build(); s3.deleteObjects(multiObjectDeleteRequest); System.out.println("Multiple objects are deleted!"); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/DeleteWebsiteConfiguration.java b/javav2/example_code/s3/src/main/java/com/example/s3/DeleteWebsiteConfiguration.java index a2705150778..53c4613b06b 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/DeleteWebsiteConfiguration.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/DeleteWebsiteConfiguration.java @@ -5,6 +5,7 @@ // snippet-start:[s3.java2.delete_website_configuration.main] // snippet-start:[s3.java2.delete_website_configuration.import] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.DeleteBucketWebsiteRequest; @@ -14,9 +15,9 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ @@ -24,11 +25,11 @@ public class DeleteWebsiteConfiguration { public static void main(String[] args) { final String usage = """ - Usage: + Usage: - Where: - bucketName - The Amazon S3 bucket to delete the website configuration from. - """; + Where: + bucketName - The Amazon S3 bucket to delete the website configuration from. + """; if (args.length != 1) { System.out.println(usage); @@ -39,8 +40,8 @@ public static void main(String[] args) { System.out.format("Deleting website configuration for Amazon S3 bucket: %s\n", bucketName); Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); deleteBucketWebsiteConfig(s3, bucketName); System.out.println("Done!"); @@ -49,8 +50,8 @@ public static void main(String[] args) { public static void deleteBucketWebsiteConfig(S3Client s3, String bucketName) { DeleteBucketWebsiteRequest delReq = DeleteBucketWebsiteRequest.builder() - .bucket(bucketName) - .build(); + .bucket(bucketName) + .build(); try { s3.deleteBucketWebsite(delReq); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedGetUrlAndRetrieve.java b/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedGetUrlAndRetrieve.java index 46f26fd06f0..c18865dbc15 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedGetUrlAndRetrieve.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedGetUrlAndRetrieve.java @@ -48,8 +48,8 @@ public class GeneratePresignedGetUrlAndRetrieve { private final static S3Client s3Client = S3Client.create(); public static void main(String[] args) { - String bucketName = "b-" + UUID.randomUUID(); - String keyName = "k-" + UUID.randomUUID(); + String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); + String keyName = "key" + UUID.randomUUID(); String resourcePath = "multipartUploadFiles/s3-userguide.pdf"; PresignUrlUtils.createBucket(bucketName, s3Client); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedUrlAndPutFileWithMetadata.java b/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedUrlAndPutFileWithMetadata.java index aacadcde3b3..5c4bf935b06 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedUrlAndPutFileWithMetadata.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedUrlAndPutFileWithMetadata.java @@ -52,8 +52,8 @@ public class GeneratePresignedUrlAndPutFileWithMetadata { private final static S3Client s3Client = S3Client.create(); public static void main(String[] args) { - String bucketName = "b-" + UUID.randomUUID(); - String keyName = "k-" + UUID.randomUUID(); + String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); + String keyName = "key" + UUID.randomUUID(); String resourcePath = "multipartUploadFiles/s3-userguide.pdf"; // Uncomment the following two lines and comment out the previous two lines to use an image file instead of a PDF file. //String resourcePath = "image.png"; diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedUrlAndUploadObject.java b/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedUrlAndUploadObject.java index 9fc6d2d578d..bead5a1f989 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedUrlAndUploadObject.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedUrlAndUploadObject.java @@ -39,9 +39,8 @@ public class GeneratePresignedUrlAndUploadObject { private static final Logger logger = org.slf4j.LoggerFactory.getLogger(GeneratePresignedUrlAndUploadObject.class); public static void main(String[] args) { - - String bucketName = "b-" + UUID.randomUUID(); - String keyName = "k-" + UUID.randomUUID(); + String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); + String keyName = "key" + UUID.randomUUID(); try (S3Client s3Client = S3Client.create()) { PresignUrlUtils.createBucket(bucketName, s3Client); @@ -58,9 +57,10 @@ public static void main(String[] args) { } // snippet-start:[presigned.java2.generatepresignedurl.main] + /** * Create a presigned URL for uploading a String object. - * + * * @param bucketName - The name of the bucket. * @param keyName - The name of the object. * @return - The presigned URL for an HTTP PUT. @@ -69,21 +69,21 @@ public URL createSignedUrlForStringPut(String bucketName, String keyName) { try (S3Presigner presigner = S3Presigner.create()) { PutObjectRequest objectRequest = PutObjectRequest.builder() - .bucket(bucketName) - .key(keyName) - .contentType("text/plain") - .build(); + .bucket(bucketName) + .key(keyName) + .contentType("text/plain") + .build(); PutObjectPresignRequest presignRequest = PutObjectPresignRequest.builder() - .signatureDuration(Duration.ofMinutes(10)) // The URL will expire in 10 minutes. - .putObjectRequest(objectRequest) - .build(); + .signatureDuration(Duration.ofMinutes(10)) // The URL will expire in 10 minutes. + .putObjectRequest(objectRequest) + .build(); PresignedPutObjectRequest presignedRequest = presigner.presignPutObject(presignRequest); String myURL = presignedRequest.url().toString(); logger.info("Presigned URL to upload to: [{}]", myURL); logger.info("Which HTTP method needs to be used when uploading: [{}]", - presignedRequest.httpRequest().method()); + presignedRequest.httpRequest().method()); return presignedRequest.url(); } @@ -93,7 +93,7 @@ public URL createSignedUrlForStringPut(String bucketName, String keyName) { * Use the JDK HttpURLConnection (since v1.1) class to upload a String, but you * can * use any HTTP client. - * + * * @param presignedUrl - The presigned URL. */ public void useHttpUrlConnectionToPutString(URL presignedUrl) { @@ -119,7 +119,7 @@ public void useHttpUrlConnectionToPutString(URL presignedUrl) { /** * Use the JDK HttpClient class (since v11) to upload a String, * but you can use any HTTP client. - * + * * @param presignedUrl - The presigned URL. */ public void useHttpClientToPutString(URL presignedUrl) { @@ -129,9 +129,9 @@ public void useHttpClientToPutString(URL presignedUrl) { .uri(presignedUrl.toURI()) .header("Content-Type", "text/plain") .PUT(HttpRequest.BodyPublishers - .ofString("This text was uploaded as an object by using a presigned URL.")) + .ofString("This text was uploaded as an object by using a presigned URL.")) .build(), - HttpResponse.BodyHandlers.discarding()); + HttpResponse.BodyHandlers.discarding()); logger.info("HTTP response code is " + response.statusCode()); } catch (S3Exception | IOException | URISyntaxException | InterruptedException e) { logger.error(e.getMessage(), e); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GetAcl.java b/javav2/example_code/s3/src/main/java/com/example/s3/GetAcl.java index 1cdcb6ef371..9970fbd2b4e 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GetAcl.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GetAcl.java @@ -5,21 +5,23 @@ // snippet-start:[s3.java2.get_acl.main] // snippet-start:[s3.java2.get_acl.import] + import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetObjectAclRequest; import software.amazon.awssdk.services.s3.model.GetObjectAclResponse; import software.amazon.awssdk.services.s3.model.Grant; + import java.util.List; // snippet-end:[s3.java2.get_acl.import] /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ @@ -27,13 +29,13 @@ public class GetAcl { public static void main(String[] args) { final String usage = """ - Usage: - + Usage: + - Where: - bucketName - The Amazon S3 bucket to get the access control list (ACL) for. - objectKey - The object to get the ACL for.\s - """; + Where: + bucketName - The Amazon S3 bucket to get the access control list (ACL) for. + objectKey - The object to get the ACL for.\s + """; if (args.length != 2) { System.out.println(usage); @@ -46,8 +48,8 @@ public static void main(String[] args) { System.out.println("in bucket: " + bucketName); Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); getBucketACL(s3, objectKey, bucketName); s3.close(); @@ -57,9 +59,9 @@ public static void main(String[] args) { public static String getBucketACL(S3Client s3, String objectKey, String bucketName) { try { GetObjectAclRequest aclReq = GetObjectAclRequest.builder() - .bucket(bucketName) - .key(objectKey) - .build(); + .bucket(bucketName) + .key(objectKey) + .build(); GetObjectAclResponse aclRes = s3.getObjectAcl(aclReq); List grants = aclRes.grants(); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GetBucketPolicy.java b/javav2/example_code/s3/src/main/java/com/example/s3/GetBucketPolicy.java index 9dc285f1b25..57d62b40ba2 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GetBucketPolicy.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GetBucketPolicy.java @@ -5,6 +5,7 @@ // snippet-start:[s3.java2.get_bucket_policy.main] // snippet-start:[s3.java2.get_bucket_policy.import] + import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; @@ -15,9 +16,9 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ @@ -25,12 +26,12 @@ public class GetBucketPolicy { public static void main(String[] args) { final String usage = """ - Usage: - + Usage: + - Where: - bucketName - The Amazon S3 bucket to get the policy from. - """; + Where: + bucketName - The Amazon S3 bucket to get the policy from. + """; if (args.length != 1) { System.out.println(usage); @@ -41,8 +42,8 @@ public static void main(String[] args) { System.out.format("Getting policy for bucket: \"%s\"\n\n", bucketName); Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); String polText = getPolicy(s3, bucketName); System.out.println("Policy Text: " + polText); @@ -53,8 +54,8 @@ public static String getPolicy(S3Client s3, String bucketName) { String policyText; System.out.format("Getting policy for bucket: \"%s\"\n\n", bucketName); GetBucketPolicyRequest policyReq = GetBucketPolicyRequest.builder() - .bucket(bucketName) - .build(); + .bucket(bucketName) + .build(); try { GetBucketPolicyResponse policyRes = s3.getBucketPolicy(policyReq); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GetDataResponseTransformer.java b/javav2/example_code/s3/src/main/java/com/example/s3/GetDataResponseTransformer.java index 73da74e5b32..94713b01dfb 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GetDataResponseTransformer.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GetDataResponseTransformer.java @@ -5,6 +5,7 @@ // snippet-start:[s3.java2.getobjectdata.transformer.main] // snippet-start:[s3.java2.getobjectdata.transformer.import] + import software.amazon.awssdk.core.ResponseBytes; import software.amazon.awssdk.core.sync.ResponseTransformer; import software.amazon.awssdk.regions.Region; @@ -12,6 +13,7 @@ import software.amazon.awssdk.services.s3.model.GetObjectRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.GetObjectResponse; + import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -21,9 +23,9 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ @@ -31,14 +33,14 @@ public class GetDataResponseTransformer { public static void main(String[] args) { final String usage = """ - Usage: - + Usage: + - Where: - bucketName - The Amazon S3 bucket name.\s - keyName - The key name.\s - path - The path where the file is written to.\s - """; + Where: + bucketName - The Amazon S3 bucket name.\s + keyName - The key name.\s + path - The path where the file is written to.\s + """; if (args.length != 3) { System.out.println(usage); @@ -50,8 +52,8 @@ public static void main(String[] args) { String path = args[2]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); getObjectBytes(s3, bucketName, keyName, path); s3.close(); @@ -60,10 +62,10 @@ public static void main(String[] args) { public static void getObjectBytes(S3Client s3, String bucketName, String keyName, String path) { try { GetObjectRequest objectRequest = GetObjectRequest - .builder() - .key(keyName) - .bucket(bucketName) - .build(); + .builder() + .key(keyName) + .bucket(bucketName) + .build(); ResponseBytes objectBytes = s3.getObject(objectRequest, ResponseTransformer.toBytes()); byte[] data = objectBytes.asByteArray(); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectContentType.java b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectContentType.java index 00d0ccfd8fe..c359d726ef8 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectContentType.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectContentType.java @@ -5,6 +5,7 @@ // snippet-start:[s3.java2.getobjectcontenttype.main] // snippet-start:[s3.java2.getobjectcontenttype.import] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.HeadObjectRequest; @@ -15,22 +16,22 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class GetObjectContentType { public static void main(String[] args) { final String usage = """ - Usage: - > + Usage: + - Where: - bucketName - The Amazon S3 bucket name.\s - keyName - The key name.\s - """; + Where: + bucketName - The Amazon S3 bucket name.\s + keyName - The key name.\s + """; if (args.length != 2) { System.out.println(usage); @@ -41,8 +42,8 @@ public static void main(String[] args) { String keyName = args[1]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); getContentType(s3, bucketName, keyName); s3.close(); @@ -51,9 +52,9 @@ public static void main(String[] args) { public static void getContentType(S3Client s3, String bucketName, String keyName) { try { HeadObjectRequest objectRequest = HeadObjectRequest.builder() - .key(keyName) - .bucket(bucketName) - .build(); + .key(keyName) + .bucket(bucketName) + .build(); HeadObjectResponse objectHead = s3.headObject(objectRequest); String type = objectHead.contentType(); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectPresignedUrl.java b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectPresignedUrl.java index 23749c856e2..957171ef0a5 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectPresignedUrl.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectPresignedUrl.java @@ -5,11 +5,13 @@ // snippet-start:[presigned.java2.getobjectpresigned.main] // snippet-start:[presigned.java2.getobjectpresigned.import] + import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.time.Duration; + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.model.GetObjectRequest; import software.amazon.awssdk.services.s3.model.S3Exception; @@ -22,22 +24,22 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class GetObjectPresignedUrl { public static void main(String[] args) { final String USAGE = """ - Usage: - \s + Usage: + \s - Where: - bucketName - The Amazon S3 bucket name.\s - keyName - A key name that represents a text file.\s - """; + Where: + bucketName - The Amazon S3 bucket name.\s + keyName - A key name that represents a text file.\s + """; if (args.length != 2) { System.out.println(USAGE); @@ -48,8 +50,8 @@ public static void main(String[] args) { String keyName = args[1]; Region region = Region.US_EAST_1; S3Presigner presigner = S3Presigner.builder() - .region(region) - .build(); + .region(region) + .build(); getPresignedUrl(presigner, bucketName, keyName); presigner.close(); @@ -58,14 +60,14 @@ public static void main(String[] args) { public static void getPresignedUrl(S3Presigner presigner, String bucketName, String keyName) { try { GetObjectRequest getObjectRequest = GetObjectRequest.builder() - .bucket(bucketName) - .key(keyName) - .build(); + .bucket(bucketName) + .key(keyName) + .build(); GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder() - .signatureDuration(Duration.ofMinutes(60)) - .getObjectRequest(getObjectRequest) - .build(); + .signatureDuration(Duration.ofMinutes(60)) + .getObjectRequest(getObjectRequest) + .build(); PresignedGetObjectRequest presignedGetObjectRequest = presigner.presignGetObject(getObjectPresignRequest); String theUrl = presignedGetObjectRequest.url().toString(); @@ -83,7 +85,7 @@ public static void getPresignedUrl(S3Presigner presigner, String bucketName, Str connection.setDoOutput(true); try (InputStream signedPayload = presignedGetObjectRequest.signedPayload().get().asInputStream(); - OutputStream httpOutputStream = connection.getOutputStream()) { + OutputStream httpOutputStream = connection.getOutputStream()) { IoUtils.copy(signedPayload, httpOutputStream); } } diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectRestoreStatus.java b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectRestoreStatus.java index b3102d23e49..6c92ada67b7 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectRestoreStatus.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectRestoreStatus.java @@ -4,6 +4,7 @@ // snippet-start:[s3.java2.get.restore.status.main] // snippet-start:[s3.java2.get.restore.status.import] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.HeadObjectRequest; @@ -15,13 +16,13 @@ public class GetObjectRestoreStatus { public static void main(String[] args) { final String usage = """ - Usage: - \s + Usage: + \s - Where: - bucketName - The Amazon S3 bucket name.\s - keyName - A key name that represents the object.\s - """; + Where: + bucketName - The Amazon S3 bucket name.\s + keyName - A key name that represents the object.\s + """; if (args.length != 2) { System.out.println(usage); @@ -32,8 +33,8 @@ public static void main(String[] args) { String keyName = args[1]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); checkStatus(s3, bucketName, keyName); s3.close(); @@ -42,9 +43,9 @@ public static void main(String[] args) { public static void checkStatus(S3Client s3, String bucketName, String keyName) { try { HeadObjectRequest headObjectRequest = HeadObjectRequest.builder() - .bucket(bucketName) - .key(keyName) - .build(); + .bucket(bucketName) + .key(keyName) + .build(); HeadObjectResponse response = s3.headObject(headObjectRequest); System.out.println("The Amazon S3 object restoration status is " + response.restore()); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectTags.java b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectTags.java index 0cd7871218b..de5cb1edb04 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectTags.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectTags.java @@ -5,21 +5,23 @@ // snippet-start:[s3.java2.getobjecttags.main] // snippet-start:[s3.java2.getobjecttags.import] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetObjectTaggingRequest; import software.amazon.awssdk.services.s3.model.GetObjectTaggingResponse; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.Tag; + import java.util.List; // snippet-end:[s3.java2.getobjecttags.import] /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ @@ -27,13 +29,13 @@ public class GetObjectTags { public static void main(String[] args) { final String usage = """ - Usage: - \s + Usage: + \s - Where: - bucketName - The Amazon S3 bucket name.\s - keyName - A key name that represents the object.\s - """; + Where: + bucketName - The Amazon S3 bucket name.\s + keyName - A key name that represents the object.\s + """; if (args.length != 2) { System.out.println(usage); @@ -44,8 +46,8 @@ public static void main(String[] args) { String keyName = args[1]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); listTags(s3, bucketName, keyName); s3.close(); @@ -54,10 +56,10 @@ public static void main(String[] args) { public static void listTags(S3Client s3, String bucketName, String keyName) { try { GetObjectTaggingRequest getTaggingRequest = GetObjectTaggingRequest - .builder() - .key(keyName) - .bucket(bucketName) - .build(); + .builder() + .key(keyName) + .bucket(bucketName) + .build(); GetObjectTaggingResponse tags = s3.getObjectTagging(getTaggingRequest); List tagSet = tags.tagSet(); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectUrl.java b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectUrl.java index a729b8374d0..e5d96764ac2 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectUrl.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectUrl.java @@ -5,19 +5,21 @@ // snippet-start:[s3.java2.getobjecturl.main] // snippet-start:[s3.java2.getobjecturl.import] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetUrlRequest; import software.amazon.awssdk.services.s3.model.S3Exception; + import java.net.URL; // snippet-end:[s3.java2.getobjecturl.import] /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ @@ -25,13 +27,13 @@ public class GetObjectUrl { public static void main(String[] args) { final String usage = """ - Usage: - \s + Usage: + \s - Where: - bucketName - The Amazon S3 bucket name. - keyName - A key name that represents the object.\s - """; + Where: + bucketName - The Amazon S3 bucket name. + keyName - A key name that represents the object.\s + """; if (args.length != 2) { System.out.println(usage); @@ -42,8 +44,8 @@ public static void main(String[] args) { String keyName = args[1]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); getURL(s3, bucketName, keyName); s3.close(); @@ -52,9 +54,9 @@ public static void main(String[] args) { public static void getURL(S3Client s3, String bucketName, String keyName) { try { GetUrlRequest request = GetUrlRequest.builder() - .bucket(bucketName) - .key(keyName) - .build(); + .bucket(bucketName) + .key(keyName) + .build(); URL url = s3.utilities().getUrl(request); System.out.println("The URL for " + keyName + " is " + url); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/HelloS3.java b/javav2/example_code/s3/src/main/java/com/example/s3/HelloS3.java index b62cf0f35cf..cad4df590b0 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/HelloS3.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/HelloS3.java @@ -4,27 +4,29 @@ package com.example.s3; // snippet-start:[s3.java2.hello.main] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.Bucket; import software.amazon.awssdk.services.s3.model.ListBucketsResponse; import software.amazon.awssdk.services.s3.model.S3Exception; + import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class HelloS3 { public static void main(String[] args) { Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); listBuckets(s3); } diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/KMSEncryptionExample.java b/javav2/example_code/s3/src/main/java/com/example/s3/KMSEncryptionExample.java index 217eb4f7ad4..b3fd9109350 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/KMSEncryptionExample.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/KMSEncryptionExample.java @@ -5,6 +5,7 @@ // snippet-start:[s3.java2.kms.main] // snippet-start:[s3.java2.kms.import] + import software.amazon.awssdk.core.ResponseBytes; import software.amazon.awssdk.core.SdkBytes; import software.amazon.awssdk.core.sync.RequestBody; @@ -20,6 +21,7 @@ import software.amazon.awssdk.services.s3.model.PutObjectRequest; import software.amazon.awssdk.services.s3.model.GetObjectRequest; import software.amazon.awssdk.services.s3.model.GetObjectResponse; + import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; @@ -32,14 +34,13 @@ * Key Management Service. * For information, see "Creating keys" in the AWS Key Management Service * Developer Guide. - * + *

* In addition, before running this Java V2 code example, set up your * development environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html - * */ public class KMSEncryptionExample { @@ -47,16 +48,16 @@ public class KMSEncryptionExample { public static void main(String[] args) { final String usage = """ - Usage: - + Usage: + - Where: - objectName - The name of the object.\s - bucketName - The Amazon S3 bucket name that contains the object (for example, bucket1).\s - objectPath - The path to a TXT file to encrypt and place into a Amazon S3 bucket (for example, C:/AWS/test.txt). - outPath - The path where a text file is written to after it's decrypted (for example, C:/AWS/testPlain.txt). - keyId - The id of the AWS KMS key to use to encrpt/decrypt the data. You can obtain the key ID value from the AWS Management Console. - """; + Where: + objectName - The name of the object.\s + bucketName - The Amazon S3 bucket name that contains the object (for example, bucket1).\s + objectPath - The path to a TXT file to encrypt and place into a Amazon S3 bucket (for example, C:/AWS/test.txt). + outPath - The path where a text file is written to after it's decrypted (for example, C:/AWS/testPlain.txt). + keyId - The id of the AWS KMS key to use to encrpt/decrypt the data. You can obtain the key ID value from the AWS Management Console. + """; if (args.length != 5) { System.out.println(usage); @@ -70,8 +71,8 @@ public static void main(String[] args) { String keyId = args[4]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); putEncryptData(s3, objectName, bucketName, objectPath, keyId); getEncryptedData(s3, bucketName, objectName, outPath, keyId); @@ -80,16 +81,14 @@ public static void main(String[] args) { // Encrypt data and place the encrypted data into an Amazon S3 bucket. public static void putEncryptData(S3Client s3, String objectName, String bucketName, String objectPath, - String keyId) { + String keyId) { try { PutObjectRequest objectRequest = PutObjectRequest.builder() - .bucket(bucketName) - .key(objectName) - .build(); + .bucket(bucketName) + .key(objectName) + .build(); byte[] myData = getObjectFile(objectPath); - - // Encrypt the data by using the AWS Key Management Service. byte[] encryptData = encryptData(keyId, myData); s3.putObject(objectRequest, RequestBody.fromBytes(encryptData)); @@ -103,9 +102,9 @@ public static void putEncryptData(S3Client s3, String objectName, String bucketN public static void getEncryptedData(S3Client s3, String bucketName, String objectName, String path, String keyId) { try { GetObjectRequest objectRequest = GetObjectRequest.builder() - .key(objectName) - .bucket(bucketName) - .build(); + .key(objectName) + .bucket(bucketName) + .build(); // Get the byte[] from the Amazon S3 bucket. ResponseBytes objectBytes = s3.getObjectAsBytes(objectRequest); @@ -136,9 +135,9 @@ private static byte[] encryptData(String keyId, byte[] data) { SdkBytes myBytes = SdkBytes.fromByteArray(data); EncryptRequest encryptRequest = EncryptRequest.builder() - .keyId(keyId) - .plaintext(myBytes) - .build(); + .keyId(keyId) + .plaintext(myBytes) + .build(); EncryptResponse response = kmsClient.encrypt(encryptRequest); String algorithm = response.encryptionAlgorithm().toString(); @@ -164,9 +163,9 @@ private static byte[] decryptData(byte[] data, String keyId) { SdkBytes encryptedData = SdkBytes.fromByteArray(data); DecryptRequest decryptRequest = DecryptRequest.builder() - .ciphertextBlob(encryptedData) - .keyId(keyId) - .build(); + .ciphertextBlob(encryptedData) + .keyId(keyId) + .build(); DecryptResponse decryptResponse = kmsClient.decrypt(decryptRequest); SdkBytes plainText = decryptResponse.plaintext(); @@ -213,8 +212,8 @@ private static KmsClient getKMSClient() { Region region = Region.US_EAST_1; return KmsClient.builder() - .region(region) - .build(); + .region(region) + .build(); } } // snippet-end:[s3.java2.kms.main] diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/LifecycleConfiguration.java b/javav2/example_code/s3/src/main/java/com/example/s3/LifecycleConfiguration.java index 65664c27839..dc96e167c18 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/LifecycleConfiguration.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/LifecycleConfiguration.java @@ -5,6 +5,7 @@ // snippet-start:[s3.java2.manage_lifecycle.main] // snippet-start:[s3.java2.manage_lifecycle.import] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.LifecycleRuleFilter; @@ -25,177 +26,177 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class LifecycleConfiguration { - public static void main(String[] args) { - final String usage = """ - - Usage: - \s - - Where: - bucketName - The Amazon Simple Storage Service (Amazon S3) bucket to upload an object into. - accountId - The id of the account that owns the Amazon S3 bucket. - """; - - if (args.length != 2) { - System.out.println(usage); - System.exit(1); - } - - String bucketName = args[0]; - String accountId = args[1]; - Region region = Region.US_EAST_1; - S3Client s3 = S3Client.builder() - .region(region) - .build(); - - setLifecycleConfig(s3, bucketName, accountId); - getLifecycleConfig(s3, bucketName, accountId); - deleteLifecycleConfig(s3, bucketName, accountId); - System.out.println("You have successfully created, updated, and deleted a Lifecycle configuration"); - s3.close(); - } + public static void main(String[] args) { + final String usage = """ - public static void setLifecycleConfig(S3Client s3, String bucketName, String accountId) { - try { - // Create a rule to archive objects with the "glacierobjects/" prefix to Amazon - // S3 Glacier. - LifecycleRuleFilter ruleFilter = LifecycleRuleFilter.builder() - .prefix("glacierobjects/") - .build(); - - Transition transition = Transition.builder() - .storageClass(TransitionStorageClass.GLACIER) - .days(0) - .build(); - - LifecycleRule rule1 = LifecycleRule.builder() - .id("Archive immediately rule") - .filter(ruleFilter) - .transitions(transition) - .status(ExpirationStatus.ENABLED) - .build(); - - // Create a second rule. - Transition transition2 = Transition.builder() - .storageClass(TransitionStorageClass.GLACIER) - .days(0) - .build(); - - List transitionList = new ArrayList<>(); - transitionList.add(transition2); - - LifecycleRuleFilter ruleFilter2 = LifecycleRuleFilter.builder() - .prefix("glacierobjects/") - .build(); - - LifecycleRule rule2 = LifecycleRule.builder() - .id("Archive and then delete rule") - .filter(ruleFilter2) - .transitions(transitionList) - .status(ExpirationStatus.ENABLED) - .build(); - - // Add the LifecycleRule objects to an ArrayList. - ArrayList ruleList = new ArrayList<>(); - ruleList.add(rule1); - ruleList.add(rule2); - - BucketLifecycleConfiguration lifecycleConfiguration = BucketLifecycleConfiguration.builder() - .rules(ruleList) - .build(); - - PutBucketLifecycleConfigurationRequest putBucketLifecycleConfigurationRequest = PutBucketLifecycleConfigurationRequest - .builder() - .bucket(bucketName) - .lifecycleConfiguration(lifecycleConfiguration) - .expectedBucketOwner(accountId) - .build(); - - s3.putBucketLifecycleConfiguration(putBucketLifecycleConfigurationRequest); - - } catch (S3Exception e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } - } + Usage: + \s - // Retrieve the configuration and add a new rule. - public static void getLifecycleConfig(S3Client s3, String bucketName, String accountId) { - try { - GetBucketLifecycleConfigurationRequest getBucketLifecycleConfigurationRequest = GetBucketLifecycleConfigurationRequest - .builder() - .bucket(bucketName) - .expectedBucketOwner(accountId) - .build(); - - GetBucketLifecycleConfigurationResponse response = s3 - .getBucketLifecycleConfiguration(getBucketLifecycleConfigurationRequest); - List newList = new ArrayList<>(); - List rules = response.rules(); - for (LifecycleRule rule : rules) { - newList.add(rule); - } - - // Add a new rule with both a prefix predicate and a tag predicate. - LifecycleRuleFilter ruleFilter = LifecycleRuleFilter.builder() - .prefix("YearlyDocuments/") - .build(); - - Transition transition = Transition.builder() - .storageClass(TransitionStorageClass.GLACIER) - .days(3650) - .build(); - - LifecycleRule rule1 = LifecycleRule.builder() - .id("NewRule") - .filter(ruleFilter) - .transitions(transition) - .status(ExpirationStatus.ENABLED) - .build(); - - // Add the new rule to the list. - newList.add(rule1); - BucketLifecycleConfiguration lifecycleConfiguration = BucketLifecycleConfiguration.builder() - .rules(newList) - .build(); - - PutBucketLifecycleConfigurationRequest putBucketLifecycleConfigurationRequest = PutBucketLifecycleConfigurationRequest - .builder() - .bucket(bucketName) - .lifecycleConfiguration(lifecycleConfiguration) - .expectedBucketOwner(accountId) - .build(); - - s3.putBucketLifecycleConfiguration(putBucketLifecycleConfigurationRequest); - - } catch (S3Exception e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } + Where: + bucketName - The Amazon Simple Storage Service (Amazon S3) bucket to upload an object into. + accountId - The id of the account that owns the Amazon S3 bucket. + """; + + if (args.length != 2) { + System.out.println(usage); + System.exit(1); } - // Delete the configuration from the Amazon S3 bucket. - public static void deleteLifecycleConfig(S3Client s3, String bucketName, String accountId) { - try { - DeleteBucketLifecycleRequest deleteBucketLifecycleRequest = DeleteBucketLifecycleRequest - .builder() - .bucket(bucketName) - .expectedBucketOwner(accountId) - .build(); - - s3.deleteBucketLifecycle(deleteBucketLifecycleRequest); - - } catch (S3Exception e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } + String bucketName = args[0]; + String accountId = args[1]; + Region region = Region.US_EAST_1; + S3Client s3 = S3Client.builder() + .region(region) + .build(); + + setLifecycleConfig(s3, bucketName, accountId); + getLifecycleConfig(s3, bucketName, accountId); + deleteLifecycleConfig(s3, bucketName, accountId); + System.out.println("You have successfully created, updated, and deleted a Lifecycle configuration"); + s3.close(); + } + + public static void setLifecycleConfig(S3Client s3, String bucketName, String accountId) { + try { + // Create a rule to archive objects with the "glacierobjects/" prefix to Amazon + // S3 Glacier. + LifecycleRuleFilter ruleFilter = LifecycleRuleFilter.builder() + .prefix("glacierobjects/") + .build(); + + Transition transition = Transition.builder() + .storageClass(TransitionStorageClass.GLACIER) + .days(0) + .build(); + + LifecycleRule rule1 = LifecycleRule.builder() + .id("Archive immediately rule") + .filter(ruleFilter) + .transitions(transition) + .status(ExpirationStatus.ENABLED) + .build(); + + // Create a second rule. + Transition transition2 = Transition.builder() + .storageClass(TransitionStorageClass.GLACIER) + .days(0) + .build(); + + List transitionList = new ArrayList<>(); + transitionList.add(transition2); + + LifecycleRuleFilter ruleFilter2 = LifecycleRuleFilter.builder() + .prefix("glacierobjects/") + .build(); + + LifecycleRule rule2 = LifecycleRule.builder() + .id("Archive and then delete rule") + .filter(ruleFilter2) + .transitions(transitionList) + .status(ExpirationStatus.ENABLED) + .build(); + + // Add the LifecycleRule objects to an ArrayList. + ArrayList ruleList = new ArrayList<>(); + ruleList.add(rule1); + ruleList.add(rule2); + + BucketLifecycleConfiguration lifecycleConfiguration = BucketLifecycleConfiguration.builder() + .rules(ruleList) + .build(); + + PutBucketLifecycleConfigurationRequest putBucketLifecycleConfigurationRequest = PutBucketLifecycleConfigurationRequest + .builder() + .bucket(bucketName) + .lifecycleConfiguration(lifecycleConfiguration) + .expectedBucketOwner(accountId) + .build(); + + s3.putBucketLifecycleConfiguration(putBucketLifecycleConfigurationRequest); + + } catch (S3Exception e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + } + + // Retrieve the configuration and add a new rule. + public static void getLifecycleConfig(S3Client s3, String bucketName, String accountId) { + try { + GetBucketLifecycleConfigurationRequest getBucketLifecycleConfigurationRequest = GetBucketLifecycleConfigurationRequest + .builder() + .bucket(bucketName) + .expectedBucketOwner(accountId) + .build(); + + GetBucketLifecycleConfigurationResponse response = s3 + .getBucketLifecycleConfiguration(getBucketLifecycleConfigurationRequest); + List newList = new ArrayList<>(); + List rules = response.rules(); + for (LifecycleRule rule : rules) { + newList.add(rule); + } + + // Add a new rule with both a prefix predicate and a tag predicate. + LifecycleRuleFilter ruleFilter = LifecycleRuleFilter.builder() + .prefix("YearlyDocuments/") + .build(); + + Transition transition = Transition.builder() + .storageClass(TransitionStorageClass.GLACIER) + .days(3650) + .build(); + + LifecycleRule rule1 = LifecycleRule.builder() + .id("NewRule") + .filter(ruleFilter) + .transitions(transition) + .status(ExpirationStatus.ENABLED) + .build(); + + // Add the new rule to the list. + newList.add(rule1); + BucketLifecycleConfiguration lifecycleConfiguration = BucketLifecycleConfiguration.builder() + .rules(newList) + .build(); + + PutBucketLifecycleConfigurationRequest putBucketLifecycleConfigurationRequest = PutBucketLifecycleConfigurationRequest + .builder() + .bucket(bucketName) + .lifecycleConfiguration(lifecycleConfiguration) + .expectedBucketOwner(accountId) + .build(); + + s3.putBucketLifecycleConfiguration(putBucketLifecycleConfigurationRequest); + + } catch (S3Exception e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); + } + } + + // Delete the configuration from the Amazon S3 bucket. + public static void deleteLifecycleConfig(S3Client s3, String bucketName, String accountId) { + try { + DeleteBucketLifecycleRequest deleteBucketLifecycleRequest = DeleteBucketLifecycleRequest + .builder() + .bucket(bucketName) + .expectedBucketOwner(accountId) + .build(); + + s3.deleteBucketLifecycle(deleteBucketLifecycleRequest); + + } catch (S3Exception e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); } + } } // snippet-end:[s3.java2.manage_lifecycle.main] diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/ListObjectsPaginated.java b/javav2/example_code/s3/src/main/java/com/example/s3/ListObjectsPaginated.java index 77f181e44e8..563ca096b8d 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/ListObjectsPaginated.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/ListObjectsPaginated.java @@ -4,6 +4,7 @@ package com.example.s3; // snippet-start:[s3.java2.list_objects.pag.main] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.ListObjectsV2Request; @@ -14,12 +15,12 @@ public class ListObjectsPaginated { public static void main(String[] args) { final String usage = """ - Usage: - \s + Usage: + \s - Where: - bucketName - The Amazon S3 bucket from which objects are read.\s - """; + Where: + bucketName - The Amazon S3 bucket from which objects are read.\s + """; if (args.length != 1) { System.out.println(usage); @@ -29,8 +30,8 @@ public static void main(String[] args) { String bucketName = args[0]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); listBucketObjects(s3, bucketName); s3.close(); @@ -39,14 +40,14 @@ public static void main(String[] args) { public static void listBucketObjects(S3Client s3, String bucketName) { try { ListObjectsV2Request listReq = ListObjectsV2Request.builder() - .bucket(bucketName) - .maxKeys(1) - .build(); + .bucket(bucketName) + .maxKeys(1) + .build(); ListObjectsV2Iterable listRes = s3.listObjectsV2Paginator(listReq); listRes.stream() - .flatMap(r -> r.contents().stream()) - .forEach(content -> System.out.println(" Key: " + content.key() + " size = " + content.size())); + .flatMap(r -> r.contents().stream()) + .forEach(content -> System.out.println(" Key: " + content.key() + " size = " + content.size())); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/ManagingObjectTags.java b/javav2/example_code/s3/src/main/java/com/example/s3/ManagingObjectTags.java index 31c90f2cde6..ba27e65548c 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/ManagingObjectTags.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/ManagingObjectTags.java @@ -4,6 +4,7 @@ package com.example.s3; // snippet-start:[s3.java2.s3_object_manage_tags.import] + import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; @@ -14,6 +15,7 @@ import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.GetObjectTaggingRequest; import software.amazon.awssdk.services.s3.model.GetObjectTaggingResponse; + import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -24,23 +26,23 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ManagingObjectTags { public static void main(String[] args) { final String usage = """ - Usage: - \s + Usage: + \s - Where: - bucketName - The Amazon S3 bucket. - objectKey - The object that a tag is applied (for example, book.pdf). - objectPath - The path where the file is located (for example, C:/AWS/book2.pdf).\s - """; + Where: + bucketName - The Amazon S3 bucket. + objectKey - The object that a tag is applied (for example, book.pdf). + objectPath - The path where the file is located (for example, C:/AWS/book2.pdf).\s + """; if (args.length != 3) { System.out.println(usage); @@ -54,8 +56,8 @@ public static void main(String[] args) { System.out.println(" in bucket: " + bucketName); Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); putS3ObjectTags(s3, bucketName, objectKey, objectPath); updateObjectTags(s3, bucketName, objectKey); @@ -66,28 +68,28 @@ public static void main(String[] args) { public static void putS3ObjectTags(S3Client s3, String bucketName, String objectKey, String objectPath) { try { Tag tag1 = Tag.builder() - .key("Tag 1") - .value("This is tag 1") - .build(); + .key("Tag 1") + .value("This is tag 1") + .build(); Tag tag2 = Tag.builder() - .key("Tag 2") - .value("This is tag 2") - .build(); + .key("Tag 2") + .value("This is tag 2") + .build(); List tags = new ArrayList<>(); tags.add(tag1); tags.add(tag2); Tagging allTags = Tagging.builder() - .tagSet(tags) - .build(); + .tagSet(tags) + .build(); PutObjectRequest putOb = PutObjectRequest.builder() - .bucket(bucketName) - .key(objectKey) - .tagging(allTags) - .build(); + .bucket(bucketName) + .key(objectKey) + .tagging(allTags) + .build(); s3.putObject(putOb, RequestBody.fromBytes(getObjectFile(objectPath))); @@ -100,9 +102,9 @@ public static void putS3ObjectTags(S3Client s3, String bucketName, String object public static void updateObjectTags(S3Client s3, String bucketName, String objectKey) { try { GetObjectTaggingRequest taggingRequest = GetObjectTaggingRequest.builder() - .bucket(bucketName) - .key(objectKey) - .build(); + .bucket(bucketName) + .key(objectKey) + .build(); GetObjectTaggingResponse getTaggingRes = s3.getObjectTagging(taggingRequest); List obTags = getTaggingRes.tagSet(); @@ -113,28 +115,28 @@ public static void updateObjectTags(S3Client s3, String bucketName, String objec // Replace the object's tags with two new tags. Tag tag3 = Tag.builder() - .key("Tag 3") - .value("This is tag 3") - .build(); + .key("Tag 3") + .value("This is tag 3") + .build(); Tag tag4 = Tag.builder() - .key("Tag 4") - .value("This is tag 4") - .build(); + .key("Tag 4") + .value("This is tag 4") + .build(); List tags = new ArrayList<>(); tags.add(tag3); tags.add(tag4); Tagging updatedTags = Tagging.builder() - .tagSet(tags) - .build(); + .tagSet(tags) + .build(); PutObjectTaggingRequest taggingRequest1 = PutObjectTaggingRequest.builder() - .bucket(bucketName) - .key(objectKey) - .tagging(updatedTags) - .build(); + .bucket(bucketName) + .key(objectKey) + .tagging(updatedTags) + .build(); s3.putObjectTagging(taggingRequest1); GetObjectTaggingResponse getTaggingRes2 = s3.getObjectTagging(taggingRequest); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/PerformMultiPartUpload.java b/javav2/example_code/s3/src/main/java/com/example/s3/PerformMultiPartUpload.java index 67d6ee5c3b3..65b41814479 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/PerformMultiPartUpload.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/PerformMultiPartUpload.java @@ -4,6 +4,7 @@ package com.example.s3; // snippet-start:[s3.java2.performMultiPartUpload.import] + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.core.exception.SdkException; @@ -37,7 +38,7 @@ // snippet-start:[s3.java2.performMultiPartUpload.full] public class PerformMultiPartUpload { static final S3Client s3Client = S3Client.create(); - static final String bucketName = "x-" + UUID.randomUUID(); + static final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); static final String key = UUID.randomUUID().toString(); static final String classPathFilePath = "/multipartUploadFiles/s3-userguide.pdf"; static final String filePath = getFullFilePath(classPathFilePath); @@ -77,11 +78,11 @@ static void deleteResources() { public void multipartUploadWithTransferManager(String filePath) { S3TransferManager transferManager = S3TransferManager.create(); UploadFileRequest uploadFileRequest = UploadFileRequest.builder() - .putObjectRequest(b -> b - .bucket(bucketName) - .key(key)) - .source(Paths.get(filePath)) - .build(); + .putObjectRequest(b -> b + .bucket(bucketName) + .key(key)) + .source(Paths.get(filePath)) + .build(); FileUpload fileUpload = transferManager.uploadFile(uploadFileRequest); fileUpload.completionFuture().join(); transferManager.close(); @@ -93,8 +94,8 @@ public void multipartUploadWithS3Client(String filePath) { // Initiate the multipart upload. CreateMultipartUploadResponse createMultipartUploadResponse = s3Client.createMultipartUpload(b -> b - .bucket(bucketName) - .key(key)); + .bucket(bucketName) + .key(key)); String uploadId = createMultipartUploadResponse.uploadId(); // Upload the parts of the file. @@ -111,20 +112,20 @@ public void multipartUploadWithS3Client(String filePath) { bb.flip(); // Swap position and limit before reading from the buffer. UploadPartRequest uploadPartRequest = UploadPartRequest.builder() - .bucket(bucketName) - .key(key) - .uploadId(uploadId) - .partNumber(partNumber) - .build(); + .bucket(bucketName) + .key(key) + .uploadId(uploadId) + .partNumber(partNumber) + .build(); UploadPartResponse partResponse = s3Client.uploadPart( - uploadPartRequest, - RequestBody.fromByteBuffer(bb)); + uploadPartRequest, + RequestBody.fromByteBuffer(bb)); CompletedPart part = CompletedPart.builder() - .partNumber(partNumber) - .eTag(partResponse.eTag()) - .build(); + .partNumber(partNumber) + .eTag(partResponse.eTag()) + .build(); completedParts.add(part); bb.clear(); @@ -137,10 +138,10 @@ public void multipartUploadWithS3Client(String filePath) { // Complete the multipart upload. s3Client.completeMultipartUpload(b -> b - .bucket(bucketName) - .key(key) - .uploadId(uploadId) - .multipartUpload(CompletedMultipartUpload.builder().parts(completedParts).build())); + .bucket(bucketName) + .key(key) + .uploadId(uploadId) + .multipartUpload(CompletedMultipartUpload.builder().parts(completedParts).build())); } // snippet-end:[s3.java2.performMultiPartUpload.s3Client] @@ -148,13 +149,13 @@ public void multipartUploadWithS3Client(String filePath) { public void multipartUploadWithS3AsyncClient(String filePath) { // Enable multipart support. S3AsyncClient s3AsyncClient = S3AsyncClient.builder() - .multipartEnabled(true) - .build(); + .multipartEnabled(true) + .build(); CompletableFuture response = s3AsyncClient.putObject(b -> b - .bucket(bucketName) - .key(key), - Paths.get(filePath)); + .bucket(bucketName) + .key(key), + Paths.get(filePath)); response.join(); logger.info("File uploaded in multiple 8 MiB parts using S3AsyncClient."); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/PutBucketLogging.java b/javav2/example_code/s3/src/main/java/com/example/s3/PutBucketLogging.java index 54fa452e874..cd9873f93ca 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/PutBucketLogging.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/PutBucketLogging.java @@ -5,6 +5,7 @@ // snippet-start:[s3.java2.s3_put_log.main] // snippet-start:[s3.java2.s3_put_log.import] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetBucketAclRequest; @@ -16,6 +17,7 @@ import software.amazon.awssdk.services.s3.model.PutBucketLoggingRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.TargetGrant; + import java.util.ArrayList; import java.util.List; // snippet-end:[s3.java2.s3_put_log.import] @@ -23,81 +25,81 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class PutBucketLogging { - public static void main(String[] args) { - final String usage = """ - - Usage: - \s - - Where: - bucketName - The Amazon S3 bucket to upload an object into. - targetBucket - The target bucket . - """; - - if (args.length != 3) { - System.out.println(usage); - System.exit(1); - } - - String bucketName = args[0]; - String targetBucket = args[1]; - Region region = Region.US_EAST_1; - S3Client s3 = S3Client.builder() - .region(region) - .build(); - - setlogRequest(s3, bucketName, targetBucket); - s3.close(); + public static void main(String[] args) { + final String usage = """ + + Usage: + \s + + Where: + bucketName - The Amazon S3 bucket to upload an object into. + targetBucket - The target bucket . + """; + + if (args.length != 3) { + System.out.println(usage); + System.exit(1); } - public static void setlogRequest(S3Client s3, String bucketName, String targetBucket) { - try { - GetBucketAclRequest aclRequest = GetBucketAclRequest.builder() - .bucket(targetBucket) - .build(); - - s3.getBucketAcl(aclRequest); - Grantee grantee = Grantee.builder() - .type(Type.GROUP) - .uri("http://acs.amazonaws.com/groups/s3/LogDelivery") - .build(); - - TargetGrant targetGrant = TargetGrant.builder() - .grantee(grantee) - .permission(BucketLogsPermission.FULL_CONTROL) - .build(); - - List granteeList = new ArrayList<>(); - granteeList.add(targetGrant); - - LoggingEnabled loggingEnabled = LoggingEnabled.builder() - .targetBucket(targetBucket) - .targetGrants(granteeList) - .build(); - - BucketLoggingStatus loggingStatus = BucketLoggingStatus.builder() - .loggingEnabled(loggingEnabled) - .build(); - - PutBucketLoggingRequest loggingRequest = PutBucketLoggingRequest.builder() - .bucket(bucketName) - .expectedBucketOwner("814548047983") - .bucketLoggingStatus(loggingStatus) - .build(); - - s3.putBucketLogging(loggingRequest); - System.out.println("Enabling logging for the target bucket " + targetBucket); - - } catch (S3Exception e) { - System.err.println(e.getMessage()); - System.exit(1); - } + String bucketName = args[0]; + String targetBucket = args[1]; + Region region = Region.US_EAST_1; + S3Client s3 = S3Client.builder() + .region(region) + .build(); + + setlogRequest(s3, bucketName, targetBucket); + s3.close(); + } + + public static void setlogRequest(S3Client s3, String bucketName, String targetBucket) { + try { + GetBucketAclRequest aclRequest = GetBucketAclRequest.builder() + .bucket(targetBucket) + .build(); + + s3.getBucketAcl(aclRequest); + Grantee grantee = Grantee.builder() + .type(Type.GROUP) + .uri("http://acs.amazonaws.com/groups/s3/LogDelivery") + .build(); + + TargetGrant targetGrant = TargetGrant.builder() + .grantee(grantee) + .permission(BucketLogsPermission.FULL_CONTROL) + .build(); + + List granteeList = new ArrayList<>(); + granteeList.add(targetGrant); + + LoggingEnabled loggingEnabled = LoggingEnabled.builder() + .targetBucket(targetBucket) + .targetGrants(granteeList) + .build(); + + BucketLoggingStatus loggingStatus = BucketLoggingStatus.builder() + .loggingEnabled(loggingEnabled) + .build(); + + PutBucketLoggingRequest loggingRequest = PutBucketLoggingRequest.builder() + .bucket(bucketName) + .expectedBucketOwner("814548047983") + .bucketLoggingStatus(loggingStatus) + .build(); + + s3.putBucketLogging(loggingRequest); + System.out.println("Enabling logging for the target bucket " + targetBucket); + + } catch (S3Exception e) { + System.err.println(e.getMessage()); + System.exit(1); } + } } // snippet-end:[s3.java2.s3_put_log.main] diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/PutObjectMetadata.java b/javav2/example_code/s3/src/main/java/com/example/s3/PutObjectMetadata.java index 231d51e9693..173bf33057c 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/PutObjectMetadata.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/PutObjectMetadata.java @@ -5,11 +5,13 @@ // snippet-start:[s3.java2.s3_object_upload.metadata.main] // snippet-start:[s3.java2.s3_object_upload.metadata.import] + import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.PutObjectRequest; import software.amazon.awssdk.services.s3.model.S3Exception; + import java.io.File; import java.util.HashMap; import java.util.Map; @@ -18,23 +20,23 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class PutObjectMetadata { public static void main(String[] args) { final String USAGE = """ - Usage: - \s + Usage: + \s - Where: - bucketName - The Amazon S3 bucket to upload an object into. - objectKey - The object to upload (for example, book.pdf). - objectPath - The path where the file is located (for example, C:/AWS/book2.pdf).\s - """; + Where: + bucketName - The Amazon S3 bucket to upload an object into. + objectKey - The object to upload (for example, book.pdf). + objectPath - The path where the file is located (for example, C:/AWS/book2.pdf).\s + """; if (args.length != 3) { System.out.println(USAGE); @@ -48,8 +50,8 @@ public static void main(String[] args) { System.out.println(" in bucket: " + bucketName); Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); putS3Object(s3, bucketName, objectKey, objectPath); s3.close(); @@ -64,10 +66,10 @@ public static void putS3Object(S3Client s3, String bucketName, String objectKey, metadata.put("version", "1.0.0.0"); PutObjectRequest putOb = PutObjectRequest.builder() - .bucket(bucketName) - .key(objectKey) - .metadata(metadata) - .build(); + .bucket(bucketName) + .key(objectKey) + .metadata(metadata) + .build(); s3.putObject(putOb, RequestBody.fromFile(new File(objectPath))); System.out.println("Successfully placed " + objectKey + " into bucket " + bucketName); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/PutObjectRetention.java b/javav2/example_code/s3/src/main/java/com/example/s3/PutObjectRetention.java index 36e4cfd7210..e5677849842 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/PutObjectRetention.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/PutObjectRetention.java @@ -5,11 +5,13 @@ // snippet-start:[s3.java2.retention_object.main] // snippet-start:[s3.java2.retention_object.import] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.PutObjectRetentionRequest; import software.amazon.awssdk.services.s3.model.ObjectLockRetention; import software.amazon.awssdk.services.s3.model.S3Exception; + import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; @@ -19,9 +21,9 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ @@ -29,13 +31,13 @@ public class PutObjectRetention { public static void main(String[] args) { final String usage = """ - Usage: - \s + Usage: + \s - Where: - key - The name of the object (for example, book.pdf).\s - bucketName - The Amazon S3 bucket name that contains the object (for example, bucket1).\s - """; + Where: + key - The name of the object (for example, book.pdf).\s + bucketName - The Amazon S3 bucket name that contains the object (for example, bucket1).\s + """; if (args.length != 2) { System.out.println(usage); @@ -46,8 +48,8 @@ public static void main(String[] args) { String bucketName = args[1]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); setRentionPeriod(s3, key, bucketName); s3.close(); @@ -60,16 +62,16 @@ public static void setRentionPeriod(S3Client s3, String key, String bucket) { Instant instant = localDateTime.toInstant(ZoneOffset.UTC); ObjectLockRetention lockRetention = ObjectLockRetention.builder() - .mode("COMPLIANCE") - .retainUntilDate(instant) - .build(); + .mode("COMPLIANCE") + .retainUntilDate(instant) + .build(); PutObjectRetentionRequest retentionRequest = PutObjectRetentionRequest.builder() - .bucket(bucket) - .key(key) - .bypassGovernanceRetention(true) - .retention(lockRetention) - .build(); + .bucket(bucket) + .key(key) + .bypassGovernanceRetention(true) + .retention(lockRetention) + .build(); // To set Retention on an object, the Amazon S3 bucket must support object // locking, otherwise an exception is thrown. diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/RestoreObject.java b/javav2/example_code/s3/src/main/java/com/example/s3/RestoreObject.java index 56c02bc09b7..e1bb594bcb9 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/RestoreObject.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/RestoreObject.java @@ -5,6 +5,7 @@ // snippet-start:[s3.java2.restore_object.main] // snippet-start:[s3.java2.restore_object.import] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.RestoreRequest; @@ -28,14 +29,14 @@ public class RestoreObject { public static void main(String[] args) { final String usage = """ - Usage: - + Usage: + - Where: - bucketName - The Amazon S3 bucket name.\s - keyName - The key name of an object with a Storage class value of Glacier.\s - expectedBucketOwner - The account that owns the bucket (you can obtain this value from the AWS Management Console).\s - """; + Where: + bucketName - The Amazon S3 bucket name.\s + keyName - The key name of an object with a Storage class value of Glacier.\s + expectedBucketOwner - The account that owns the bucket (you can obtain this value from the AWS Management Console).\s + """; if (args.length != 3) { System.out.println(usage); @@ -47,8 +48,8 @@ public static void main(String[] args) { String expectedBucketOwner = args[2]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); restoreS3Object(s3, bucketName, keyName, expectedBucketOwner); s3.close(); @@ -57,16 +58,16 @@ public static void main(String[] args) { public static void restoreS3Object(S3Client s3, String bucketName, String keyName, String expectedBucketOwner) { try { RestoreRequest restoreRequest = RestoreRequest.builder() - .days(10) - .glacierJobParameters(GlacierJobParameters.builder().tier(Tier.STANDARD).build()) - .build(); + .days(10) + .glacierJobParameters(GlacierJobParameters.builder().tier(Tier.STANDARD).build()) + .build(); RestoreObjectRequest objectRequest = RestoreObjectRequest.builder() - .expectedBucketOwner(expectedBucketOwner) - .bucket(bucketName) - .key(keyName) - .restoreRequest(restoreRequest) - .build(); + .expectedBucketOwner(expectedBucketOwner) + .bucket(bucketName) + .key(keyName) + .restoreRequest(restoreRequest) + .build(); s3.restoreObject(objectRequest); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/S3BucketDeletion.java b/javav2/example_code/s3/src/main/java/com/example/s3/S3BucketDeletion.java index a73be7dcf73..999f4ede219 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/S3BucketDeletion.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/S3BucketDeletion.java @@ -6,6 +6,7 @@ // snippet-start:[s3.java2.s3_bucket_deletion.delete_objects] // snippet-start:[s3.java2.bucket_deletion.import] // snippet-start:[s3.java2.s3_bucket_ops.delete_bucket.import] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.DeleteBucketRequest; @@ -20,21 +21,21 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class S3BucketDeletion { public static void main(String[] args) throws Exception { final String usage = """ - Usage: - + Usage: + - Where: - bucket - The bucket to delete (for example, bucket1).\s - """; + Where: + bucket - The bucket to delete (for example, bucket1).\s + """; if (args.length != 1) { System.out.println(usage); @@ -44,8 +45,8 @@ public static void main(String[] args) throws Exception { String bucket = args[0]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); deleteObjectsInBucket(s3, bucket); s3.close(); @@ -55,17 +56,17 @@ public static void deleteObjectsInBucket(S3Client s3, String bucket) { try { // To delete a bucket, all the objects in the bucket must be deleted first. ListObjectsV2Request listObjectsV2Request = ListObjectsV2Request.builder() - .bucket(bucket) - .build(); + .bucket(bucket) + .build(); ListObjectsV2Response listObjectsV2Response; do { listObjectsV2Response = s3.listObjectsV2(listObjectsV2Request); for (S3Object s3Object : listObjectsV2Response.contents()) { DeleteObjectRequest request = DeleteObjectRequest.builder() - .bucket(bucket) - .key(s3Object.key()) - .build(); + .bucket(bucket) + .key(s3Object.key()) + .build(); s3.deleteObject(request); } } while (listObjectsV2Response.isTruncated()); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/S3Cors.java b/javav2/example_code/s3/src/main/java/com/example/s3/S3Cors.java index 43fd689d70e..7276c01be8b 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/S3Cors.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/S3Cors.java @@ -5,10 +5,13 @@ // snippet-start:[s3.java2.cors.main] // snippet-start:[s3.java2.cors.import] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; + import java.util.ArrayList; import java.util.List; + import software.amazon.awssdk.services.s3.model.GetBucketCorsRequest; import software.amazon.awssdk.services.s3.model.GetBucketCorsResponse; import software.amazon.awssdk.services.s3.model.DeleteBucketCorsRequest; @@ -21,22 +24,22 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class S3Cors { public static void main(String[] args) { final String usage = """ - Usage: - \s + Usage: + \s - Where: - bucketName - The Amazon S3 bucket to upload an object into. - accountId - The id of the account that owns the Amazon S3 bucket. - """; + Where: + bucketName - The Amazon S3 bucket to upload an object into. + accountId - The id of the account that owns the Amazon S3 bucket. + """; if (args.length != 2) { System.out.println(usage); @@ -47,8 +50,8 @@ public static void main(String[] args) { String accountId = args[1]; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); setCorsInformation(s3, bucketName, accountId); getBucketCorsInformation(s3, bucketName, accountId); @@ -59,9 +62,9 @@ public static void main(String[] args) { public static void deleteBucketCorsInformation(S3Client s3, String bucketName, String accountId) { try { DeleteBucketCorsRequest bucketCorsRequest = DeleteBucketCorsRequest.builder() - .bucket(bucketName) - .expectedBucketOwner(accountId) - .build(); + .bucket(bucketName) + .expectedBucketOwner(accountId) + .build(); s3.deleteBucketCors(bucketCorsRequest); @@ -74,9 +77,9 @@ public static void deleteBucketCorsInformation(S3Client s3, String bucketName, S public static void getBucketCorsInformation(S3Client s3, String bucketName, String accountId) { try { GetBucketCorsRequest bucketCorsRequest = GetBucketCorsRequest.builder() - .bucket(bucketName) - .expectedBucketOwner(accountId) - .build(); + .bucket(bucketName) + .expectedBucketOwner(accountId) + .build(); GetBucketCorsResponse corsResponse = s3.getBucketCors(bucketCorsRequest); List corsRules = corsResponse.corsRules(); @@ -103,21 +106,21 @@ public static void setCorsInformation(S3Client s3, String bucketName, String acc try { // Define CORS rules. CORSRule corsRule = CORSRule.builder() - .allowedMethods(allowMethods) - .allowedOrigins(allowOrigins) - .build(); + .allowedMethods(allowMethods) + .allowedOrigins(allowOrigins) + .build(); List corsRules = new ArrayList<>(); corsRules.add(corsRule); CORSConfiguration configuration = CORSConfiguration.builder() - .corsRules(corsRules) - .build(); + .corsRules(corsRules) + .build(); PutBucketCorsRequest putBucketCorsRequest = PutBucketCorsRequest.builder() - .bucket(bucketName) - .corsConfiguration(configuration) - .expectedBucketOwner(accountId) - .build(); + .bucket(bucketName) + .corsConfiguration(configuration) + .expectedBucketOwner(accountId) + .build(); s3.putBucketCors(putBucketCorsRequest); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/S3ObjectOperations.java b/javav2/example_code/s3/src/main/java/com/example/s3/S3ObjectOperations.java index d877463a5b6..50208950d99 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/S3ObjectOperations.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/S3ObjectOperations.java @@ -5,9 +5,11 @@ // snippet-start:[s3.java2.s3_object_operations.complete] // snippet-start:[s3.java2.s3_object_operations.import] + import java.io.IOException; import java.nio.ByteBuffer; import java.util.Random; + import software.amazon.awssdk.core.waiters.WaiterResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; @@ -39,230 +41,230 @@ /** * To run this AWS code example, ensure that you have setup your development * environment, including your AWS credentials. - * + *

* For information, see this documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class S3ObjectOperations { - private static S3Client s3; - - public static void main(String[] args) throws IOException { - final String USAGE = """ - - Usage: - - - Where: - bucketName - the Amazon S3 bucket to create. - key - the key to use. - """; - - if (args.length != 2) { - System.out.println(USAGE); - System.exit(1); - } - - String bucketName = args[0]; - String key = args[1]; - - // snippet-start:[s3.java2.s3_object_operations.upload] - Region region = Region.US_WEST_2; - s3 = S3Client.builder() - .region(region) - .build(); - - createBucket(s3, bucketName, region); - - PutObjectRequest objectRequest = PutObjectRequest.builder() - .bucket(bucketName) - .key(key) - .build(); - - s3.putObject(objectRequest, RequestBody.fromByteBuffer(getRandomByteBuffer(10_000))); - // snippet-end:[s3.java2.s3_object_operations.upload] - - // Multipart upload example - String multipartKey = "multiPartKey"; - multipartUpload(bucketName, multipartKey); - - // snippet-start:[s3.java2.s3_object_operations.pagination] - ListObjectsV2Request listObjectsReqManual = ListObjectsV2Request.builder() - .bucket(bucketName) - .maxKeys(1) - .build(); - - boolean done = false; - while (!done) { - ListObjectsV2Response listObjResponse = s3.listObjectsV2(listObjectsReqManual); - for (S3Object content : listObjResponse.contents()) { - System.out.println(content.key()); - } - - if (listObjResponse.nextContinuationToken() == null) { - done = true; - } - - listObjectsReqManual = listObjectsReqManual.toBuilder() - .continuationToken(listObjResponse.nextContinuationToken()) - .build(); - } - // snippet-end:[s3.java2.s3_object_operations.pagination] - // snippet-start:[s3.java2.s3_object_operations.iterative] - ListObjectsV2Request listReq = ListObjectsV2Request.builder() - .bucket(bucketName) - .maxKeys(1) - .build(); - - ListObjectsV2Iterable listRes = s3.listObjectsV2Paginator(listReq); - // Process response pages - listRes.stream() - .flatMap(r -> r.contents().stream()) - .forEach(content -> System.out - .println(" Key: " + content.key() + " size = " + content.size())); - - // snippet-end:[s3.java2.s3_object_operations.iterative] - // snippet-start:[s3.java2.s3_object_operations.stream] - // Helper method to work with paginated collection of items directly. - listRes.contents().stream() - .forEach(content -> System.out - .println(" Key: " + content.key() + " size = " + content.size())); - - // snippet-end:[s3.java2.s3_object_operations.stream] - // snippet-start:[s3.java2.s3_object_operations.forloop] - for (S3Object content : listRes.contents()) { - System.out.println(" Key: " + content.key() + " size = " + content.size()); - } - // snippet-end:[s3.java2.s3_object_operations.forloop] - - // snippet-start:[s3.java2.s3_object_operations.download] - GetObjectRequest getObjectRequest = GetObjectRequest.builder() - .bucket(bucketName) - .key(key) - .build(); - - s3.getObject(getObjectRequest); - // snippet-end:[s3.java2.s3_object_operations.download] - - // snippet-start:[s3.java2.s3_object_operations.delete] - DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder() - .bucket(bucketName) - .key(key) - .build(); - - s3.deleteObject(deleteObjectRequest); - // snippet-end:[s3.java2.s3_object_operations.delete] - - // Delete an object - deleteObjectRequest = DeleteObjectRequest.builder() - .bucket(bucketName) - .key(multipartKey) - .build(); - - s3.deleteObject(deleteObjectRequest); - deleteBucket(s3, bucketName); - System.out.println("Done"); - } + private static S3Client s3; - // Create a bucket by using a S3Waiter object - public static void createBucket(S3Client s3Client, String bucketName, Region region) { - - S3Waiter s3Waiter = s3Client.waiter(); - - try { - CreateBucketRequest bucketRequest = CreateBucketRequest.builder() - .bucket(bucketName) - .createBucketConfiguration( - CreateBucketConfiguration.builder() - .locationConstraint(region.id()) - .build()) - .build(); - - s3Client.createBucket(bucketRequest); - HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder() - .bucket(bucketName) - .build(); - - // Wait until the bucket is created and print out the response - WaiterResponse waiterResponse = s3Waiter - .waitUntilBucketExists(bucketRequestWait); - waiterResponse.matched().response().ifPresent(System.out::println); - System.out.println(bucketName + " is ready"); - - } catch (S3Exception e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } - } + public static void main(String[] args) throws IOException { + final String USAGE = """ - public static void deleteBucket(S3Client client, String bucket) { - DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder() - .bucket(bucket) - .build(); - client.deleteBucket(deleteBucketRequest); - } + Usage: + + + Where: + bucketName - the Amazon S3 bucket to create. + key - the key to use. + """; - /** - * Upload an object in parts - */ - private static void multipartUpload(String bucketName, String key) throws IOException { - - int mB = 1024 * 1024; - // snippet-start:[s3.java2.s3_object_operations.upload_multi_part] - // First create a multipart upload and get the upload id - CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest.builder() - .bucket(bucketName) - .key(key) - .build(); - - CreateMultipartUploadResponse response = s3.createMultipartUpload(createMultipartUploadRequest); - String uploadId = response.uploadId(); - System.out.println(uploadId); - - // Upload all the different parts of the object - UploadPartRequest uploadPartRequest1 = UploadPartRequest.builder() - .bucket(bucketName) - .key(key) - .uploadId(uploadId) - .partNumber(1).build(); - - String etag1 = s3 - .uploadPart(uploadPartRequest1, RequestBody.fromByteBuffer(getRandomByteBuffer(5 * mB))) - .eTag(); - - CompletedPart part1 = CompletedPart.builder().partNumber(1).eTag(etag1).build(); - - UploadPartRequest uploadPartRequest2 = UploadPartRequest.builder().bucket(bucketName).key(key) - .uploadId(uploadId) - .partNumber(2).build(); - String etag2 = s3 - .uploadPart(uploadPartRequest2, RequestBody.fromByteBuffer(getRandomByteBuffer(3 * mB))) - .eTag(); - CompletedPart part2 = CompletedPart.builder().partNumber(2).eTag(etag2).build(); - - // Finally call completeMultipartUpload operation to tell S3 to merge all - // uploaded - // parts and finish the multipart operation. - CompletedMultipartUpload completedMultipartUpload = CompletedMultipartUpload.builder() - .parts(part1, part2) - .build(); - - CompleteMultipartUploadRequest completeMultipartUploadRequest = CompleteMultipartUploadRequest.builder() - .bucket(bucketName) - .key(key) - .uploadId(uploadId) - .multipartUpload(completedMultipartUpload) - .build(); - - s3.completeMultipartUpload(completeMultipartUploadRequest); - // snippet-end:[s3.java2.s3_object_operations.upload_multi_part] + if (args.length != 2) { + System.out.println(USAGE); + System.exit(1); } - private static ByteBuffer getRandomByteBuffer(int size) throws IOException { - byte[] b = new byte[size]; - new Random().nextBytes(b); - return ByteBuffer.wrap(b); + String bucketName = args[0]; + String key = args[1]; + + // snippet-start:[s3.java2.s3_object_operations.upload] + Region region = Region.US_WEST_2; + s3 = S3Client.builder() + .region(region) + .build(); + + createBucket(s3, bucketName, region); + + PutObjectRequest objectRequest = PutObjectRequest.builder() + .bucket(bucketName) + .key(key) + .build(); + + s3.putObject(objectRequest, RequestBody.fromByteBuffer(getRandomByteBuffer(10_000))); + // snippet-end:[s3.java2.s3_object_operations.upload] + + // Multipart upload example + String multipartKey = "multiPartKey"; + multipartUpload(bucketName, multipartKey); + + // snippet-start:[s3.java2.s3_object_operations.pagination] + ListObjectsV2Request listObjectsReqManual = ListObjectsV2Request.builder() + .bucket(bucketName) + .maxKeys(1) + .build(); + + boolean done = false; + while (!done) { + ListObjectsV2Response listObjResponse = s3.listObjectsV2(listObjectsReqManual); + for (S3Object content : listObjResponse.contents()) { + System.out.println(content.key()); + } + + if (listObjResponse.nextContinuationToken() == null) { + done = true; + } + + listObjectsReqManual = listObjectsReqManual.toBuilder() + .continuationToken(listObjResponse.nextContinuationToken()) + .build(); + } + // snippet-end:[s3.java2.s3_object_operations.pagination] + // snippet-start:[s3.java2.s3_object_operations.iterative] + ListObjectsV2Request listReq = ListObjectsV2Request.builder() + .bucket(bucketName) + .maxKeys(1) + .build(); + + ListObjectsV2Iterable listRes = s3.listObjectsV2Paginator(listReq); + // Process response pages + listRes.stream() + .flatMap(r -> r.contents().stream()) + .forEach(content -> System.out + .println(" Key: " + content.key() + " size = " + content.size())); + + // snippet-end:[s3.java2.s3_object_operations.iterative] + // snippet-start:[s3.java2.s3_object_operations.stream] + // Helper method to work with paginated collection of items directly. + listRes.contents().stream() + .forEach(content -> System.out + .println(" Key: " + content.key() + " size = " + content.size())); + + // snippet-end:[s3.java2.s3_object_operations.stream] + // snippet-start:[s3.java2.s3_object_operations.forloop] + for (S3Object content : listRes.contents()) { + System.out.println(" Key: " + content.key() + " size = " + content.size()); + } + // snippet-end:[s3.java2.s3_object_operations.forloop] + + // snippet-start:[s3.java2.s3_object_operations.download] + GetObjectRequest getObjectRequest = GetObjectRequest.builder() + .bucket(bucketName) + .key(key) + .build(); + + s3.getObject(getObjectRequest); + // snippet-end:[s3.java2.s3_object_operations.download] + + // snippet-start:[s3.java2.s3_object_operations.delete] + DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder() + .bucket(bucketName) + .key(key) + .build(); + + s3.deleteObject(deleteObjectRequest); + // snippet-end:[s3.java2.s3_object_operations.delete] + + // Delete an object + deleteObjectRequest = DeleteObjectRequest.builder() + .bucket(bucketName) + .key(multipartKey) + .build(); + + s3.deleteObject(deleteObjectRequest); + deleteBucket(s3, bucketName); + System.out.println("Done"); + } + + // Create a bucket by using a S3Waiter object + public static void createBucket(S3Client s3Client, String bucketName, Region region) { + + S3Waiter s3Waiter = s3Client.waiter(); + + try { + CreateBucketRequest bucketRequest = CreateBucketRequest.builder() + .bucket(bucketName) + .createBucketConfiguration( + CreateBucketConfiguration.builder() + .locationConstraint(region.id()) + .build()) + .build(); + + s3Client.createBucket(bucketRequest); + HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder() + .bucket(bucketName) + .build(); + + // Wait until the bucket is created and print out the response + WaiterResponse waiterResponse = s3Waiter + .waitUntilBucketExists(bucketRequestWait); + waiterResponse.matched().response().ifPresent(System.out::println); + System.out.println(bucketName + " is ready"); + + } catch (S3Exception e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); } + } + + public static void deleteBucket(S3Client client, String bucket) { + DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder() + .bucket(bucket) + .build(); + client.deleteBucket(deleteBucketRequest); + } + + /** + * Upload an object in parts + */ + private static void multipartUpload(String bucketName, String key) throws IOException { + + int mB = 1024 * 1024; + // snippet-start:[s3.java2.s3_object_operations.upload_multi_part] + // First create a multipart upload and get the upload id + CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest.builder() + .bucket(bucketName) + .key(key) + .build(); + + CreateMultipartUploadResponse response = s3.createMultipartUpload(createMultipartUploadRequest); + String uploadId = response.uploadId(); + System.out.println(uploadId); + + // Upload all the different parts of the object + UploadPartRequest uploadPartRequest1 = UploadPartRequest.builder() + .bucket(bucketName) + .key(key) + .uploadId(uploadId) + .partNumber(1).build(); + + String etag1 = s3 + .uploadPart(uploadPartRequest1, RequestBody.fromByteBuffer(getRandomByteBuffer(5 * mB))) + .eTag(); + + CompletedPart part1 = CompletedPart.builder().partNumber(1).eTag(etag1).build(); + + UploadPartRequest uploadPartRequest2 = UploadPartRequest.builder().bucket(bucketName).key(key) + .uploadId(uploadId) + .partNumber(2).build(); + String etag2 = s3 + .uploadPart(uploadPartRequest2, RequestBody.fromByteBuffer(getRandomByteBuffer(3 * mB))) + .eTag(); + CompletedPart part2 = CompletedPart.builder().partNumber(2).eTag(etag2).build(); + + // Finally call completeMultipartUpload operation to tell S3 to merge all + // uploaded + // parts and finish the multipart operation. + CompletedMultipartUpload completedMultipartUpload = CompletedMultipartUpload.builder() + .parts(part1, part2) + .build(); + + CompleteMultipartUploadRequest completeMultipartUploadRequest = CompleteMultipartUploadRequest.builder() + .bucket(bucketName) + .key(key) + .uploadId(uploadId) + .multipartUpload(completedMultipartUpload) + .build(); + + s3.completeMultipartUpload(completeMultipartUploadRequest); + // snippet-end:[s3.java2.s3_object_operations.upload_multi_part] + } + + private static ByteBuffer getRandomByteBuffer(int size) throws IOException { + byte[] b = new byte[size]; + new Random().nextBytes(b); + return ByteBuffer.wrap(b); + } } // snippet-end:[s3.java2.s3_object_operations.main] diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/S3ZipExample.java b/javav2/example_code/s3/src/main/java/com/example/s3/S3ZipExample.java index aa0ed071392..bcde54ef765 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/S3ZipExample.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/S3ZipExample.java @@ -14,6 +14,7 @@ import software.amazon.awssdk.services.s3.presigner.S3Presigner; import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest; import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest; + import java.io.ByteArrayOutputStream; import java.io.IOException; import java.time.Duration; @@ -23,21 +24,21 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class S3ZipExample { public static void main(String[] args) { final String usage = """ - Usage: - \s + Usage: + \s - Where: - bucketName - The Amazon S3 bucket where JPG images are located.\s - keys - A comma separated list of images (without spaces) located in the S3 bucket and to be placed into a ZIP file. For example, For example pic1.jpg,pic2.jpg"""; + Where: + bucketName - The Amazon S3 bucket where JPG images are located.\s + keys - A comma separated list of images (without spaces) located in the S3 bucket and to be placed into a ZIP file. For example, For example pic1.jpg,pic2.jpg"""; if (args.length != 2) { System.out.println(usage); @@ -50,8 +51,8 @@ public static void main(String[] args) { String[] imageKeys = keys.split("[,]", 0); Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); createZIPFile(s3, bucketName, imageKeys); } @@ -61,8 +62,6 @@ public static void createZIPFile(S3Client s3, String bucketName, String[] imageK String zipName = uuid + ".zip"; // Create a ByteArrayOutputStream to write the ZIP file to. ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - - // Create a ZipOutputStream to write the files to the ZIP file. ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); try { @@ -70,9 +69,9 @@ public static void createZIPFile(S3Client s3, String bucketName, String[] imageK for (String imageKey : imageKeys) { // Get the object data from S3. GetObjectRequest getObjectRequest = GetObjectRequest.builder() - .bucket(bucketName) - .key(imageKey) - .build(); + .bucket(bucketName) + .key(imageKey) + .build(); ResponseBytes responseBytes = s3.getObjectAsBytes(getObjectRequest); // Create a ZipEntry for the object and add it to the ZipOutputStream. @@ -91,9 +90,9 @@ public static void createZIPFile(S3Client s3, String bucketName, String[] imageK // Upload the ZIP file to S3. PutObjectRequest putObjectRequest = PutObjectRequest.builder() - .bucket(bucketName) - .key(zipName) - .build(); + .bucket(bucketName) + .key(zipName) + .build(); s3.putObject(putObjectRequest, RequestBody.fromBytes(outputStream.toByteArray())); String preSignUrl = signObjectToDownload(bucketName, zipName); System.out.println("The Presigned URL is " + preSignUrl); @@ -112,19 +111,19 @@ public static void createZIPFile(S3Client s3, String bucketName, String[] imageK public static String signObjectToDownload(String bucketName, String keyName) { S3Presigner presignerOb = S3Presigner.builder() - .region(Region.US_EAST_1) - .build(); + .region(Region.US_EAST_1) + .build(); try { GetObjectRequest getObjectRequest = GetObjectRequest.builder() - .bucket(bucketName) - .key(keyName) - .build(); + .bucket(bucketName) + .key(keyName) + .build(); GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder() - .signatureDuration(Duration.ofMinutes(1440)) - .getObjectRequest(getObjectRequest) - .build(); + .signatureDuration(Duration.ofMinutes(1440)) + .getObjectRequest(getObjectRequest) + .build(); PresignedGetObjectRequest presignedGetObjectRequest = presignerOb.presignGetObject(getObjectPresignRequest); return presignedGetObjectRequest.url().toString(); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/SetAcl.java b/javav2/example_code/s3/src/main/java/com/example/s3/SetAcl.java index 3ae6aefd562..0cf999cf198 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/SetAcl.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/SetAcl.java @@ -5,6 +5,7 @@ // snippet-start:[s3.java2.set_acl.main] // snippet-start:[s3.java2.set_acl.import] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.AccessControlPolicy; @@ -21,26 +22,26 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class SetAcl { public static void main(String[] args) { final String usage = """ - Usage: - \s + Usage: + \s - Where: - bucketName - The Amazon S3 bucket to grant permissions on.\s - id - The ID of the owner of this bucket (you can get this value from the AWS Management Console). - """; + Where: + bucketName - The Amazon S3 bucket to grant permissions on.\s + id - The ID of the owner of this bucket (you can get this value from the AWS Management Console). + """; if (args.length != 2) { System.out.println(usage); - System.exit(1); + return; } String bucketName = args[0]; @@ -49,8 +50,8 @@ public static void main(String[] args) { System.out.println(" in bucket: " + bucketName); Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); setBucketAcl(s3, bucketName, id); System.out.println("Done!"); @@ -60,23 +61,23 @@ public static void main(String[] args) { public static void setBucketAcl(S3Client s3, String bucketName, String id) { try { Grant ownerGrant = Grant.builder() - .grantee(builder -> builder.id(id) - .type(Type.CANONICAL_USER)) - .permission(Permission.FULL_CONTROL) - .build(); + .grantee(builder -> builder.id(id) + .type(Type.CANONICAL_USER)) + .permission(Permission.FULL_CONTROL) + .build(); List grantList2 = new ArrayList<>(); grantList2.add(ownerGrant); AccessControlPolicy acl = AccessControlPolicy.builder() - .owner(builder -> builder.id(id)) - .grants(grantList2) - .build(); + .owner(builder -> builder.id(id)) + .grants(grantList2) + .build(); PutBucketAclRequest putAclReq = PutBucketAclRequest.builder() - .bucket(bucketName) - .accessControlPolicy(acl) - .build(); + .bucket(bucketName) + .accessControlPolicy(acl) + .build(); s3.putBucketAcl(putAclReq); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/SetBucketPolicy.java b/javav2/example_code/s3/src/main/java/com/example/s3/SetBucketPolicy.java index 06cecf0316b..0c747f724a4 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/SetBucketPolicy.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/SetBucketPolicy.java @@ -5,15 +5,18 @@ // snippet-start:[s3.java2.set_bucket_policy.main] // snippet-start:[s3.java2.set_bucket_policy.import] + import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.PutBucketPolicyRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.regions.Region; + import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; + import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.ObjectMapper; // snippet-end:[s3.java2.set_bucket_policy.import] @@ -21,22 +24,22 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class SetBucketPolicy { public static void main(String[] args) { final String usage = """ - Usage: - + Usage: + - Where: - bucketName - The Amazon S3 bucket to set the policy on. - polFile - A JSON file containing the policy (see the Amazon S3 Readme for an example).\s - """; + Where: + bucketName - The Amazon S3 bucket to set the policy on. + polFile - A JSON file containing the policy (see the Amazon S3 Readme for an example).\s + """; if (args.length != 2) { System.out.println(usage); @@ -48,8 +51,8 @@ public static void main(String[] args) { String policyText = getBucketPolicyFromFile(polFile); Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); setPolicy(s3, bucketName, policyText); s3.close(); @@ -64,9 +67,9 @@ public static void setPolicy(S3Client s3, String bucketName, String policyText) try { PutBucketPolicyRequest policyReq = PutBucketPolicyRequest.builder() - .bucket(bucketName) - .policy(policyText) - .build(); + .bucket(bucketName) + .policy(policyText) + .build(); s3.putBucketPolicy(policyReq); @@ -80,7 +83,6 @@ public static void setPolicy(S3Client s3, String bucketName, String policyText) // Loads a JSON-formatted policy from a file public static String getBucketPolicyFromFile(String policyFile) { - StringBuilder fileText = new StringBuilder(); try { List lines = Files.readAllLines(Paths.get(policyFile), StandardCharsets.UTF_8); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/SetWebsiteConfiguration.java b/javav2/example_code/s3/src/main/java/com/example/s3/SetWebsiteConfiguration.java index 5513530017f..82865094d0b 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/SetWebsiteConfiguration.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/SetWebsiteConfiguration.java @@ -5,6 +5,7 @@ // snippet-start:[s3.java2.set_website_configuration.main] // snippet-start:[s3.java2.set_website_configuration.import] + import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.IndexDocument; import software.amazon.awssdk.services.s3.model.PutBucketWebsiteRequest; @@ -16,9 +17,9 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ @@ -26,13 +27,13 @@ public class SetWebsiteConfiguration { public static void main(String[] args) { final String usage = """ - Usage: [indexdoc]\s + Usage: [indexdoc]\s - Where: - bucketName - The Amazon S3 bucket to set the website configuration on.\s - indexdoc - The index document, ex. 'index.html' - If not specified, 'index.html' will be set. - """; + Where: + bucketName - The Amazon S3 bucket to set the website configuration on.\s + indexdoc - The index document, ex. 'index.html' + If not specified, 'index.html' will be set. + """; if (args.length != 1) { System.out.println(usage); @@ -43,8 +44,8 @@ public static void main(String[] args) { String indexDoc = "index.html"; Region region = Region.US_EAST_1; S3Client s3 = S3Client.builder() - .region(region) - .build(); + .region(region) + .build(); setWebsiteConfig(s3, bucketName, indexDoc); s3.close(); @@ -53,13 +54,13 @@ public static void main(String[] args) { public static void setWebsiteConfig(S3Client s3, String bucketName, String indexDoc) { try { WebsiteConfiguration websiteConfig = WebsiteConfiguration.builder() - .indexDocument(IndexDocument.builder().suffix(indexDoc).build()) - .build(); + .indexDocument(IndexDocument.builder().suffix(indexDoc).build()) + .build(); PutBucketWebsiteRequest pubWebsiteReq = PutBucketWebsiteRequest.builder() - .bucket(bucketName) - .websiteConfiguration(websiteConfig) - .build(); + .bucket(bucketName) + .websiteConfiguration(websiteConfig) + .build(); s3.putBucketWebsite(pubWebsiteReq); System.out.println("The call was successful"); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/VPCCreateJob.java b/javav2/example_code/s3/src/main/java/com/example/s3/VPCCreateJob.java index 2db27dd47d6..c36af46a7fc 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/VPCCreateJob.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/VPCCreateJob.java @@ -5,6 +5,7 @@ // snippet-start:[s3.java2.create_job.vpc.main] // snippet-start:[s3.java2.create_job.vpc.import] + import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3control.S3ControlClient; import software.amazon.awssdk.services.s3control.model.S3SetObjectTaggingOperation; @@ -18,6 +19,7 @@ import software.amazon.awssdk.services.s3control.model.JobReportFormat; import software.amazon.awssdk.services.s3control.model.CreateJobRequest; import software.amazon.awssdk.services.s3control.model.S3ControlException; + import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; @@ -26,117 +28,116 @@ /** * Before running this Java V2 code example, set up your development * environment, including your credentials. - * + *

* For more information, see the following documentation topic: - * + *

* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class VPCCreateJob { - public static void main(String[] args) throws URISyntaxException { - final String usage = """ - - Usage: - - - Where: - accountId - The account id value that owns the Amazon S3 bucket. - - iamRoleArn - The ARN of the AWS Identity and Access Management (IAM) role that has permissions to create a batch job. - manifestLocation - The location where the manaifest file required for the job (for example, arn:aws:s3:::/manifest.csv). - reportBucketName - The Amazon S3 bucket where the report is written to (for example, arn:aws:s3:::). - tagKey - The key used for a tag (for example, keyOne). - tagValue - The value for the key (for example, ValueOne). - eTag - The ETag for the specified manifest object (for example, 000000c9d1046e73f7dde5043ac3ae85). - vpcBucketURL - The URL of the bucket located in your virtual private cloud (VPC) (for example, https://bucket.vpce-xxxxxc4d-5e6f.s3.us-east-1.vpce.amazonaws.com) - """; - - if (args.length != 8) { - System.out.println(usage); - System.exit(1); - } - - String accountId = args[0]; - String iamRoleArn = args[1]; - String manifestLocation = args[2]; - String reportBucketName = args[3]; - String tagKey = args[4]; - String tagValue = args[5]; - String eTag = args[6]; - String vpcBucketURL = args[7]; - String uuid = java.util.UUID.randomUUID().toString(); - URI myURI = new URI(vpcBucketURL); - S3ControlClient s3ControlClient = S3ControlClient.builder() - .region(Region.US_EAST_1) - .endpointOverride(myURI) - .build(); - - createS3Job(s3ControlClient, accountId, iamRoleArn, manifestLocation, reportBucketName, tagKey, - tagValue, eTag, - uuid); - s3ControlClient.close(); + public static void main(String[] args) throws URISyntaxException { + final String usage = """ + + Usage: + + + Where: + accountId - The account id value that owns the Amazon S3 bucket. + iamRoleArn - The ARN of the AWS Identity and Access Management (IAM) role that has permissions to create a batch job. + manifestLocation - The location where the manaifest file required for the job (for example, arn:aws:s3:::/manifest.csv). + reportBucketName - The Amazon S3 bucket where the report is written to (for example, arn:aws:s3:::). + tagKey - The key used for a tag (for example, keyOne). + tagValue - The value for the key (for example, ValueOne). + eTag - The ETag for the specified manifest object (for example, 000000c9d1046e73f7dde5043ac3ae85). + vpcBucketURL - The URL of the bucket located in your virtual private cloud (VPC) (for example, https://bucket.vpce-xxxxxc4d-5e6f.s3.us-east-1.vpce.amazonaws.com) + """; + + if (args.length != 8) { + System.out.println(usage); + System.exit(1); } - public static void createS3Job(S3ControlClient s3ControlClient, String accountId, String iamRoleArn, - String manifestLocation, String reportBucketName, String tagKey, String tagValue, String eTag, - String uuid) { - try { - ArrayList tagSet = new ArrayList<>(); - S3Tag s3Tag = S3Tag.builder() - .key(tagKey) - .value(tagValue) - .build(); - - tagSet.add(s3Tag); - S3SetObjectTaggingOperation objectTaggingOperation = S3SetObjectTaggingOperation.builder() - .tagSet(tagSet) - .build(); - - JobOperation jobOperation = JobOperation.builder() - .s3PutObjectTagging(objectTaggingOperation) - .build(); - - JobManifestLocation jobManifestLocation = JobManifestLocation.builder() - .objectArn(manifestLocation) - .eTag(eTag) - .build(); - - JobManifestSpec manifestSpec = JobManifestSpec.builder() - .fieldsWithStrings(new String[] { "Bucket", "Key" }) - .format(JobManifestFormat.S3_BATCH_OPERATIONS_CSV_20180820) - .build(); - - JobManifest jobManifest = JobManifest.builder() - .spec(manifestSpec) - .location(jobManifestLocation) - .build(); - - JobReport jobReport = JobReport.builder() - .bucket(reportBucketName) - .prefix("reports") - .format(JobReportFormat.REPORT_CSV_20180820) - .enabled(true) - .reportScope("AllTasks") - .build(); - - CreateJobRequest jobRequest = CreateJobRequest.builder() - .accountId(accountId) - .description("Job created using the AWS Java SDK") - .manifest(jobManifest) - .operation(jobOperation) - .report(jobReport) - .priority(42) - .roleArn(iamRoleArn) - .clientRequestToken(uuid) - .confirmationRequired(false) - .build(); - - s3ControlClient.createJob(jobRequest); - - } catch (S3ControlException e) { - System.err.println(e.awsErrorDetails().errorMessage()); - System.exit(1); - } + String accountId = args[0]; + String iamRoleArn = args[1]; + String manifestLocation = args[2]; + String reportBucketName = args[3]; + String tagKey = args[4]; + String tagValue = args[5]; + String eTag = args[6]; + String vpcBucketURL = args[7]; + String uuid = java.util.UUID.randomUUID().toString(); + URI myURI = new URI(vpcBucketURL); + S3ControlClient s3ControlClient = S3ControlClient.builder() + .region(Region.US_EAST_1) + .endpointOverride(myURI) + .build(); + + createS3Job(s3ControlClient, accountId, iamRoleArn, manifestLocation, reportBucketName, tagKey, + tagValue, eTag, + uuid); + s3ControlClient.close(); + } + + public static void createS3Job(S3ControlClient s3ControlClient, String accountId, String iamRoleArn, + String manifestLocation, String reportBucketName, String tagKey, String tagValue, String eTag, + String uuid) { + try { + ArrayList tagSet = new ArrayList<>(); + S3Tag s3Tag = S3Tag.builder() + .key(tagKey) + .value(tagValue) + .build(); + + tagSet.add(s3Tag); + S3SetObjectTaggingOperation objectTaggingOperation = S3SetObjectTaggingOperation.builder() + .tagSet(tagSet) + .build(); + + JobOperation jobOperation = JobOperation.builder() + .s3PutObjectTagging(objectTaggingOperation) + .build(); + + JobManifestLocation jobManifestLocation = JobManifestLocation.builder() + .objectArn(manifestLocation) + .eTag(eTag) + .build(); + + JobManifestSpec manifestSpec = JobManifestSpec.builder() + .fieldsWithStrings(new String[]{"Bucket", "Key"}) + .format(JobManifestFormat.S3_BATCH_OPERATIONS_CSV_20180820) + .build(); + + JobManifest jobManifest = JobManifest.builder() + .spec(manifestSpec) + .location(jobManifestLocation) + .build(); + + JobReport jobReport = JobReport.builder() + .bucket(reportBucketName) + .prefix("reports") + .format(JobReportFormat.REPORT_CSV_20180820) + .enabled(true) + .reportScope("AllTasks") + .build(); + + CreateJobRequest jobRequest = CreateJobRequest.builder() + .accountId(accountId) + .description("Job created using the AWS Java SDK") + .manifest(jobManifest) + .operation(jobOperation) + .report(jobReport) + .priority(42) + .roleArn(iamRoleArn) + .clientRequestToken(uuid) + .confirmationRequired(false) + .build(); + + s3ControlClient.createJob(jobRequest); + + } catch (S3ControlException e) { + System.err.println(e.awsErrorDetails().errorMessage()); + System.exit(1); } + } } // snippet-end:[s3.java2.create_job.vpc.main] diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/lockscenario/S3ObjectLockWorkflow.java b/javav2/example_code/s3/src/main/java/com/example/s3/lockscenario/S3ObjectLockWorkflow.java index de2301e184a..045e475117d 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/lockscenario/S3ObjectLockWorkflow.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/lockscenario/S3ObjectLockWorkflow.java @@ -8,8 +8,6 @@ import software.amazon.awssdk.services.s3.model.ObjectLockRetention; import java.io.BufferedWriter; import java.io.IOException; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; @@ -38,15 +36,20 @@ public class S3ObjectLockWorkflow { private static final List fileNames = new ArrayList<>(); public static void main(String[] args) { - // Get the current date and time to ensure bucket name is unique. - LocalDateTime currentTime = LocalDateTime.now(); + final String usage = """ + Usage: + \s - // Format the date and time as a string. - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); - String timeStamp = currentTime.format(formatter); + Where: + bucketName - The Amazon S3 bucket name. + """; + if (args.length != 1) { + System.out.println(usage); + System.exit(1); + } s3LockActions = new S3LockActions(); - bucketName = "bucket"+timeStamp; + bucketName = args[0]; Scanner scanner = new Scanner(System.in); System.out.println(DASHES); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadFile.java b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadFile.java index 586cd3af93d..74dbe765d9c 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadFile.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadFile.java @@ -35,7 +35,7 @@ public class DownloadFile { private static final Logger logger = LoggerFactory.getLogger(UploadFile.class); - public final String bucketName = "x-" + UUID.randomUUID(); + public final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); public final String key = UUID.randomUUID().toString(); private final String downloadedFileName = "downloaded.pdf"; public String downloadedFileWithPath; diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadToDirectory.java b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadToDirectory.java index 27202ecacee..4d852e99fe8 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadToDirectory.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadToDirectory.java @@ -35,7 +35,7 @@ public class DownloadToDirectory { private static final Logger logger = LoggerFactory.getLogger(DownloadToDirectory.class); - public final String bucketName = "x-" + UUID.randomUUID(); + public final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); public URI destinationPathURI; private final Set downloadedFileNameSet = new HashSet<>(); private final String destinationDirName = "downloadDirectory"; diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/ObjectCopy.java b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/ObjectCopy.java index a7aa0d7c711..411b90ee117 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/ObjectCopy.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/ObjectCopy.java @@ -27,9 +27,9 @@ public class ObjectCopy { private static final Logger logger = LoggerFactory.getLogger(ObjectCopy.class); - public final String bucketName = "x-" + UUID.randomUUID(); + public final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); public final String key = UUID.randomUUID().toString(); - public final String destinationBucket = "y-" + UUID.randomUUID(); + public final String destinationBucket = "amzn-s3-demo-bucket-" + UUID.randomUUID(); public final String destinationKey = UUID.randomUUID().toString(); public ObjectCopy() { diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadADirectory.java b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadADirectory.java index 3a0c4cbf219..7830d785806 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadADirectory.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadADirectory.java @@ -30,7 +30,7 @@ public class UploadADirectory { private static final Logger logger = LoggerFactory.getLogger(UploadADirectory.class); - public final String bucketName = "x-" + UUID.randomUUID(); + public final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); public URI sourceDirectory; public UploadADirectory() { diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadFile.java b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadFile.java index f446f680325..2145bf85429 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadFile.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadFile.java @@ -30,7 +30,7 @@ public class UploadFile { private static final Logger logger = LoggerFactory.getLogger(UploadFile.class); - public final String bucketName = "x-" + UUID.randomUUID(); + public final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); public final String key = UUID.randomUUID().toString(); public URI filePathURI; diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadStream.java b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadStream.java index ae1377a6fbe..20a11700fea 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadStream.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadStream.java @@ -22,7 +22,7 @@ public class UploadStream { private static final Logger logger = LoggerFactory.getLogger(UploadStream.class); public static void main(String[] args) { - String bucketName = "x-" + UUID.randomUUID(); + String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); String key = UUID.randomUUID().toString(); AsyncExampleUtils.createBucket(bucketName); From 01a37a0fca9d3191e7c993abc34ccce11424c5ba Mon Sep 17 00:00:00 2001 From: scmacdon Date: Tue, 24 Sep 2024 10:04:35 -0400 Subject: [PATCH 03/10] updated S3 examples --- .../s3/src/main/java/com/example/s3/S3ZipExample.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/S3ZipExample.java b/javav2/example_code/s3/src/main/java/com/example/s3/S3ZipExample.java index bcde54ef765..e899baa936f 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/S3ZipExample.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/S3ZipExample.java @@ -65,9 +65,7 @@ public static void createZIPFile(S3Client s3, String bucketName, String[] imageK ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); try { - // Loop through each object key. for (String imageKey : imageKeys) { - // Get the object data from S3. GetObjectRequest getObjectRequest = GetObjectRequest.builder() .bucket(bucketName) .key(imageKey) From a0e0af4864c72b8250aca6b1338fcc3b8867a534 Mon Sep 17 00:00:00 2001 From: scmacdon Date: Thu, 3 Oct 2024 12:48:23 -0400 Subject: [PATCH 04/10] added new s3 code for v2 update project --- javav2/example_code/s3/pom.xml | 4 + .../example/s3/batch/CreateRetentionJob.java | 345 ++++++++++++++++++ .../com/example/s3/batch/HelloS3Batch.java | 9 + .../com/example/s3/batch/S3BatchActions.java | 4 + .../com/example/s3/scenario/S3Actions.java | 42 +++ .../com/example/s3/scenario/S3Scenario.java | 25 +- .../src/main/resources/batch/job-manifest.csv | 8 +- 7 files changed, 431 insertions(+), 6 deletions(-) create mode 100644 javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateRetentionJob.java diff --git a/javav2/example_code/s3/pom.xml b/javav2/example_code/s3/pom.xml index 6a5badbe402..03744279709 100644 --- a/javav2/example_code/s3/pom.xml +++ b/javav2/example_code/s3/pom.xml @@ -182,6 +182,10 @@ software.amazon.awssdk iam-policy-builder + + software.amazon.awssdk + iam + org.apache.logging.log4j log4j-core diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateRetentionJob.java b/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateRetentionJob.java new file mode 100644 index 00000000000..1765a038d5b --- /dev/null +++ b/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateRetentionJob.java @@ -0,0 +1,345 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package com.example.s3.batch; + +import software.amazon.awssdk.core.sync.RequestBody; +import software.amazon.awssdk.services.s3.S3Client; +import software.amazon.awssdk.services.s3.model.PutObjectRequest; +import software.amazon.awssdk.services.s3control.S3ControlClient; +import software.amazon.awssdk.services.s3control.model.CreateJobRequest; +import software.amazon.awssdk.services.s3control.model.CreateJobResponse; +import software.amazon.awssdk.services.s3control.model.JobManifest; +import software.amazon.awssdk.services.s3control.model.JobManifestLocation; +import software.amazon.awssdk.services.s3control.model.JobManifestSpec; +import software.amazon.awssdk.services.s3control.model.JobOperation; +import software.amazon.awssdk.services.s3control.model.JobReport; +import software.amazon.awssdk.services.s3control.model.JobReportFormat; +import software.amazon.awssdk.services.s3control.model.JobReportScope; +import software.amazon.awssdk.services.s3control.model.S3ObjectLockLegalHold; +import software.amazon.awssdk.services.s3control.model.S3ObjectLockLegalHoldStatus; +import software.amazon.awssdk.services.s3control.model.S3ObjectLockRetentionMode; +import software.amazon.awssdk.services.s3control.model.S3Retention; +import software.amazon.awssdk.services.s3control.model.S3SetObjectLegalHoldOperation; +import software.amazon.awssdk.services.s3control.model.S3SetObjectRetentionOperation; +import software.amazon.awssdk.services.sts.StsClient; +import software.amazon.awssdk.services.sts.model.GetCallerIdentityResponse; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.text.ParseException; +import java.time.Instant; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +/** + * Before running this example: + *

+ * The SDK must be able to authenticate AWS requests on your behalf. If you have not configured + * authentication for SDKs and tools,see https://docs.aws.amazon.com/sdkref/latest/guide/access.html in the AWS SDKs and Tools Reference Guide. + *

+ * You must have a runtime environment configured with the Java SDK. + * See https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html in the Developer Guide if this is not set up. + */ +public class CreateRetentionJob { + private static final String STACK_NAME = "MyS3Stack"; + public static void main(String [] args) throws IOException, ParseException { + S3Client s3 = S3Client.create(); + S3ControlClient s3ControlClient = S3ControlClient.create(); + + // Use CloudFormation to stand up the resource required for this scenario. + System.out.println("Use CloudFormation to stand up the resource required for this scenario."); + CloudFormationHelper.deployCloudFormationStack(STACK_NAME); + + Map stackOutputs = CloudFormationHelper.getStackOutputs(STACK_NAME); + String iamRoleArn = stackOutputs.get("S3BatchRoleArn"); + String accountId = getAccountId(); + + // Specify your S3 bucket name. + String bucketName = "amazon-s3-demo-manifest-bucket" ; + System.out.println("Populate the bucket with the required files."); + String[] fileNames = {"job-manifest.csv", "object-key-1.txt", "object-key-2.txt", "object-key-3.txt", "object-key-4.txt"}; + uploadFilesToBucket(s3, bucketName, fileNames); + String jobId = createComplianceRetentionJob(s3ControlClient, iamRoleArn, bucketName, accountId); + System.out.println("The job Id is "+jobId); + + // Create a legal Hold Off Job. + String jobHoldOffId =createLegalHoldOffJob(s3ControlClient, iamRoleArn, bucketName, accountId); + System.out.println("The id of the hold off job is "+jobHoldOffId); + CloudFormationHelper.destroyCloudFormationStack(STACK_NAME); + } + + /** + * Uploads a set of files to an Amazon S3 bucket. + * + * @param s3 the S3 client to use for the file uploads + * @param bucketName the name of the S3 bucket to upload the files to + * @param fileNames an array of file names to be uploaded + * @throws IOException if an I/O error occurs during the file creation or upload process + */ + public static void uploadFilesToBucket(S3Client s3, String bucketName, String[] fileNames) throws IOException { + updateCSV(bucketName); + createTextFiles(fileNames); + for (String fileName : fileNames) { + populateBucket(s3, bucketName, fileName); + } + System.out.println("All files are placed in the S3 bucket " + bucketName); + } + + /** + * Uploads a file to an Amazon S3 bucket. + * + * @param s3 The {@link S3Client} instance used to interact with the Amazon S3 service. + * @param bucketName The name of the Amazon S3 bucket where the file will be uploaded. + * @param fileName The name of the file to be uploaded. + */ + public static void populateBucket(S3Client s3, String bucketName, String fileName) { + Path filePath = Paths.get("src/main/resources/batch/", fileName).toAbsolutePath(); + PutObjectRequest putOb = PutObjectRequest.builder() + .bucket(bucketName) + .key(fileName) + .build(); + + s3.putObject(putOb, RequestBody.fromFile(filePath)); + System.out.println("Successfully placed " + fileName + " into bucket " + bucketName); + } + + /** + * Updates the first value in each line of a CSV file located at the specified path. + * + * @param newValue the new value to be set for the first field in each line of the CSV file + * @throws IOException if an I/O error occurs while reading or writing the CSV file + * @throws NullPointerException if the {@code newValue} parameter is {@code null} + */ + public static void updateCSV(String newValue) { + Path csvFilePath = Paths.get("src/main/resources/batch/job-manifest.csv").toAbsolutePath(); + try { + // Read all lines from the CSV file. + List lines = Files.readAllLines(csvFilePath); + + // Update the first value in each line. + List updatedLines = lines.stream() + .map(line -> { + String[] parts = line.split(","); + parts[0] = newValue; + return String.join(",", parts); + }) + .collect(Collectors.toList()); + + // Write the updated lines back to the CSV file. + Files.write(csvFilePath, updatedLines); + System.out.println("CSV file updated successfully."); + } catch (Exception e) { + e.printStackTrace(); + } + } + + // snippet-start:[s3control.java2.create_job.compliance.main] + /** + * Creates a compliance retention job in Amazon S3 Control. + * + * A compliance retention job in Amazon S3 Control is a feature that allows you to + * set a retention period for objects stored in an S3 bucket. + * This feature is particularly useful for organizations that need to comply with + * regulatory requirements or internal policies that mandate the retention of data for + * a specific duration. + * + * @param s3ControlClient The S3ControlClient instance to use for the API call. + * @return The job ID of the created compliance retention job. + */ + public static String createComplianceRetentionJob(final S3ControlClient s3ControlClient, String roleArn, String bucketName, String accountId) { + final String manifestObjectArn = "arn:aws:s3:::amzn-s3-demo-manifest-bucket/compliance-objects-manifest.csv"; + final String manifestObjectVersionId = "your-object-version-Id"; + + Instant jan2025 = Instant.parse("2025-01-01T00:00:00Z"); + S3Retention retention = S3Retention.builder() + .mode(S3ObjectLockRetentionMode.COMPLIANCE) + .retainUntilDate(jan2025) + .build(); + + JobOperation jobOperation = JobOperation.builder() + .s3PutObjectRetention(S3SetObjectRetentionOperation.builder() + .retention(S3Retention.builder() + .mode(S3ObjectLockRetentionMode.COMPLIANCE) + .retainUntilDate(jan2025) + .build()) + .build()) + .build(); + + JobManifestLocation manifestLocation = JobManifestLocation.builder() + .objectArn(manifestObjectArn) + .eTag(manifestObjectVersionId) + .build(); + + JobManifestSpec manifestSpec = JobManifestSpec.builder() + .fieldsWithStrings("Bucket", "Key") + .format("S3BatchOperations_CSV_20180820") + .build(); + + JobManifest manifestToPublicApi = JobManifest.builder() + .location(manifestLocation) + .spec(manifestSpec) + .build(); + + // Report details. + final String jobReportBucketArn = "arn:aws:s3:::"+bucketName; + final String jobReportPrefix = "reports/compliance-objects-bops"; + + JobReport jobReport = JobReport.builder() + .enabled(true) + .reportScope(JobReportScope.ALL_TASKS) + .bucket(jobReportBucketArn) + .prefix(jobReportPrefix) + .format(JobReportFormat.REPORT_CSV_20180820) + .build(); + + final Boolean requiresConfirmation = true; + final int priority = 10; + CreateJobRequest request = CreateJobRequest.builder() + .accountId(accountId) + .description("Set compliance retain-until to 1 Jan 2025") + .manifest(manifestToPublicApi) + .operation(jobOperation) + .priority(priority) + .roleArn(roleArn) + .report(jobReport) + .confirmationRequired(requiresConfirmation) + .build(); + + // Create the job and get the result. + CreateJobResponse result = s3ControlClient.createJob(request); + return result.jobId(); + } + // snippet-end:[s3control.java2.create_job.compliance.main] + + + // snippet-start:[s3control.java2.create_job.legal.off.main] + /** + * Creates a legal hold off job in an S3 bucket. + * + * @param s3ControlClient the S3 Control client used to create the job + * @param roleArn the ARN of the IAM role to use for the job + * @param bucketName the name of the S3 bucket to create the job report in + * @param accountId the AWS account ID to create the job in + * @return the job ID of the created job + */ + public static String createLegalHoldOffJob(final S3ControlClient s3ControlClient, String roleArn, String bucketName, String accountId) { + final String manifestObjectArn = "arn:aws:s3:::amzn-s3-demo-manifest-bucket/compliance-objects-manifest.csv"; + final String manifestObjectVersionId = "your-object-version-Id"; + + Instant jan2025 = Instant.parse("2025-01-01T00:00:00Z"); + S3Retention retention = S3Retention.builder() + .mode(S3ObjectLockRetentionMode.COMPLIANCE) + .retainUntilDate(jan2025) + .build(); + + JobOperation jobOperation = JobOperation.builder() + .s3PutObjectLegalHold(S3SetObjectLegalHoldOperation.builder() + .legalHold(S3ObjectLockLegalHold.builder() + .status(S3ObjectLockLegalHoldStatus.OFF) + .build()) + .build()) + .build(); + + JobManifestLocation manifestLocation = JobManifestLocation.builder() + .objectArn(manifestObjectArn) + .eTag(manifestObjectVersionId) + .build(); + + JobManifestSpec manifestSpec = JobManifestSpec.builder() + .fieldsWithStrings("Bucket", "Key") + .format("S3BatchOperations_CSV_20180820") + .build(); + + JobManifest manifestToPublicApi = JobManifest.builder() + .location(manifestLocation) + .spec(manifestSpec) + .build(); + + // Report details. + final String jobReportBucketArn = "arn:aws:s3:::"+bucketName; + final String jobReportPrefix = "reports/compliance-objects-bops"; + + JobReport jobReport = JobReport.builder() + .enabled(true) + .reportScope(JobReportScope.ALL_TASKS) + .bucket(jobReportBucketArn) + .prefix(jobReportPrefix) + .format(JobReportFormat.REPORT_CSV_20180820) + .build(); + + final Boolean requiresConfirmation = true; + final int priority = 10; + CreateJobRequest request = CreateJobRequest.builder() + .accountId(accountId) + .description("Set compliance retain-until to 1 Jan 2025") + .manifest(manifestToPublicApi) + .operation(jobOperation) + .priority(priority) + .roleArn(roleArn) + .report(jobReport) + .confirmationRequired(requiresConfirmation) + .build(); + + // Create the job and get the result. + CreateJobResponse result = s3ControlClient.createJob(request); + return result.jobId(); + } + // snippet-end:[s3control.java2.create_job.legal.off.main] + + /** + * Creates text files with the given file names in the "batch" directory within the "src/main/resources" folder. + * + * @param fileNames an array of file names to be created, each with a ".txt" extension + */ + public static void createTextFiles(String[] fileNames) { + String currentDirectory = System.getProperty("user.dir"); + String directoryPath = currentDirectory + "\\src\\main\\resources\\batch"; + Path path = Paths.get(directoryPath); + + try { + if (Files.notExists(path)) { + Files.createDirectories(path); + System.out.println("Created directory: " + path.toString()); + } else { + System.out.println("Directory already exists: " + path.toString()); + } + + for (String fileName : fileNames) { + // Check if the file is a .txt file. + if (fileName.endsWith(".txt")) { + // Define the path for the new file. + Path filePath = path.resolve(fileName); + System.out.println("Attempting to create file: " + filePath.toString()); + + // Create and write content to the new file. + Files.write(filePath, "This is a test".getBytes()); + + // Verify the file was created. + if (Files.exists(filePath)) { + System.out.println("Successfully created file: " + filePath.toString()); + } else { + System.out.println("Failed to create file: " + filePath.toString()); + } + } + } + + } catch (IOException e) { + System.err.println("An error occurred: " + e.getMessage()); + e.printStackTrace(); + } + } + + /** + * Retrieves the account ID of the current AWS user or role. + * + * @return the account ID as a String + */ + public static String getAccountId() { + StsClient stsClient = StsClient.create(); + GetCallerIdentityResponse callerIdentityResponse = stsClient.getCallerIdentity(); + return callerIdentityResponse.account(); + } +} diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/batch/HelloS3Batch.java b/javav2/example_code/s3/src/main/java/com/example/s3/batch/HelloS3Batch.java index cefdc581b96..0143fe58ea0 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/batch/HelloS3Batch.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/batch/HelloS3Batch.java @@ -19,6 +19,15 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; +/** + * Before running this example: + *

+ * The SDK must be able to authenticate AWS requests on your behalf. If you have not configured + * authentication for SDKs and tools,see https://docs.aws.amazon.com/sdkref/latest/guide/access.html in the AWS SDKs and Tools Reference Guide. + *

+ * You must have a runtime environment configured with the Java SDK. + * See https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html in the Developer Guide if this is not set up. + */ public class HelloS3Batch { private static S3ControlAsyncClient asyncClient; public static void main(String []args ) { diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/batch/S3BatchActions.java b/javav2/example_code/s3/src/main/java/com/example/s3/batch/S3BatchActions.java index 70f0edb0f1e..2ff4d938d59 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/batch/S3BatchActions.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/batch/S3BatchActions.java @@ -43,6 +43,9 @@ import software.amazon.awssdk.services.s3control.model.JobStatus; import software.amazon.awssdk.services.s3control.model.PutJobTaggingRequest; import software.amazon.awssdk.services.s3control.model.S3ControlException; +import software.amazon.awssdk.services.s3control.model.S3ObjectLockRetentionMode; +import software.amazon.awssdk.services.s3control.model.S3Retention; +import software.amazon.awssdk.services.s3control.model.S3SetObjectRetentionOperation; import software.amazon.awssdk.services.s3control.model.S3SetObjectTaggingOperation; import software.amazon.awssdk.services.s3control.model.S3Tag; import software.amazon.awssdk.services.s3control.model.UpdateJobPriorityRequest; @@ -54,6 +57,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.time.Duration; +import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CompletableFuture; diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Actions.java b/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Actions.java index 8e0b42e9e9d..5064b761973 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Actions.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Actions.java @@ -33,6 +33,7 @@ import software.amazon.awssdk.services.s3.model.PutObjectRequest; import software.amazon.awssdk.services.s3.model.PutObjectResponse; import software.amazon.awssdk.core.async.AsyncResponseTransformer; +import software.amazon.awssdk.services.s3.model.UploadPartCopyRequest; import software.amazon.awssdk.services.s3.model.UploadPartRequest; import software.amazon.awssdk.services.s3.paginators.ListObjectsV2Publisher; import software.amazon.awssdk.services.s3.waiters.S3AsyncWaiter; @@ -364,6 +365,47 @@ public CompletableFuture deleteBucketAsync(String bucket) { } // snippet-end:[s3.java2.bucket_deletion.main] + // snippet-start:[s3.java2.multi_copy.main] + public CompletableFuture performMultiCopy(String toBucket, String bucketName, String key) { + // Step 1: Initiate multipart upload. + CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest.builder() + .bucket(toBucket) + .key(key) + .build(); + + getAsyncClient().createMultipartUpload(createMultipartUploadRequest) + .thenApply(createMultipartUploadResponse -> { + String uploadId = createMultipartUploadResponse.uploadId(); + System.out.println("Upload ID: " + uploadId); + + // Step 2: Set the parameters for the upload part copy request. + UploadPartCopyRequest uploadPartCopyRequest = UploadPartCopyRequest.builder() + .sourceBucket(bucketName) + .destinationBucket(toBucket) + .sourceKey(key) + .destinationKey(key) + .uploadId(uploadId) // Use the valid uploadId. + .partNumber(1) // Ensure the part number is correct. + .copySourceRange("bytes=0-1023") // Adjust range as needed + .build(); + + // Step 3: Perform the upload part copy operation asynchronously. + return getAsyncClient().uploadPartCopy(uploadPartCopyRequest); + }) + .thenCompose(uploadPartCopyFuture -> uploadPartCopyFuture) + .whenComplete((uploadPartCopyResponse, exception) -> { + if (exception != null) { + // Handle any exceptions. + logger.error("Error during upload part copy: " + exception.getMessage()); + } else { + // Successfully completed the upload part copy. + System.out.println("Upload Part Copy completed successfully. ETag: " + uploadPartCopyResponse.copyPartResult().eTag()); + } + }); + return null; + } + // snippet-end:[s3.java2.multi_copy.main] + private static ByteBuffer getRandomByteBuffer(int size) { ByteBuffer buffer = ByteBuffer.allocate(size); for (int i = 0; i < size; i++) { diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Scenario.java b/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Scenario.java index 6837bda696a..9f05ceefd14 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Scenario.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Scenario.java @@ -222,7 +222,28 @@ private static void runScenario(String bucketName, String key, String objectPath logger.info(DASHES); logger.info(DASHES); - logger.info("7. Delete objects from the Amazon S3 bucket."); + logger.info("7. Copy the object to another Amazon S3 bucket using multi copy."); + waitForInputToContinue(scanner); + + try { + CompletableFuture future = s3Actions.performMultiCopy(toBucket, bucketName, key); + String result = future.join(); + logger.info("Copy operation result: {}", result); + + } catch (RuntimeException rt) { + Throwable cause = rt.getCause(); + if (cause instanceof S3Exception s3Ex) { + logger.info("KMS error occurred: Error message: {}, Error code {}", s3Ex.getMessage(), s3Ex.awsErrorDetails().errorCode()); + } else { + logger.info("An unexpected error occurred: " + rt.getMessage()); + } + } + waitForInputToContinue(scanner); + logger.info(DASHES); + + + logger.info(DASHES); + logger.info("8. Delete objects from the Amazon S3 bucket."); waitForInputToContinue(scanner); try { CompletableFuture future = s3Actions.deleteObjectFromBucketAsync(bucketName, key); @@ -254,7 +275,7 @@ private static void runScenario(String bucketName, String key, String objectPath logger.info(DASHES); logger.info(DASHES); - logger.info("8. Delete the Amazon S3 bucket."); + logger.info("9. Delete the Amazon S3 bucket."); waitForInputToContinue(scanner); try { CompletableFuture future = s3Actions.deleteBucketAsync(bucketName); diff --git a/javav2/example_code/s3/src/main/resources/batch/job-manifest.csv b/javav2/example_code/s3/src/main/resources/batch/job-manifest.csv index a04dbf146e9..a1a421ff9c0 100644 --- a/javav2/example_code/s3/src/main/resources/batch/job-manifest.csv +++ b/javav2/example_code/s3/src/main/resources/batch/job-manifest.csv @@ -1,4 +1,4 @@ -amazon-s3-demo-bucket-50270fac-5f8f-4b0e-9209-d80b7cc0760d,object-key-1.txt -amazon-s3-demo-bucket-50270fac-5f8f-4b0e-9209-d80b7cc0760d,object-key-2.txt -amazon-s3-demo-bucket-50270fac-5f8f-4b0e-9209-d80b7cc0760d,object-key-3.txt -amazon-s3-demo-bucket-50270fac-5f8f-4b0e-9209-d80b7cc0760d,object-key-4.txt +amazon-s3-demo-manifest-bucket,object-key-1.txt +amazon-s3-demo-manifest-bucket,object-key-2.txt +amazon-s3-demo-manifest-bucket,object-key-3.txt +amazon-s3-demo-manifest-bucket,object-key-4.txt From a479b8edd242423e95cccd82f01e6564a6a29d74 Mon Sep 17 00:00:00 2001 From: scmacdon Date: Thu, 3 Oct 2024 13:30:51 -0400 Subject: [PATCH 05/10] added new s3 code for v2 update project --- .doc_gen/metadata/s3-control_metadata.yaml | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/.doc_gen/metadata/s3-control_metadata.yaml b/.doc_gen/metadata/s3-control_metadata.yaml index 120f1242d6d..4d078665fbd 100644 --- a/.doc_gen/metadata/s3-control_metadata.yaml +++ b/.doc_gen/metadata/s3-control_metadata.yaml @@ -24,9 +24,15 @@ s3-control_CreateJob: github: javav2/example_code/s3 sdkguide: excerpts: - - description: + - description: create an asynchronous S3 job snippet_tags: - s3control.java2.create_job.async.main + - description: create a compliance retention job + snippet_tags: + - s3control.java2.create_job.compliance.main + - description: creates a legal hold off job + snippet_tags: + - s3control.java2.create_job.compliance.main services: s3-control: {CreateJob} s3-control_PutJobTagging: @@ -108,10 +114,8 @@ s3-control_UpdateJobPriority: services: s3-control: {UpdateJobPriority} s3-control_Basics: - title: Learn core operations for'&S3Control;' using an &AWS; SDK - title_abbrev: Learn core operations synopsis: learn core operations for'&S3Control;'. - category: Scenarios + category: Basics languages: Java: versions: From ac24460e5383cb8530a56a04a18cb22c714bbe77 Mon Sep 17 00:00:00 2001 From: scmacdon Date: Thu, 3 Oct 2024 18:42:03 -0400 Subject: [PATCH 06/10] updated bucket names --- .../s3/async/PutObjectFromStreamAsync.java | 3 +- .../s3/async/SelectObjectContentExample.java | 2 +- .../example/s3/batch/CreateRetentionJob.java | 34 +++++++++++-------- .../com/example/s3/batch/S3BatchScenario.java | 2 +- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/async/PutObjectFromStreamAsync.java b/javav2/example_code/s3/src/main/java/com/example/s3/async/PutObjectFromStreamAsync.java index 3883ab305ce..7eadc1ae508 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/async/PutObjectFromStreamAsync.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/async/PutObjectFromStreamAsync.java @@ -21,9 +21,8 @@ public class PutObjectFromStreamAsync { private static final Logger logger = LoggerFactory.getLogger(PutObjectFromStreamAsync.class); - public static void main(String[] args) { - String bucketName = "x-" + UUID.randomUUID(); + String bucketName = "amzn-s3-demo-bucket-" + UUID.randomUUID(); String key = UUID.randomUUID().toString(); AsyncExampleUtils.createBucket(bucketName); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/async/SelectObjectContentExample.java b/javav2/example_code/s3/src/main/java/com/example/s3/async/SelectObjectContentExample.java index 1a8514249c4..41aa1f46f90 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/async/SelectObjectContentExample.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/async/SelectObjectContentExample.java @@ -46,7 +46,7 @@ // snippet-start:[s3.java2.async.selectObjectContentMethod.main] public class SelectObjectContentExample { static final Logger logger = LoggerFactory.getLogger(SelectObjectContentExample.class); - static final String BUCKET_NAME = "select-object-content-" + UUID.randomUUID(); + static final String BUCKET_NAME = "amzn-s3-demo-bucket-" + UUID.randomUUID(); static final S3AsyncClient s3AsyncClient = S3AsyncClient.create(); static String FILE_CSV = "csv"; static String FILE_JSON = "json"; diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateRetentionJob.java b/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateRetentionJob.java index 1765a038d5b..417c0b805da 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateRetentionJob.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateRetentionJob.java @@ -33,7 +33,9 @@ import java.time.Instant; import java.util.List; import java.util.Map; +import java.util.UUID; import java.util.stream.Collectors; + /** * Before running this example: *

@@ -45,7 +47,8 @@ */ public class CreateRetentionJob { private static final String STACK_NAME = "MyS3Stack"; - public static void main(String [] args) throws IOException, ParseException { + + public static void main(String[] args) throws IOException, ParseException { S3Client s3 = S3Client.create(); S3ControlClient s3ControlClient = S3ControlClient.create(); @@ -58,16 +61,16 @@ public static void main(String [] args) throws IOException, ParseException { String accountId = getAccountId(); // Specify your S3 bucket name. - String bucketName = "amazon-s3-demo-manifest-bucket" ; + String bucketName = "amzn-s3-demo-bucket-" + UUID.randomUUID(); // Change bucket name. System.out.println("Populate the bucket with the required files."); String[] fileNames = {"job-manifest.csv", "object-key-1.txt", "object-key-2.txt", "object-key-3.txt", "object-key-4.txt"}; uploadFilesToBucket(s3, bucketName, fileNames); String jobId = createComplianceRetentionJob(s3ControlClient, iamRoleArn, bucketName, accountId); - System.out.println("The job Id is "+jobId); + System.out.println("The job Id is " + jobId); // Create a legal Hold Off Job. - String jobHoldOffId =createLegalHoldOffJob(s3ControlClient, iamRoleArn, bucketName, accountId); - System.out.println("The id of the hold off job is "+jobHoldOffId); + String jobHoldOffId = createLegalHoldOffJob(s3ControlClient, iamRoleArn, bucketName, accountId); + System.out.println("The id of the hold off job is " + jobHoldOffId); CloudFormationHelper.destroyCloudFormationStack(STACK_NAME); } @@ -91,9 +94,9 @@ public static void uploadFilesToBucket(S3Client s3, String bucketName, String[] /** * Uploads a file to an Amazon S3 bucket. * - * @param s3 The {@link S3Client} instance used to interact with the Amazon S3 service. + * @param s3 The {@link S3Client} instance used to interact with the Amazon S3 service. * @param bucketName The name of the Amazon S3 bucket where the file will be uploaded. - * @param fileName The name of the file to be uploaded. + * @param fileName The name of the file to be uploaded. */ public static void populateBucket(S3Client s3, String bucketName, String fileName) { Path filePath = Paths.get("src/main/resources/batch/", fileName).toAbsolutePath(); @@ -110,7 +113,7 @@ public static void populateBucket(S3Client s3, String bucketName, String fileNam * Updates the first value in each line of a CSV file located at the specified path. * * @param newValue the new value to be set for the first field in each line of the CSV file - * @throws IOException if an I/O error occurs while reading or writing the CSV file + * @throws IOException if an I/O error occurs while reading or writing the CSV file * @throws NullPointerException if the {@code newValue} parameter is {@code null} */ public static void updateCSV(String newValue) { @@ -137,9 +140,10 @@ public static void updateCSV(String newValue) { } // snippet-start:[s3control.java2.create_job.compliance.main] + /** * Creates a compliance retention job in Amazon S3 Control. - * + *

* A compliance retention job in Amazon S3 Control is a feature that allows you to * set a retention period for objects stored in an S3 bucket. * This feature is particularly useful for organizations that need to comply with @@ -184,7 +188,7 @@ public static String createComplianceRetentionJob(final S3ControlClient s3Contro .build(); // Report details. - final String jobReportBucketArn = "arn:aws:s3:::"+bucketName; + final String jobReportBucketArn = "arn:aws:s3:::" + bucketName; final String jobReportPrefix = "reports/compliance-objects-bops"; JobReport jobReport = JobReport.builder() @@ -214,15 +218,15 @@ public static String createComplianceRetentionJob(final S3ControlClient s3Contro } // snippet-end:[s3control.java2.create_job.compliance.main] - // snippet-start:[s3control.java2.create_job.legal.off.main] + /** * Creates a legal hold off job in an S3 bucket. * * @param s3ControlClient the S3 Control client used to create the job - * @param roleArn the ARN of the IAM role to use for the job - * @param bucketName the name of the S3 bucket to create the job report in - * @param accountId the AWS account ID to create the job in + * @param roleArn the ARN of the IAM role to use for the job + * @param bucketName the name of the S3 bucket to create the job report in + * @param accountId the AWS account ID to create the job in * @return the job ID of the created job */ public static String createLegalHoldOffJob(final S3ControlClient s3ControlClient, String roleArn, String bucketName, String accountId) { @@ -259,7 +263,7 @@ public static String createLegalHoldOffJob(final S3ControlClient s3ControlClient .build(); // Report details. - final String jobReportBucketArn = "arn:aws:s3:::"+bucketName; + final String jobReportBucketArn = "arn:aws:s3:::" + bucketName; final String jobReportPrefix = "reports/compliance-objects-bops"; JobReport jobReport = JobReport.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/batch/S3BatchScenario.java b/javav2/example_code/s3/src/main/java/com/example/s3/batch/S3BatchScenario.java index cfe7abac0aa..0152d267213 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/batch/S3BatchScenario.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/batch/S3BatchScenario.java @@ -54,7 +54,7 @@ public static void main(String[] args) throws IOException { System.out.println(DASHES); System.out.println("Setup the required bucket for this scenario."); waitForInputToContinue(scanner); - String bucketName = "amazon-s3-demo-bucket-" + UUID.randomUUID(); + String bucketName = "amzn-s3-demo-bucket-" + UUID.randomUUID(); // Change bucket name. actions.createBucket(bucketName); String reportBucketName = "arn:aws:s3:::"+bucketName; String manifestLocation = "arn:aws:s3:::"+bucketName+"/job-manifest.csv"; From 50c838ac6b014652fb2e2306c5d6b7643065743f Mon Sep 17 00:00:00 2001 From: scmacdon Date: Fri, 4 Oct 2024 12:09:09 -0400 Subject: [PATCH 07/10] updated S3 Java files to adhere to bucket names --- .../s3/AbortMultipartUploadExamples.java | 8 ++- .../com/example/s3/BasicOpsWithChecksums.java | 2 +- .../com/example/s3/CheckObjectIntegrity.java | 26 ++++++++ .../com/example/s3/CopyObjectStorage.java | 8 +++ .../com/example/s3/CreateAccessPoint.java | 19 +++++- .../com/example/s3/DeleteBucketPolicy.java | 8 +++ .../com/example/s3/DeleteMultiObjects.java | 6 ++ .../s3/DeleteWebsiteConfiguration.java | 7 ++ .../GeneratePresignedGetUrlAndRetrieve.java | 2 +- ...atePresignedUrlAndPutFileWithMetadata.java | 2 +- .../GeneratePresignedUrlAndUploadObject.java | 2 +- .../src/main/java/com/example/s3/GetAcl.java | 8 +++ .../java/com/example/s3/GetBucketPolicy.java | 7 ++ .../com/example/s3/GetObjectContentType.java | 7 ++ ...nseTransformer.java => GetObjectData.java} | 12 +++- .../com/example/s3/GetObjectPresignedUrl.java | 10 +++ .../example/s3/GetObjectRestoreStatus.java | 8 +++ .../java/com/example/s3/GetObjectTags.java | 7 ++ .../java/com/example/s3/GetObjectUrl.java | 8 +++ .../src/main/java/com/example/s3/HelloS3.java | 6 +- .../com/example/s3/KMSEncryptionExample.java | 47 ++++++++++++-- .../example/s3/LifecycleConfiguration.java | 27 +++++++- .../main/java/com/example/s3/ListBuckets.java | 6 ++ .../com/example/s3/ListMultipartUploads.java | 6 ++ .../com/example/s3/ListObjectsPaginated.java | 6 ++ .../com/example/s3/ManagingObjectTags.java | 23 ++++++- .../example/s3/PerformMultiPartUpload.java | 41 +++++++++++- .../java/com/example/s3/PresignedSwing.java | 65 ------------------- .../java/com/example/s3/PutBucketLogging.java | 13 +++- .../com/example/s3/PutObjectMetadata.java | 10 ++- .../com/example/s3/PutObjectRetention.java | 9 +++ .../java/com/example/s3/RestoreObject.java | 8 +++ .../java/com/example/s3/S3BucketDeletion.java | 7 ++ .../main/java/com/example/s3/S3BucketOps.java | 14 +++- .../src/main/java/com/example/s3/S3Cors.java | 25 +++++++ .../java/com/example/s3/S3ZipExample.java | 15 +++++ .../src/main/java/com/example/s3/SetAcl.java | 8 +++ .../java/com/example/s3/SetBucketPolicy.java | 15 ++++- .../example/s3/SetWebsiteConfiguration.java | 7 ++ .../java/com/example/s3/VPCCreateJob.java | 13 ++++ .../java/com/example/s3/VPCS3Example.java | 6 ++ .../s3/async/PutObjectFromStreamAsync.java | 2 +- .../example/s3/async/S3AsyncStreamOps.java | 1 - .../com/example/s3/batch/S3BatchActions.java | 5 +- .../s3/outposts/CreateOutpostsBucket.java | 2 +- .../com/example/s3/scenario/S3Actions.java | 2 + .../com/example/s3/scenario/S3Scenario.java | 21 +++--- .../s3/transfermanager/DownloadFile.java | 2 +- .../transfermanager/DownloadToDirectory.java | 2 +- .../s3/transfermanager/ObjectCopy.java | 2 +- .../s3/transfermanager/UploadADirectory.java | 2 +- 51 files changed, 464 insertions(+), 111 deletions(-) rename javav2/example_code/s3/src/main/java/com/example/s3/{GetDataResponseTransformer.java => GetObjectData.java} (81%) delete mode 100644 javav2/example_code/s3/src/main/java/com/example/s3/PresignedSwing.java diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/AbortMultipartUploadExamples.java b/javav2/example_code/s3/src/main/java/com/example/s3/AbortMultipartUploadExamples.java index 47afd015831..a52fb742955 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/AbortMultipartUploadExamples.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/AbortMultipartUploadExamples.java @@ -54,7 +54,7 @@ */ public class AbortMultipartUploadExamples { - static final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); + static final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); // Change bucket name. static final String key = UUID.randomUUID().toString(); static final String classPathFilePath = "/multipartUploadFiles/s3-userguide.pdf"; static final String filePath = getFullFilePath(classPathFilePath); @@ -78,6 +78,12 @@ public static void doAbortIncompleteMultipartUploadsFromList() { } // snippet-start:[s3.java2.abort_upload_from_list] + /** + * Aborts all incomplete multipart uploads from the specified S3 bucket. + *

+ * This method retrieves a list of all incomplete multipart uploads in the specified S3 bucket, + * and then aborts each of those uploads. + */ public static void abortIncompleteMultipartUploadsFromList() { ListMultipartUploadsRequest listMultipartUploadsRequest = ListMultipartUploadsRequest.builder() .bucket(bucketName) diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/BasicOpsWithChecksums.java b/javav2/example_code/s3/src/main/java/com/example/s3/BasicOpsWithChecksums.java index c290852aac0..c590297902f 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/BasicOpsWithChecksums.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/BasicOpsWithChecksums.java @@ -43,7 +43,7 @@ // snippet-start:[s3.java2.basicOpsWithChecksums.full] public class BasicOpsWithChecksums { static final S3Client s3Client = S3Client.create(); - static final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); + static final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); // Change bucket name. static final String key = UUID.randomUUID().toString(); private static final Logger logger = LoggerFactory.getLogger(BasicOpsWithChecksums.class); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/CheckObjectIntegrity.java b/javav2/example_code/s3/src/main/java/com/example/s3/CheckObjectIntegrity.java index 3d3522f8e85..1a3023857ff 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/CheckObjectIntegrity.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/CheckObjectIntegrity.java @@ -86,6 +86,14 @@ public static void main(String[] args) { validateExistingFileAgainstS3Checksum(s3, bucketName, objectKey, objectPath); } + /** + * Uploads an object to an Amazon S3 bucket using the Multipart Upload API, with the object being bracketed by a checksum. + * + * @param s3 The S3Client instance used to interact with the Amazon S3 service. + * @param bucketName The name of the S3 bucket to upload the object to. + * @param objectKey The key (name) of the object to be uploaded. + * @param objectPath The local file path of the object to be uploaded. + */ public static void putS3MultipartObjectBracketedByChecksum(S3Client s3, String bucketName, String objectKey, String objectPath) { System.out.println("Starting uploading file with additional checksum."); @@ -137,6 +145,14 @@ public static void putS3MultipartObjectBracketedByChecksum(S3Client s3, String b } } + /** + * Downloads an S3 object that is split into multiple parts, and verifies the checksum of each part as well as the + * overall checksum of the entire object. + * + * @param s3 the S3 client used for interacting with the S3 service + * @param bucketName the name of the S3 bucket where the object is located + * @param objectKey the key of the S3 object to be downloaded + */ public static void downloadS3MultipartObjectBracketedByChecksum(S3Client s3, String bucketName, String objectKey) { System.out.println("Starting downloading file and doing validation"); @@ -196,6 +212,16 @@ public static void downloadS3MultipartObjectBracketedByChecksum(S3Client s3, Str } } + /** + * Validates the locally persisted file against the checksum stored in Amazon S3. + * + * @param s3 The S3Client instance to interact with Amazon S3. + * @param bucketName The name of the S3 bucket where the object is stored. + * @param objectKey The key of the S3 object to be validated. + * @param objectPath The local file path of the object to be validated. + * @throws IOException If there is an error reading the local file or validating the checksum. + * @throws NoSuchAlgorithmException If the specified algorithm (SHA-256) is not available. + */ public static void validateExistingFileAgainstS3Checksum(S3Client s3, String bucketName, String objectKey, String objectPath) { System.out.println("Starting validating the locally persisted file."); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/CopyObjectStorage.java b/javav2/example_code/s3/src/main/java/com/example/s3/CopyObjectStorage.java index 83166ef73dc..a40b185db9f 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/CopyObjectStorage.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/CopyObjectStorage.java @@ -51,6 +51,14 @@ public static void main(String[] args) { s3.close(); } + /** + * Copies an object from one Amazon S3 bucket to another, and stores the object in the DEEP_ARCHIVE storage class. + * + * @param s3 the S3Client object used to interact with the Amazon S3 service + * @param fromBucket the name of the bucket from which the object is being copied + * @param objectKey the name of the object to be copied + * @param toBucket the name of the bucket to which the object is being copied + */ public static void copyBucketObject(S3Client s3, String fromBucket, String objectKey, String toBucket) { CopyObjectRequest copyReq = CopyObjectRequest.builder() .sourceBucket(fromBucket) diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/CreateAccessPoint.java b/javav2/example_code/s3/src/main/java/com/example/s3/CreateAccessPoint.java index c0285d253c1..00900053d3b 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/CreateAccessPoint.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/CreateAccessPoint.java @@ -53,7 +53,16 @@ public static void main(String[] args) { s3ControlClient.close(); } - // This method creates an access point for the given Amazon S3 bucket. + /** + * Creates a specific access point on an Amazon S3 bucket. + * + * @param s3ControlClient the S3 Control client to use for the operation + * @param accountId the AWS account ID associated with the bucket + * @param bucketName the name of the S3 bucket + * @param accessPointName the name of the access point to be created + * + * @throws S3ControlException if there is an error creating the access point + */ public static void createSpecificAccessPoint(S3ControlClient s3ControlClient, String accountId, String bucketName, String accessPointName) { try { @@ -72,6 +81,14 @@ public static void createSpecificAccessPoint(S3ControlClient s3ControlClient, St } } + /** + * Deletes a specific S3 access point. + * + * @param s3ControlClient the S3 Control client to use for the operation + * @param accountId the account ID of the access point to delete + * @param accessPointName the name of the access point to delete + * @throws S3ControlException if an error occurs while deleting the access point + */ public static void deleteSpecificAccessPoint(S3ControlClient s3ControlClient, String accountId, String accessPointName) { try { diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/DeleteBucketPolicy.java b/javav2/example_code/s3/src/main/java/com/example/s3/DeleteBucketPolicy.java index cc751c1d538..0feaa411b55 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/DeleteBucketPolicy.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/DeleteBucketPolicy.java @@ -47,6 +47,14 @@ public static void main(String[] args) { s3.close(); } + /** + * Deletes the S3 bucket policy for the specified bucket. + * + * @param s3 the {@link S3Client} instance to use for the operation + * @param bucketName the name of the S3 bucket for which the policy should be deleted + * + * @throws S3Exception if there is an error deleting the bucket policy + */ public static void deleteS3BucketPolicy(S3Client s3, String bucketName) { DeleteBucketPolicyRequest delReq = DeleteBucketPolicyRequest.builder() .bucket(bucketName) diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/DeleteMultiObjects.java b/javav2/example_code/s3/src/main/java/com/example/s3/DeleteMultiObjects.java index b0ececa7497..caca3c4e29e 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/DeleteMultiObjects.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/DeleteMultiObjects.java @@ -52,6 +52,12 @@ public static void main(String[] args) { s3.close(); } + /** + * Deletes multiple objects from an Amazon S3 bucket. + * + * @param s3 An Amazon S3 client object. + * @param bucketName The name of the Amazon S3 bucket to delete objects from. + */ public static void deleteBucketObjects(S3Client s3, String bucketName) { // Upload three sample objects to the specfied Amazon S3 bucket. ArrayList keys = new ArrayList<>(); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/DeleteWebsiteConfiguration.java b/javav2/example_code/s3/src/main/java/com/example/s3/DeleteWebsiteConfiguration.java index 53c4613b06b..25a21edab66 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/DeleteWebsiteConfiguration.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/DeleteWebsiteConfiguration.java @@ -48,6 +48,13 @@ public static void main(String[] args) { s3.close(); } + /** + * Deletes the website configuration for an Amazon S3 bucket. + * + * @param s3 The {@link S3Client} instance used to interact with Amazon S3. + * @param bucketName The name of the S3 bucket for which the website configuration should be deleted. + * @throws S3Exception If an error occurs while deleting the website configuration. + */ public static void deleteBucketWebsiteConfig(S3Client s3, String bucketName) { DeleteBucketWebsiteRequest delReq = DeleteBucketWebsiteRequest.builder() .bucket(bucketName) diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedGetUrlAndRetrieve.java b/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedGetUrlAndRetrieve.java index c18865dbc15..faf3844c9e5 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedGetUrlAndRetrieve.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedGetUrlAndRetrieve.java @@ -48,7 +48,7 @@ public class GeneratePresignedGetUrlAndRetrieve { private final static S3Client s3Client = S3Client.create(); public static void main(String[] args) { - String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); + String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); // Change bucket name. String keyName = "key" + UUID.randomUUID(); String resourcePath = "multipartUploadFiles/s3-userguide.pdf"; diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedUrlAndPutFileWithMetadata.java b/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedUrlAndPutFileWithMetadata.java index 5c4bf935b06..4ffc23ed689 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedUrlAndPutFileWithMetadata.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedUrlAndPutFileWithMetadata.java @@ -52,7 +52,7 @@ public class GeneratePresignedUrlAndPutFileWithMetadata { private final static S3Client s3Client = S3Client.create(); public static void main(String[] args) { - String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); + String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); // Change bucket name. String keyName = "key" + UUID.randomUUID(); String resourcePath = "multipartUploadFiles/s3-userguide.pdf"; // Uncomment the following two lines and comment out the previous two lines to use an image file instead of a PDF file. diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedUrlAndUploadObject.java b/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedUrlAndUploadObject.java index bead5a1f989..cd116e5f7f3 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedUrlAndUploadObject.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GeneratePresignedUrlAndUploadObject.java @@ -39,7 +39,7 @@ public class GeneratePresignedUrlAndUploadObject { private static final Logger logger = org.slf4j.LoggerFactory.getLogger(GeneratePresignedUrlAndUploadObject.class); public static void main(String[] args) { - String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); + String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); // Change bucket name. String keyName = "key" + UUID.randomUUID(); try (S3Client s3Client = S3Client.create()) { diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GetAcl.java b/javav2/example_code/s3/src/main/java/com/example/s3/GetAcl.java index 9970fbd2b4e..0fb9f74d9cf 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GetAcl.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GetAcl.java @@ -56,6 +56,14 @@ public static void main(String[] args) { System.out.println("Done!"); } + /** + * Retrieves the Access Control List (ACL) for an object in an Amazon S3 bucket. + * + * @param s3 The S3Client object used to interact with the Amazon S3 service. + * @param objectKey The key of the object for which the ACL is to be retrieved. + * @param bucketName The name of the bucket containing the object. + * @return The ID of the grantee who has permission on the object, or an empty string if an error occurs. + */ public static String getBucketACL(S3Client s3, String objectKey, String bucketName) { try { GetObjectAclRequest aclReq = GetObjectAclRequest.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GetBucketPolicy.java b/javav2/example_code/s3/src/main/java/com/example/s3/GetBucketPolicy.java index 57d62b40ba2..b913b4951ba 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GetBucketPolicy.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GetBucketPolicy.java @@ -50,6 +50,13 @@ public static void main(String[] args) { s3.close(); } + /** + * Retrieves the policy for the specified Amazon S3 bucket. + * + * @param s3 the {@link S3Client} instance to use for making the request + * @param bucketName the name of the S3 bucket for which to retrieve the policy + * @return the policy text for the specified bucket, or an empty string if an error occurs + */ public static String getPolicy(S3Client s3, String bucketName) { String policyText; System.out.format("Getting policy for bucket: \"%s\"\n\n", bucketName); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectContentType.java b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectContentType.java index c359d726ef8..dbef47158ca 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectContentType.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectContentType.java @@ -49,6 +49,13 @@ public static void main(String[] args) { s3.close(); } + /** + * Retrieves the content type of an object stored in an Amazon S3 bucket. + * + * @param s3 an instance of the {@link S3Client} class, which is used to interact with the Amazon S3 service + * @param bucketName the name of the S3 bucket where the object is stored + * @param keyName the key (file name) of the object in the S3 bucket + */ public static void getContentType(S3Client s3, String bucketName, String keyName) { try { HeadObjectRequest objectRequest = HeadObjectRequest.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GetDataResponseTransformer.java b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectData.java similarity index 81% rename from javav2/example_code/s3/src/main/java/com/example/s3/GetDataResponseTransformer.java rename to javav2/example_code/s3/src/main/java/com/example/s3/GetObjectData.java index 94713b01dfb..7f99ced252f 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GetDataResponseTransformer.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectData.java @@ -29,7 +29,7 @@ * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ -public class GetDataResponseTransformer { +public class GetObjectData { public static void main(String[] args) { final String usage = """ @@ -59,6 +59,16 @@ public static void main(String[] args) { s3.close(); } + /** + * Retrieves the bytes of an object stored in an Amazon S3 bucket and saves them to a local file. + * + * @param s3 The S3Client instance used to interact with the Amazon S3 service. + * @param bucketName The name of the S3 bucket where the object is stored. + * @param keyName The key (or name) of the S3 object. + * @param path The local file path where the object's bytes will be saved. + * @throws IOException If an I/O error occurs while writing the bytes to the local file. + * @throws S3Exception If an error occurs while retrieving the object from the S3 bucket. + */ public static void getObjectBytes(S3Client s3, String bucketName, String keyName, String path) { try { GetObjectRequest objectRequest = GetObjectRequest diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectPresignedUrl.java b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectPresignedUrl.java index 957171ef0a5..4637e0cc0ee 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectPresignedUrl.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectPresignedUrl.java @@ -57,6 +57,16 @@ public static void main(String[] args) { presigner.close(); } + /** + * Generates a pre-signed URL for an Amazon S3 object. + * + * @param presigner The {@link S3Presigner} instance to use for generating the pre-signed URL. + * @param bucketName The name of the Amazon S3 bucket where the object is stored. + * @param keyName The key name (file name) of the object in the Amazon S3 bucket. + * + * @throws S3Exception If there is an error interacting with the Amazon S3 service. + * @throws IOException If there is an error opening the HTTP connection or reading/writing the request/response. + */ public static void getPresignedUrl(S3Presigner presigner, String bucketName, String keyName) { try { GetObjectRequest getObjectRequest = GetObjectRequest.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectRestoreStatus.java b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectRestoreStatus.java index 6c92ada67b7..de3d82c680a 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectRestoreStatus.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectRestoreStatus.java @@ -40,6 +40,14 @@ public static void main(String[] args) { s3.close(); } + /** + * Checks the restoration status of an Amazon S3 object. + * + * @param s3 an instance of the {@link S3Client} class used to interact with the Amazon S3 service + * @param bucketName the name of the Amazon S3 bucket where the object is stored + * @param keyName the name of the Amazon S3 object to be checked + * @throws S3Exception if an error occurs while interacting with the Amazon S3 service + */ public static void checkStatus(S3Client s3, String bucketName, String keyName) { try { HeadObjectRequest headObjectRequest = HeadObjectRequest.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectTags.java b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectTags.java index de5cb1edb04..09d4c40e369 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectTags.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectTags.java @@ -53,6 +53,13 @@ public static void main(String[] args) { s3.close(); } + /** + * Lists the tags associated with an Amazon S3 object. + * + * @param s3 the S3Client object used to interact with the Amazon S3 service + * @param bucketName the name of the S3 bucket that contains the object + * @param keyName the key (name) of the S3 object + */ public static void listTags(S3Client s3, String bucketName, String keyName) { try { GetObjectTaggingRequest getTaggingRequest = GetObjectTaggingRequest diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectUrl.java b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectUrl.java index e5d96764ac2..1553f422412 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectUrl.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/GetObjectUrl.java @@ -51,6 +51,14 @@ public static void main(String[] args) { s3.close(); } + /** + * Retrieves the URL for a specific object in an Amazon S3 bucket. + * + * @param s3 the S3Client object used to interact with the Amazon S3 service + * @param bucketName the name of the S3 bucket where the object is stored + * @param keyName the name of the object for which the URL should be retrieved + * @throws S3Exception if there is an error retrieving the URL for the specified object + */ public static void getURL(S3Client s3, String bucketName, String keyName) { try { GetUrlRequest request = GetUrlRequest.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/HelloS3.java b/javav2/example_code/s3/src/main/java/com/example/s3/HelloS3.java index cad4df590b0..e166e618421 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/HelloS3.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/HelloS3.java @@ -10,7 +10,6 @@ import software.amazon.awssdk.services.s3.model.Bucket; import software.amazon.awssdk.services.s3.model.ListBucketsResponse; import software.amazon.awssdk.services.s3.model.S3Exception; - import java.util.List; /** @@ -31,6 +30,11 @@ public static void main(String[] args) { listBuckets(s3); } + /** + * Lists all the S3 buckets associated with the provided AWS S3 client. + * + * @param s3 the S3Client instance used to interact with the AWS S3 service + */ public static void listBuckets(S3Client s3) { try { ListBucketsResponse response = s3.listBuckets(); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/KMSEncryptionExample.java b/javav2/example_code/s3/src/main/java/com/example/s3/KMSEncryptionExample.java index b3fd9109350..75b49d85ae2 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/KMSEncryptionExample.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/KMSEncryptionExample.java @@ -79,7 +79,17 @@ public static void main(String[] args) { s3.close(); } - // Encrypt data and place the encrypted data into an Amazon S3 bucket. + /** + * Uploads an encrypted object to an Amazon S3 bucket. + * + * @param s3 The {@link S3Client} instance used to interact with Amazon S3. + * @param objectName The name of the object to be uploaded. + * @param bucketName The name of the Amazon S3 bucket where the object will be uploaded. + * @param objectPath The local file path of the object to be uploaded. + * @param keyId The ID of the encryption key to be used for encrypting the object data. + * + * @throws S3Exception If an error occurs while uploading the object to Amazon S3. + */ public static void putEncryptData(S3Client s3, String objectName, String bucketName, String objectPath, String keyId) { try { @@ -98,7 +108,15 @@ public static void putEncryptData(S3Client s3, String objectName, String bucketN } } - // Obtain the encrypted data, decrypt it, and write the data to a text file. + /** + * Retrieves encrypted data from an Amazon S3 bucket, decrypts it using the specified AWS Key Management Service (KMS) key, and writes the decrypted data to a local file. + * + * @param s3 the S3Client instance used to interact with Amazon S3 + * @param bucketName the name of the Amazon S3 bucket to retrieve the encrypted data from + * @param objectName the name of the object within the Amazon S3 bucket to retrieve + * @param path the local file path where the decrypted data will be written + * @param keyId the ID of the AWS KMS key to use for decrypting the data + */ public static void getEncryptedData(S3Client s3, String bucketName, String objectName, String path, String keyId) { try { GetObjectRequest objectRequest = GetObjectRequest.builder() @@ -128,7 +146,13 @@ public static void getEncryptedData(S3Client s3, String bucketName, String objec } } - // Encrypt the data passed as a byte array. + /** + * Encrypts the given data using the specified key ID in the AWS Key Management Service (KMS). + * + * @param keyId the ID of the KMS key to use for encryption + * @param data the data to be encrypted + * @return the encrypted data as a byte array, or null if an error occurred during the encryption process + */ private static byte[] encryptData(String keyId, byte[] data) { try { KmsClient kmsClient = getKMSClient(); @@ -155,7 +179,13 @@ private static byte[] encryptData(String keyId, byte[] data) { return null; } - // Decrypt the data passed as a byte array. + /** + * Decrypts the given encrypted data using the specified key ID. + * + * @param data the encrypted data to be decrypted + * @param keyId the ID of the KMS key to be used for decryption + * @return the decrypted data as a byte array, or {@code null} if an error occurs + */ private static byte[] decryptData(byte[] data, String keyId) { try { @@ -179,12 +209,15 @@ private static byte[] decryptData(byte[] data, String keyId) { return null; } - // Return a byte array. + /** + * Reads the contents of a file and returns it as a byte array. + * + * @param filePath the path to the file to be read + * @return a byte array containing the contents of the file, or null if an error occurred + */ private static byte[] getObjectFile(String filePath) { - FileInputStream fileInputStream = null; byte[] bytesArray = null; - try { File file = new File(filePath); bytesArray = new byte[(int) file.length()]; diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/LifecycleConfiguration.java b/javav2/example_code/s3/src/main/java/com/example/s3/LifecycleConfiguration.java index dc96e167c18..87a323465d4 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/LifecycleConfiguration.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/LifecycleConfiguration.java @@ -63,6 +63,15 @@ public static void main(String[] args) { s3.close(); } + /** + * Sets the lifecycle configuration for an Amazon S3 bucket. + * + * @param s3 The Amazon S3 client to use for the operation. + * @param bucketName The name of the Amazon S3 bucket. + * @param accountId The expected owner of the Amazon S3 bucket. + * + * @throws S3Exception if there is an error setting the lifecycle configuration. + */ public static void setLifecycleConfig(S3Client s3, String bucketName, String accountId) { try { // Create a rule to archive objects with the "glacierobjects/" prefix to Amazon @@ -127,7 +136,13 @@ public static void setLifecycleConfig(S3Client s3, String bucketName, String acc } } - // Retrieve the configuration and add a new rule. + /** + * Retrieves the lifecycle configuration for an Amazon S3 bucket and adds a new lifecycle rule to it. + * + * @param s3 the S3Client instance used to interact with Amazon S3 + * @param bucketName the name of the Amazon S3 bucket + * @param accountId the expected owner of the Amazon S3 bucket + */ public static void getLifecycleConfig(S3Client s3, String bucketName, String accountId) { try { GetBucketLifecycleConfigurationRequest getBucketLifecycleConfigurationRequest = GetBucketLifecycleConfigurationRequest @@ -182,7 +197,15 @@ public static void getLifecycleConfig(S3Client s3, String bucketName, String acc } } - // Delete the configuration from the Amazon S3 bucket. + /** + * Deletes the lifecycle configuration for an Amazon S3 bucket. + * + * @param s3 the {@link S3Client} to use for the operation + * @param bucketName the name of the S3 bucket + * @param accountId the expected account owner of the S3 bucket + * + * @throws S3Exception if an error occurs while deleting the lifecycle configuration + */ public static void deleteLifecycleConfig(S3Client s3, String bucketName, String accountId) { try { DeleteBucketLifecycleRequest deleteBucketLifecycleRequest = DeleteBucketLifecycleRequest diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/ListBuckets.java b/javav2/example_code/s3/src/main/java/com/example/s3/ListBuckets.java index 591fc4eed56..4717df9de74 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/ListBuckets.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/ListBuckets.java @@ -28,6 +28,12 @@ public static void main(String[] args) { listAllBuckets(s3); } + + /** + * Lists all the S3 buckets available in the current AWS account. + * + * @param s3 The {@link S3Client} instance to use for interacting with the Amazon S3 service. + */ public static void listAllBuckets(S3Client s3) { ListBucketsResponse response = s3.listBuckets(); List bucketList = response.buckets(); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/ListMultipartUploads.java b/javav2/example_code/s3/src/main/java/com/example/s3/ListMultipartUploads.java index 0ca810ad817..834bb801627 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/ListMultipartUploads.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/ListMultipartUploads.java @@ -48,6 +48,12 @@ public static void main(String[] args) { s3.close(); } + /** + * Lists the multipart uploads currently in progress in the specified Amazon S3 bucket. + * + * @param s3 the S3Client object used to interact with Amazon S3 + * @param bucketName the name of the Amazon S3 bucket to list the multipart uploads for + */ public static void listUploads(S3Client s3, String bucketName) { try { ListMultipartUploadsRequest listMultipartUploadsRequest = ListMultipartUploadsRequest.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/ListObjectsPaginated.java b/javav2/example_code/s3/src/main/java/com/example/s3/ListObjectsPaginated.java index 563ca096b8d..78db4520a44 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/ListObjectsPaginated.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/ListObjectsPaginated.java @@ -37,6 +37,12 @@ public static void main(String[] args) { s3.close(); } + /** + * Lists the objects in the specified S3 bucket. + * + * @param s3 the S3Client instance used to interact with Amazon S3 + * @param bucketName the name of the S3 bucket to list the objects from + */ public static void listBucketObjects(S3Client s3, String bucketName) { try { ListObjectsV2Request listReq = ListObjectsV2Request.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/ManagingObjectTags.java b/javav2/example_code/s3/src/main/java/com/example/s3/ManagingObjectTags.java index ba27e65548c..f6be14bffa4 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/ManagingObjectTags.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/ManagingObjectTags.java @@ -65,6 +65,14 @@ public static void main(String[] args) { } // snippet-start:[s3.java2.s3_object_manage_tags.main] + /** + * Puts tags on an Amazon S3 object. + * + * @param s3 An {@link S3Client} object that represents the Amazon S3 client. + * @param bucketName The name of the Amazon S3 bucket. + * @param objectKey The key of the Amazon S3 object. + * @param objectPath The file path of the object to be uploaded. + */ public static void putS3ObjectTags(S3Client s3, String bucketName, String objectKey, String objectPath) { try { Tag tag1 = Tag.builder() @@ -99,6 +107,14 @@ public static void putS3ObjectTags(S3Client s3, String bucketName, String object } } + /** + * Updates the tags associated with an object in an Amazon S3 bucket. + * + * @param s3 an instance of the S3Client class, which is used to interact with the Amazon S3 service + * @param bucketName the name of the S3 bucket containing the object + * @param objectKey the key (or name) of the object in the S3 bucket + * @throws S3Exception if there is an error updating the object's tags + */ public static void updateObjectTags(S3Client s3, String bucketName, String objectKey) { try { GetObjectTaggingRequest taggingRequest = GetObjectTaggingRequest.builder() @@ -152,7 +168,12 @@ public static void updateObjectTags(S3Client s3, String bucketName, String objec } } - // Return a byte array. + /** + * Retrieves the contents of a file as a byte array. + * + * @param filePath the path of the file to be read + * @return a byte array containing the contents of the file, or null if an error occurs + */ private static byte[] getObjectFile(String filePath) { FileInputStream fileInputStream = null; byte[] bytesArray = null; diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/PerformMultiPartUpload.java b/javav2/example_code/s3/src/main/java/com/example/s3/PerformMultiPartUpload.java index 65b41814479..ffd0e700574 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/PerformMultiPartUpload.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/PerformMultiPartUpload.java @@ -38,7 +38,7 @@ // snippet-start:[s3.java2.performMultiPartUpload.full] public class PerformMultiPartUpload { static final S3Client s3Client = S3Client.create(); - static final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); + static final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); // Change bucket name. static final String key = UUID.randomUUID().toString(); static final String classPathFilePath = "/multipartUploadFiles/s3-userguide.pdf"; static final String filePath = getFullFilePath(classPathFilePath); @@ -51,6 +51,13 @@ public static void main(String[] args) { performMultiPartUpload.doMultipartUploadWithS3Client(); } + /** + * Retrieves the full file path of a resource using the given file path. + * + * @param filePath the relative file path of the resource + * @return the full file path of the resource + * @throws RuntimeException if the file path is invalid or cannot be converted to a URI + */ static String getFullFilePath(String filePath) { URL uploadDirectoryURL = PerformMultiPartUpload.class.getResource(filePath); String fullFilePath; @@ -62,6 +69,15 @@ static String getFullFilePath(String filePath) { return fullFilePath; } + /** + * Creates an Amazon S3 bucket with the specified bucket name. + *

+ * This method uses the {@link software.amazon.awssdk.services.s3.S3Client} to create a new S3 bucket. It also waits for the + * bucket to be successfully created using the {@link software.amazon.awssdk.services.s3.waiters.S3Waiter}. + *

+ * + * @throws software.amazon.awssdk.services.s3.model.S3Exception if there is an error creating the bucket + */ static void createBucket() { s3Client.createBucket(b -> b.bucket(bucketName)); try (S3Waiter s3Waiter = s3Client.waiter()) { @@ -69,12 +85,25 @@ static void createBucket() { } } + /** + * Deletes the resources stored in the specified S3 bucket. + *

+ * This method first deletes the object with the specified key from the S3 bucket, + * and then deletes the S3 bucket itself. + * + * @throws RuntimeException if there is an error deleting the resources + */ static void deleteResources() { s3Client.deleteObject(b -> b.bucket(bucketName).key(key)); s3Client.deleteBucket(b -> b.bucket(bucketName)); } // snippet-start:[s3.java2.performMultiPartUpload.transferManager] + /** + * Uploads a file to an Amazon S3 bucket using the S3TransferManager. + * + * @param filePath the file path of the file to be uploaded + */ public void multipartUploadWithTransferManager(String filePath) { S3TransferManager transferManager = S3TransferManager.create(); UploadFileRequest uploadFileRequest = UploadFileRequest.builder() @@ -90,6 +119,11 @@ public void multipartUploadWithTransferManager(String filePath) { // snippet-end:[s3.java2.performMultiPartUpload.transferManager] // snippet-start:[s3.java2.performMultiPartUpload.s3Client] + /** + * Performs a multipart upload to Amazon S3 using the provided S3 client. + * + * @param filePath the path to the file to be uploaded + */ public void multipartUploadWithS3Client(String filePath) { // Initiate the multipart upload. @@ -146,6 +180,11 @@ public void multipartUploadWithS3Client(String filePath) { // snippet-end:[s3.java2.performMultiPartUpload.s3Client] // snippet-start:[s3.java2.performMultiPartUpload.s3AsyncClient] + /** + * Uploads a file to an S3 bucket using the S3AsyncClient and enabling multipart support. + * + * @param filePath the local file path of the file to be uploaded + */ public void multipartUploadWithS3AsyncClient(String filePath) { // Enable multipart support. S3AsyncClient s3AsyncClient = S3AsyncClient.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/PresignedSwing.java b/javav2/example_code/s3/src/main/java/com/example/s3/PresignedSwing.java deleted file mode 100644 index fc1635a3daa..00000000000 --- a/javav2/example_code/s3/src/main/java/com/example/s3/PresignedSwing.java +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -// SPDX-License-Identifier: Apache-2.0 - -package com.example.s3; - -import javax.swing.JFrame; -import javax.swing.JButton; -import java.awt.event.ActionListener; -import java.awt.event.ActionEvent; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; - -class Swing implements ActionListener { - JFrame frame = new JFrame(); - JButton button = new JButton("Get Presigned Amazon S3 Object"); - - Swing() { - prepareGUI(); - buttonProperties(); - } - - public void prepareGUI() { - frame.setTitle("My Window"); - frame.getContentPane().setLayout(null); - frame.setVisible(true); - frame.setBounds(200, 200, 400, 400); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - } - - public void buttonProperties() { - button.setBounds(130, 200, 200, 100); - frame.add(button); - button.addActionListener(this); - } - - @Override - public void actionPerformed(ActionEvent e) { - // Get a presigned PDF from an Amazon S3 bucket. - try { - URL url = new URL(""); - InputStream in; - in = url.openStream(); - FileOutputStream fos = new FileOutputStream("C:\\AWS\\allpeople.png"); - System.out.println("reading from resource and writing to file..."); - int length; - byte[] buffer = new byte[1024];// buffer for portion of data from connection - while ((length = in.read(buffer)) > -1) { - fos.write(buffer, 0, length); - } - fos.close(); - in.close(); - System.out.println("File downloaded"); - } catch (IOException ex) { - ex.printStackTrace(); - } - } -} - -public class PresignedSwing { - public static void main(String[] args) { - new Swing(); - } -} \ No newline at end of file diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/PutBucketLogging.java b/javav2/example_code/s3/src/main/java/com/example/s3/PutBucketLogging.java index cd9873f93ca..6ae08c33c9f 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/PutBucketLogging.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/PutBucketLogging.java @@ -5,7 +5,6 @@ // snippet-start:[s3.java2.s3_put_log.main] // snippet-start:[s3.java2.s3_put_log.import] - import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.GetBucketAclRequest; @@ -17,7 +16,6 @@ import software.amazon.awssdk.services.s3.model.PutBucketLoggingRequest; import software.amazon.awssdk.services.s3.model.S3Exception; import software.amazon.awssdk.services.s3.model.TargetGrant; - import java.util.ArrayList; import java.util.List; // snippet-end:[s3.java2.s3_put_log.import] @@ -42,7 +40,7 @@ public static void main(String[] args) { targetBucket - The target bucket . """; - if (args.length != 3) { + if (args.length != 2) { System.out.println(usage); System.exit(1); } @@ -58,6 +56,15 @@ public static void main(String[] args) { s3.close(); } + /** + * Enables logging for the specified S3 bucket. + * + * @param s3 an instance of the {@link S3Client} used to interact with the S3 service + * @param bucketName the name of the bucket for which logging needs to be enabled + * @param targetBucket the name of the target bucket where the logs will be stored + * + * @throws S3Exception if an error occurs while enabling logging for the bucket + */ public static void setlogRequest(S3Client s3, String bucketName, String targetBucket) { try { GetBucketAclRequest aclRequest = GetBucketAclRequest.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/PutObjectMetadata.java b/javav2/example_code/s3/src/main/java/com/example/s3/PutObjectMetadata.java index 173bf33057c..1e12a4d528a 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/PutObjectMetadata.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/PutObjectMetadata.java @@ -57,8 +57,14 @@ public static void main(String[] args) { s3.close(); } - // This example uses RequestBody.fromFile to avoid loading the whole file into - // memory. + /** + * Uploads an object to an Amazon S3 bucket with metadata. + * + * @param s3 the S3Client object used to interact with the Amazon S3 service + * @param bucketName the name of the S3 bucket to upload the object to + * @param objectKey the name of the object to be uploaded + * @param objectPath the local file path of the object to be uploaded + */ public static void putS3Object(S3Client s3, String bucketName, String objectKey, String objectPath) { try { Map metadata = new HashMap<>(); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/PutObjectRetention.java b/javav2/example_code/s3/src/main/java/com/example/s3/PutObjectRetention.java index e5677849842..147aef4dc49 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/PutObjectRetention.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/PutObjectRetention.java @@ -55,6 +55,15 @@ public static void main(String[] args) { s3.close(); } + /** + * Sets the retention period for an object in an Amazon S3 bucket. + * + * @param s3 the S3Client object used to interact with the Amazon S3 service + * @param key the key (name) of the object in the S3 bucket + * @param bucket the name of the S3 bucket where the object is stored + * + * @throws S3Exception if an error occurs while setting the object retention period + */ public static void setRentionPeriod(S3Client s3, String key, String bucket) { try { LocalDate localDate = LocalDate.parse("2020-07-17"); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/RestoreObject.java b/javav2/example_code/s3/src/main/java/com/example/s3/RestoreObject.java index e1bb594bcb9..24d7e56b423 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/RestoreObject.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/RestoreObject.java @@ -55,6 +55,14 @@ public static void main(String[] args) { s3.close(); } + /** + * Restores an S3 object from the Glacier storage class. + * + * @param s3 an instance of the {@link S3Client} to be used for interacting with Amazon S3 + * @param bucketName the name of the S3 bucket where the object is stored + * @param keyName the key (object name) of the S3 object to be restored + * @param expectedBucketOwner the AWS account ID of the expected bucket owner + */ public static void restoreS3Object(S3Client s3, String bucketName, String keyName, String expectedBucketOwner) { try { RestoreRequest restoreRequest = RestoreRequest.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/S3BucketDeletion.java b/javav2/example_code/s3/src/main/java/com/example/s3/S3BucketDeletion.java index 999f4ede219..25c6d997b0b 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/S3BucketDeletion.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/S3BucketDeletion.java @@ -52,6 +52,13 @@ public static void main(String[] args) throws Exception { s3.close(); } + /** + * Deletes all objects in the specified S3 bucket and then deletes the bucket. + * + * @param s3 The S3Client instance to use for the S3 operations. + * @param bucket The name of the S3 bucket to delete. + * @throws S3Exception if any error occurs during the S3 operations. + */ public static void deleteObjectsInBucket(S3Client s3, String bucket) { try { // To delete a bucket, all the objects in the bucket must be deleted first. diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/S3BucketOps.java b/javav2/example_code/s3/src/main/java/com/example/s3/S3BucketOps.java index ef50f72ec4c..ddce10acd11 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/S3BucketOps.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/S3BucketOps.java @@ -42,7 +42,13 @@ public static void main(String[] args) { performOperations(s3, bucket); } - // Create a bucket by using a S3Waiter object + /** + * Creates an Amazon S3 bucket. + * + * @param s3Client the {@link S3Client} object used to interact with Amazon S3. + * @param bucketName the name of the bucket to create. + * @throws S3Exception if the bucket cannot be created. + */ public static void createBucket(S3Client s3Client, String bucketName) { try { S3Waiter s3Waiter = s3Client.waiter(); @@ -67,6 +73,12 @@ public static void createBucket(S3Client s3Client, String bucketName) { } // snippet-end:[s3.java2.s3_bucket_ops.create_bucket] + /** + * Performs various operations on an Amazon S3 bucket. + * + * @param s3 An {@link S3Client} object to interact with the Amazon S3 service. + * @param bucket The name of the Amazon S3 bucket to perform operations on. + */ public static void performOperations(S3Client s3, String bucket) { // snippet-start:[s3.java2.s3_bucket_ops.list_bucket] // List buckets diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/S3Cors.java b/javav2/example_code/s3/src/main/java/com/example/s3/S3Cors.java index 7276c01be8b..2ef534109b5 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/S3Cors.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/S3Cors.java @@ -59,6 +59,15 @@ public static void main(String[] args) { s3.close(); } + /** + * Deletes the CORS (Cross-Origin Resource Sharing) configuration for an Amazon S3 bucket. + * + * @param s3 the {@link S3Client} instance used to interact with the Amazon S3 service + * @param bucketName the name of the Amazon S3 bucket for which the CORS configuration should be deleted + * @param accountId the expected AWS account ID of the bucket owner + * + * @throws S3Exception if an error occurs while deleting the CORS configuration for the bucket + */ public static void deleteBucketCorsInformation(S3Client s3, String bucketName, String accountId) { try { DeleteBucketCorsRequest bucketCorsRequest = DeleteBucketCorsRequest.builder() @@ -74,6 +83,15 @@ public static void deleteBucketCorsInformation(S3Client s3, String bucketName, S } } + /** + * Retrieves the CORS (Cross-Origin Resource Sharing) configuration for the specified S3 bucket. + * + * @param s3 the S3Client instance to use for the operation + * @param bucketName the name of the S3 bucket to retrieve the CORS configuration for + * @param accountId the expected bucket owner's account ID + * + * @throws S3Exception if there is an error retrieving the CORS configuration + */ public static void getBucketCorsInformation(S3Client s3, String bucketName, String accountId) { try { GetBucketCorsRequest bucketCorsRequest = GetBucketCorsRequest.builder() @@ -95,6 +113,13 @@ public static void getBucketCorsInformation(S3Client s3, String bucketName, Stri } } + /** + * Sets the Cross-Origin Resource Sharing (CORS) rules for an Amazon S3 bucket. + * + * @param s3 The S3Client object used to interact with the Amazon S3 service. + * @param bucketName The name of the S3 bucket to set the CORS rules for. + * @param accountId The AWS account ID of the bucket owner. + */ public static void setCorsInformation(S3Client s3, String bucketName, String accountId) { List allowMethods = new ArrayList<>(); allowMethods.add("PUT"); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/S3ZipExample.java b/javav2/example_code/s3/src/main/java/com/example/s3/S3ZipExample.java index e899baa936f..cfdfe3a578e 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/S3ZipExample.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/S3ZipExample.java @@ -57,6 +57,13 @@ public static void main(String[] args) { createZIPFile(s3, bucketName, imageKeys); } + /** + * Creates a ZIP file containing the specified image keys from an S3 bucket and uploads it to S3. + * + * @param s3 the S3Client instance to use for interacting with S3 + * @param bucketName the name of the S3 bucket to use + * @param imageKeys an array of image keys to include in the ZIP file + */ public static void createZIPFile(S3Client s3, String bucketName, String[] imageKeys) { String uuid = java.util.UUID.randomUUID().toString(); String zipName = uuid + ".zip"; @@ -107,6 +114,14 @@ public static void createZIPFile(S3Client s3, String bucketName, String[] imageK } } + /** + * Generates a pre-signed URL for downloading an object from an Amazon S3 bucket. + * + * @param bucketName the name of the S3 bucket where the object is stored + * @param keyName the key (object name) of the object to be downloaded + * @return the pre-signed URL that can be used to download the object + * @throws S3Exception if an error occurs while generating the pre-signed URL + */ public static String signObjectToDownload(String bucketName, String keyName) { S3Presigner presignerOb = S3Presigner.builder() .region(Region.US_EAST_1) diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/SetAcl.java b/javav2/example_code/s3/src/main/java/com/example/s3/SetAcl.java index 0cf999cf198..196c56202e2 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/SetAcl.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/SetAcl.java @@ -58,6 +58,14 @@ public static void main(String[] args) { s3.close(); } + /** + * Sets the Access Control List (ACL) for an Amazon S3 bucket. + * + * @param s3 the S3Client instance to be used for the operation + * @param bucketName the name of the S3 bucket to set the ACL for + * @param id the ID of the AWS user or account that will be granted full control of the bucket + * @throws S3Exception if an error occurs while setting the bucket ACL + */ public static void setBucketAcl(S3Client s3, String bucketName, String id) { try { Grant ownerGrant = Grant.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/SetBucketPolicy.java b/javav2/example_code/s3/src/main/java/com/example/s3/SetBucketPolicy.java index 0c747f724a4..04b0249c67f 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/SetBucketPolicy.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/SetBucketPolicy.java @@ -58,6 +58,14 @@ public static void main(String[] args) { s3.close(); } + /** + * Sets the policy for an Amazon S3 bucket. + * + * @param s3 the {@link S3Client} object used to interact with the Amazon S3 service + * @param bucketName the name of the Amazon S3 bucket + * @param policyText the text of the policy to be set on the bucket + * @throws S3Exception if there is an error setting the bucket policy + */ public static void setPolicy(S3Client s3, String bucketName, String policyText) { System.out.println("Setting policy:"); System.out.println("----"); @@ -81,7 +89,12 @@ public static void setPolicy(S3Client s3, String bucketName, String policyText) System.out.println("Done!"); } - // Loads a JSON-formatted policy from a file + /** + * Retrieves the bucket policy from a specified file. + * + * @param policyFile the path to the file containing the bucket policy + * @return the content of the bucket policy file as a string + */ public static String getBucketPolicyFromFile(String policyFile) { StringBuilder fileText = new StringBuilder(); try { diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/SetWebsiteConfiguration.java b/javav2/example_code/s3/src/main/java/com/example/s3/SetWebsiteConfiguration.java index 82865094d0b..45fa0b76ebf 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/SetWebsiteConfiguration.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/SetWebsiteConfiguration.java @@ -51,6 +51,13 @@ public static void main(String[] args) { s3.close(); } + /** + * Sets the website configuration for an Amazon S3 bucket. + * + * @param s3 The {@link S3Client} instance to use for the AWS SDK operations. + * @param bucketName The name of the S3 bucket to configure. + * @param indexDoc The name of the index document to use for the website configuration. + */ public static void setWebsiteConfig(S3Client s3, String bucketName, String indexDoc) { try { WebsiteConfiguration websiteConfig = WebsiteConfiguration.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/VPCCreateJob.java b/javav2/example_code/s3/src/main/java/com/example/s3/VPCCreateJob.java index c36af46a7fc..c2dce119eb4 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/VPCCreateJob.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/VPCCreateJob.java @@ -78,6 +78,19 @@ public static void main(String[] args) throws URISyntaxException { s3ControlClient.close(); } + /** + * Creates an S3 Batch Operations job using the AWS Java SDK. + * + * @param s3ControlClient the S3 Control client used to create the job + * @param accountId the AWS account ID associated with the job + * @param iamRoleArn the ARN of the IAM role to be used for the job + * @param manifestLocation the Amazon S3 object location of the job manifest + * @param reportBucketName the name of the Amazon S3 bucket to store the job report + * @param tagKey the key of the tag to be added to the objects + * @param tagValue the value of the tag to be added to the objects + * @param eTag the ETag of the job manifest object + * @param uuid a unique identifier for the job request + */ public static void createS3Job(S3ControlClient s3ControlClient, String accountId, String iamRoleArn, String manifestLocation, String reportBucketName, String tagKey, String tagValue, String eTag, String uuid) { diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/VPCS3Example.java b/javav2/example_code/s3/src/main/java/com/example/s3/VPCS3Example.java index a8eec411ef5..1f74f027236 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/VPCS3Example.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/VPCS3Example.java @@ -55,6 +55,12 @@ public static void main(String[] args) throws URISyntaxException { s3.close(); } + /** + * Lists the objects in an Amazon S3 bucket. + * + * @param s3 an instance of the S3Client class, which is used to interact with the Amazon S3 service + * @param bucketName the name of the S3 bucket to list the objects for + */ public static void listBucketObjects(S3Client s3, String bucketName) { try { ListObjectsRequest listObjects = ListObjectsRequest.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/async/PutObjectFromStreamAsync.java b/javav2/example_code/s3/src/main/java/com/example/s3/async/PutObjectFromStreamAsync.java index 7eadc1ae508..42c61bd4ac4 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/async/PutObjectFromStreamAsync.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/async/PutObjectFromStreamAsync.java @@ -22,7 +22,7 @@ public class PutObjectFromStreamAsync { private static final Logger logger = LoggerFactory.getLogger(PutObjectFromStreamAsync.class); public static void main(String[] args) { - String bucketName = "amzn-s3-demo-bucket-" + UUID.randomUUID(); + String bucketName = "amzn-s3-demo-bucket-" + UUID.randomUUID(); // Change bucket name. String key = UUID.randomUUID().toString(); AsyncExampleUtils.createBucket(bucketName); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/async/S3AsyncStreamOps.java b/javav2/example_code/s3/src/main/java/com/example/s3/async/S3AsyncStreamOps.java index 5d747dcdffa..a09b5f628b4 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/async/S3AsyncStreamOps.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/async/S3AsyncStreamOps.java @@ -33,7 +33,6 @@ public static void main(String[] args) { Where: bucketName - The name of the Amazon S3 bucket (for example, bucket1).\s - objectKey - The name of the object (for example, book.pdf).\s path - The local path to the file (for example, C:/AWS/book.pdf).\s """; diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/batch/S3BatchActions.java b/javav2/example_code/s3/src/main/java/com/example/s3/batch/S3BatchActions.java index 2ff4d938d59..6cff30635e9 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/batch/S3BatchActions.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/batch/S3BatchActions.java @@ -5,6 +5,7 @@ import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; import software.amazon.awssdk.core.async.AsyncRequestBody; import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; +import software.amazon.awssdk.core.retry.RetryMode; import software.amazon.awssdk.core.retry.RetryPolicy; import software.amazon.awssdk.core.waiters.WaiterResponse; import software.amazon.awssdk.http.async.SdkAsyncHttpClient; @@ -128,9 +129,7 @@ private static S3AsyncClient getS3AsyncClient() { ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder() .apiCallTimeout(Duration.ofMinutes(2)) .apiCallAttemptTimeout(Duration.ofSeconds(90)) - .retryPolicy(RetryPolicy.builder() - .numRetries(3) - .build()) + .retryStrategy(RetryMode.STANDARD) .build(); s3AsyncClient = S3AsyncClient.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/outposts/CreateOutpostsBucket.java b/javav2/example_code/s3/src/main/java/com/example/s3/outposts/CreateOutpostsBucket.java index 40ce7230262..db03527e145 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/outposts/CreateOutpostsBucket.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/outposts/CreateOutpostsBucket.java @@ -24,7 +24,7 @@ public static void createOutpostsBucket() { try (S3ControlClient s3ControlClient = S3ControlClient.create()) { try { CreateBucketResponse response = s3ControlClient.createBucket(b -> b - .bucket("") + .bucket("") // Enter bucket name. .outpostId("op-<123456789abcdefgh>") .createBucketConfiguration(CreateBucketConfiguration.builder().build())); logger.info("Bucket created with Arn: [{}]", response.bucketArn()); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Actions.java b/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Actions.java index 5064b761973..bbe9d9a8484 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Actions.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Actions.java @@ -8,6 +8,7 @@ import software.amazon.awssdk.core.ResponseBytes; import software.amazon.awssdk.core.async.AsyncRequestBody; import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; +import software.amazon.awssdk.core.retry.RetryMode; import software.amazon.awssdk.core.waiters.WaiterResponse; import software.amazon.awssdk.http.async.SdkAsyncHttpClient; import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient; @@ -72,6 +73,7 @@ public static S3AsyncClient getAsyncClient() { ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder() .apiCallTimeout(Duration.ofMinutes(2)) // Set the overall API call timeout. .apiCallAttemptTimeout(Duration.ofSeconds(90)) // Set the individual call attempt timeout. + .retryStrategy(RetryMode.STANDARD) .build(); s3AsyncClient = S3AsyncClient.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Scenario.java b/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Scenario.java index 9f05ceefd14..557ec3b48c6 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Scenario.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Scenario.java @@ -44,25 +44,26 @@ public class S3Scenario { public static void main(String[] args) throws IOException { final String usage = """ Usage: - + Where: + bucketName - The name of the S3 bucket. key - The unique identifier for the object stored in the S3 bucket. objectPath - The full file path of the object within the S3 bucket (e.g., "documents/reports/annual_report.pdf"). savePath - The local file path where the object will be downloaded and saved (e.g., "C:/Users/username/Downloads/annual_report.pdf"). toBucket - The name of the S3 bucket to which the object will be copied. """; - // if (args.length != 4) { - // logger.info(usage); - // return; - // } + if (args.length != 5) { + logger.info(usage); + return; + } - String bucketName = "amazon-s3-demo-bucket-" + UUID.randomUUID(); - String key = "book.pdf" ; //args[0]; - String objectPath = "C:\\AWS\\book2.pdf" ; //args[1]; - String savePath = "C:\\AWS\\foo\\book33.pdf"; //args[2]; - String toBucket = "scottsitewise";// args[3]; + String bucketName = args[0]; + String key = args[1]; + String objectPath = args[2]; + String savePath = args[3]; + String toBucket = args[4]; logger.info(DASHES); logger.info("Welcome to the Amazon Simple Storage Service (S3) example scenario."); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadFile.java b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadFile.java index 74dbe765d9c..472686ae3f1 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadFile.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadFile.java @@ -35,7 +35,7 @@ public class DownloadFile { private static final Logger logger = LoggerFactory.getLogger(UploadFile.class); - public final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); + public final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); // Change bucket name. public final String key = UUID.randomUUID().toString(); private final String downloadedFileName = "downloaded.pdf"; public String downloadedFileWithPath; diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadToDirectory.java b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadToDirectory.java index 4d852e99fe8..a8df923fae1 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadToDirectory.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/DownloadToDirectory.java @@ -35,7 +35,7 @@ public class DownloadToDirectory { private static final Logger logger = LoggerFactory.getLogger(DownloadToDirectory.class); - public final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); + public final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); // Change bucket name. public URI destinationPathURI; private final Set downloadedFileNameSet = new HashSet<>(); private final String destinationDirName = "downloadDirectory"; diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/ObjectCopy.java b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/ObjectCopy.java index 411b90ee117..28d044bb846 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/ObjectCopy.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/ObjectCopy.java @@ -27,7 +27,7 @@ public class ObjectCopy { private static final Logger logger = LoggerFactory.getLogger(ObjectCopy.class); - public final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); + public final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); // Change bucket name. public final String key = UUID.randomUUID().toString(); public final String destinationBucket = "amzn-s3-demo-bucket-" + UUID.randomUUID(); public final String destinationKey = UUID.randomUUID().toString(); diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadADirectory.java b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadADirectory.java index 7830d785806..152fd33032a 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadADirectory.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadADirectory.java @@ -30,7 +30,7 @@ public class UploadADirectory { private static final Logger logger = LoggerFactory.getLogger(UploadADirectory.class); - public final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); + public final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); // Change bucket name. public URI sourceDirectory; public UploadADirectory() { From ed08077ae4baf1104f59a56451b352c33b7ddf5f Mon Sep 17 00:00:00 2001 From: scmacdon Date: Mon, 7 Oct 2024 11:53:50 -0400 Subject: [PATCH 08/10] updated S3 Java file --- .doc_gen/metadata/s3-control_metadata.yaml | 6 ++-- .../com/example/s3/batch/HelloS3Batch.java | 11 ++++--- .../com/example/s3/scenario/S3Actions.java | 30 +++++++++++-------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/.doc_gen/metadata/s3-control_metadata.yaml b/.doc_gen/metadata/s3-control_metadata.yaml index 4d078665fbd..ef7c53f00de 100644 --- a/.doc_gen/metadata/s3-control_metadata.yaml +++ b/.doc_gen/metadata/s3-control_metadata.yaml @@ -24,13 +24,13 @@ s3-control_CreateJob: github: javav2/example_code/s3 sdkguide: excerpts: - - description: create an asynchronous S3 job + - description: Create an asynchronous S3 job. snippet_tags: - s3control.java2.create_job.async.main - - description: create a compliance retention job + - description: Create a compliance retention job. snippet_tags: - s3control.java2.create_job.compliance.main - - description: creates a legal hold off job + - description: Create a legal hold off job. snippet_tags: - s3control.java2.create_job.compliance.main services: diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/batch/HelloS3Batch.java b/javav2/example_code/s3/src/main/java/com/example/s3/batch/HelloS3Batch.java index 0143fe58ea0..74fe36fbb0d 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/batch/HelloS3Batch.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/batch/HelloS3Batch.java @@ -5,6 +5,7 @@ // snippet-start:[s3control.java2.list_jobs.main] import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider; import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; +import software.amazon.awssdk.core.retry.RetryMode; import software.amazon.awssdk.core.retry.RetryPolicy; import software.amazon.awssdk.http.async.SdkAsyncHttpClient; import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient; @@ -30,9 +31,10 @@ */ public class HelloS3Batch { private static S3ControlAsyncClient asyncClient; - public static void main(String []args ) { + + public static void main(String[] args) { S3BatchActions actions = new S3BatchActions(); - String accountId= actions.getAccountId(); + String accountId = actions.getAccountId(); try { listBatchJobsAsync(accountId) .exceptionally(ex -> { @@ -77,9 +79,7 @@ private static S3ControlAsyncClient getAsyncClient() { ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder() .apiCallTimeout(Duration.ofMinutes(2)) .apiCallAttemptTimeout(Duration.ofSeconds(90)) - .retryPolicy(RetryPolicy.builder() - .numRetries(3) - .build()) + .retryStrategy(RetryMode.STANDARD) .build(); asyncClient = S3ControlAsyncClient.builder() @@ -92,7 +92,6 @@ private static S3ControlAsyncClient getAsyncClient() { return asyncClient; } - /** * Asynchronously lists batch jobs that have completed for the specified account. * diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Actions.java b/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Actions.java index bbe9d9a8484..825ec5bbeca 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Actions.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Actions.java @@ -38,6 +38,7 @@ import software.amazon.awssdk.services.s3.model.UploadPartRequest; import software.amazon.awssdk.services.s3.paginators.ListObjectsV2Publisher; import software.amazon.awssdk.services.s3.waiters.S3AsyncWaiter; + import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @@ -54,6 +55,7 @@ public class S3Actions { private static final Logger logger = LoggerFactory.getLogger(S3Actions.class); private static S3AsyncClient s3AsyncClient; + public static S3AsyncClient getAsyncClient() { if (s3AsyncClient == null) { /* @@ -86,6 +88,7 @@ public static S3AsyncClient getAsyncClient() { } // snippet-start:[s3.java2.create_bucket_waiters.main] + /** * Creates an S3 bucket asynchronously. * @@ -121,11 +124,12 @@ public CompletableFuture createBucketAsync(String bucketName) { // snippet-end:[s3.java2.create_bucket_waiters.main] // snippet-start:[s3.java2.s3_object_upload.main] + /** * Uploads a local file to an AWS S3 bucket asynchronously. * * @param bucketName the name of the S3 bucket to upload the file to - * @param key the key (object name) to use for the uploaded file + * @param key the key (object name) to use for the uploaded file * @param objectPath the local file path of the file to be uploaded * @return a {@link CompletableFuture} that completes with the {@link PutObjectResponse} when the upload is successful, or throws a {@link RuntimeException} if the upload fails */ @@ -138,19 +142,20 @@ public CompletableFuture uploadLocalFileAsync(String bucketNa CompletableFuture response = getAsyncClient().putObject(objectRequest, AsyncRequestBody.fromFile(Paths.get(objectPath))); return response.whenComplete((resp, ex) -> { if (ex != null) { - throw new RuntimeException("Failed to upload file", ex); + throw new RuntimeException("Failed to upload file", ex); } }); } // snippet-end:[s3.java2.s3_object_upload.main] // snippet-start:[s3.java2.getobjectdata.main] + /** * Asynchronously retrieves the bytes of an object from an Amazon S3 bucket and writes them to a local file. * * @param bucketName the name of the S3 bucket containing the object - * @param keyName the key (or name) of the S3 object to retrieve - * @param path the local file path where the object's bytes will be written + * @param keyName the key (or name) of the S3 object to retrieve + * @param path the local file path where the object's bytes will be written * @return a {@link CompletableFuture} that completes when the object bytes have been written to the local file */ public CompletableFuture getObjectBytesAsync(String bucketName, String keyName, String path) { @@ -178,6 +183,7 @@ public CompletableFuture getObjectBytesAsync(String bucketName, String key // snippet-end:[s3.java2.getobjectdata.main] // snippet-start:[s3.java2.list_objects.main] + /** * Asynchronously lists all objects in the specified S3 bucket. * @@ -204,12 +210,13 @@ public CompletableFuture listAllObjectsAsync(String bucketName) { // snippet-end:[s3.java2.list_objects.main] // snippet-start:[s3.java2.copy_object.main] + /** * Asynchronously copies an object from one S3 bucket to another. * * @param fromBucket the name of the source S3 bucket - * @param objectKey the key (name) of the object to be copied - * @param toBucket the name of the destination S3 bucket + * @param objectKey the key (name) of the object to be copied + * @param toBucket the name of the destination S3 bucket * @return a {@link CompletableFuture} that completes with the copy result as a {@link String} * @throws RuntimeException if the URL could not be encoded or an S3 exception occurred during the copy */ @@ -239,7 +246,7 @@ public CompletableFuture copyBucketObjectAsync(String fromBucket, String * Performs a multipart upload to an Amazon S3 bucket. * * @param bucketName the name of the S3 bucket to upload the file to - * @param key the key (name) of the file to be uploaded + * @param key the key (name) of the file to be uploaded * @return a {@link CompletableFuture} that completes when the multipart upload is successful */ public CompletableFuture multipartUpload(String bucketName, String key) { @@ -316,11 +323,12 @@ public CompletableFuture multipartUpload(String bucketName, String key) { } // snippet-start:[s3.java2.delete_objects.main] + /** * Deletes an object from an S3 bucket asynchronously. * * @param bucketName the name of the S3 bucket - * @param key the key (file name) of the object to be deleted + * @param key the key (file name) of the object to be deleted * @return a {@link CompletableFuture} that completes when the object has been deleted */ public CompletableFuture deleteObjectFromBucketAsync(String bucketName, String key) { @@ -343,12 +351,13 @@ public CompletableFuture deleteObjectFromBucketAsync(String bucketName, St // snippet-end:[s3.java2.delete_objects.main] // snippet-start:[s3.java2.bucket_deletion.main] + /** * Deletes an S3 bucket asynchronously. * * @param bucket the name of the bucket to be deleted * @return a {@link CompletableFuture} that completes when the bucket deletion is successful, or throws a {@link RuntimeException} - * if an error occurs during the deletion process + * if an error occurs during the deletion process */ public CompletableFuture deleteBucketAsync(String bucket) { DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder() @@ -369,7 +378,6 @@ public CompletableFuture deleteBucketAsync(String bucket) { // snippet-start:[s3.java2.multi_copy.main] public CompletableFuture performMultiCopy(String toBucket, String bucketName, String key) { - // Step 1: Initiate multipart upload. CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest.builder() .bucket(toBucket) .key(key) @@ -380,7 +388,6 @@ public CompletableFuture performMultiCopy(String toBucket, String bucket String uploadId = createMultipartUploadResponse.uploadId(); System.out.println("Upload ID: " + uploadId); - // Step 2: Set the parameters for the upload part copy request. UploadPartCopyRequest uploadPartCopyRequest = UploadPartCopyRequest.builder() .sourceBucket(bucketName) .destinationBucket(toBucket) @@ -391,7 +398,6 @@ public CompletableFuture performMultiCopy(String toBucket, String bucket .copySourceRange("bytes=0-1023") // Adjust range as needed .build(); - // Step 3: Perform the upload part copy operation asynchronously. return getAsyncClient().uploadPartCopy(uploadPartCopyRequest); }) .thenCompose(uploadPartCopyFuture -> uploadPartCopyFuture) From 2d33ede9b086c869fe35c2b6899dced528217b49 Mon Sep 17 00:00:00 2001 From: scmacdon Date: Mon, 7 Oct 2024 15:15:29 -0400 Subject: [PATCH 09/10] updated Java S3 example files --- .../com/example/s3/batch/CreateRetentionJob.java | 12 ------------ .../com/example/s3/transfermanager/UploadFile.java | 2 +- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateRetentionJob.java b/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateRetentionJob.java index 417c0b805da..cad42c7fdeb 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateRetentionJob.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateRetentionJob.java @@ -158,11 +158,6 @@ public static String createComplianceRetentionJob(final S3ControlClient s3Contro final String manifestObjectVersionId = "your-object-version-Id"; Instant jan2025 = Instant.parse("2025-01-01T00:00:00Z"); - S3Retention retention = S3Retention.builder() - .mode(S3ObjectLockRetentionMode.COMPLIANCE) - .retainUntilDate(jan2025) - .build(); - JobOperation jobOperation = JobOperation.builder() .s3PutObjectRetention(S3SetObjectRetentionOperation.builder() .retention(S3Retention.builder() @@ -232,13 +227,6 @@ public static String createComplianceRetentionJob(final S3ControlClient s3Contro public static String createLegalHoldOffJob(final S3ControlClient s3ControlClient, String roleArn, String bucketName, String accountId) { final String manifestObjectArn = "arn:aws:s3:::amzn-s3-demo-manifest-bucket/compliance-objects-manifest.csv"; final String manifestObjectVersionId = "your-object-version-Id"; - - Instant jan2025 = Instant.parse("2025-01-01T00:00:00Z"); - S3Retention retention = S3Retention.builder() - .mode(S3ObjectLockRetentionMode.COMPLIANCE) - .retainUntilDate(jan2025) - .build(); - JobOperation jobOperation = JobOperation.builder() .s3PutObjectLegalHold(S3SetObjectLegalHoldOperation.builder() .legalHold(S3ObjectLockLegalHold.builder() diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadFile.java b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadFile.java index 2145bf85429..b2565113eba 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadFile.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/transfermanager/UploadFile.java @@ -30,7 +30,7 @@ public class UploadFile { private static final Logger logger = LoggerFactory.getLogger(UploadFile.class); - public final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); + public final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); // Change bucket name. public final String key = UUID.randomUUID().toString(); public URI filePathURI; From 4aab840e3122ec8c50895e8a7d73187ed482c40b Mon Sep 17 00:00:00 2001 From: scmacdon Date: Tue, 8 Oct 2024 11:07:51 -0400 Subject: [PATCH 10/10] updated S3 Readme using Write me --- javav2/example_code/s3/README.md | 14 +++++++------- .../com/example/s3/batch/CreateRetentionJob.java | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/javav2/example_code/s3/README.md b/javav2/example_code/s3/README.md index 948b6e3bc02..c6700d14e52 100644 --- a/javav2/example_code/s3/README.md +++ b/javav2/example_code/s3/README.md @@ -45,29 +45,29 @@ Code examples that show you how to perform the essential operations within a ser Code excerpts that show you how to call individual service functions. -- [CopyObject](src/main/java/com/example/s3/scenario/S3Actions.java#L203) -- [CreateBucket](src/main/java/com/example/s3/scenario/S3Actions.java#L85) -- [DeleteBucket](src/main/java/com/example/s3/scenario/S3Actions.java#L342) +- [CopyObject](src/main/java/com/example/s3/scenario/S3Actions.java#L212) +- [CreateBucket](src/main/java/com/example/s3/scenario/S3Actions.java#L90) +- [DeleteBucket](src/main/java/com/example/s3/scenario/S3Actions.java#L353) - [DeleteBucketPolicy](src/main/java/com/example/s3/DeleteBucketPolicy.java#L6) - [DeleteBucketWebsite](src/main/java/com/example/s3/DeleteWebsiteConfiguration.java#L6) -- [DeleteObject](src/main/java/com/example/s3/scenario/S3Actions.java#L315) +- [DeleteObject](src/main/java/com/example/s3/scenario/S3Actions.java#L325) - [DeleteObjects](src/main/java/com/example/s3/DeleteMultiObjects.java#L6) - [GetBucketAcl](src/main/java/com/example/s3/GetAcl.java#L6) - [GetBucketPolicy](src/main/java/com/example/s3/GetBucketPolicy.java#L6) -- [GetObject](src/main/java/com/example/s3/scenario/S3Actions.java#L144) +- [GetObject](src/main/java/com/example/s3/scenario/S3Actions.java#L151) - [GetObjectLegalHold](src/main/java/com/example/s3/lockscenario/S3LockActions.java#L94) - [GetObjectLockConfiguration](src/main/java/com/example/s3/lockscenario/S3LockActions.java#L345) - [GetObjectRetention](src/main/java/com/example/s3/lockscenario/S3LockActions.java#L309) - [HeadObject](src/main/java/com/example/s3/GetObjectContentType.java#L6) - [ListBuckets](src/main/java/com/example/s3/ListBuckets.java#L6) - [ListMultipartUploads](src/main/java/com/example/s3/ListMultipartUploads.java#L6) -- [ListObjectsV2](src/main/java/com/example/s3/scenario/S3Actions.java#L177) +- [ListObjectsV2](src/main/java/com/example/s3/scenario/S3Actions.java#L185) - [PutBucketAcl](src/main/java/com/example/s3/SetAcl.java#L6) - [PutBucketCors](src/main/java/com/example/s3/S3Cors.java#L6) - [PutBucketLifecycleConfiguration](src/main/java/com/example/s3/LifecycleConfiguration.java#L6) - [PutBucketPolicy](src/main/java/com/example/s3/SetBucketPolicy.java#L6) - [PutBucketWebsite](src/main/java/com/example/s3/SetWebsiteConfiguration.java#L6) -- [PutObject](src/main/java/com/example/s3/scenario/S3Actions.java#L120) +- [PutObject](src/main/java/com/example/s3/scenario/S3Actions.java#L126) - [PutObjectLegalHold](src/main/java/com/example/s3/lockscenario/S3LockActions.java#L255) - [PutObjectLockConfiguration](src/main/java/com/example/s3/lockscenario/S3LockActions.java#L206) - [PutObjectRetention](src/main/java/com/example/s3/lockscenario/S3LockActions.java#L61) diff --git a/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateRetentionJob.java b/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateRetentionJob.java index cad42c7fdeb..e8ec195b22d 100644 --- a/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateRetentionJob.java +++ b/javav2/example_code/s3/src/main/java/com/example/s3/batch/CreateRetentionJob.java @@ -52,7 +52,7 @@ public static void main(String[] args) throws IOException, ParseException { S3Client s3 = S3Client.create(); S3ControlClient s3ControlClient = S3ControlClient.create(); - // Use CloudFormation to stand up the resource required for this scenario. + // Use CloudFormation to stand up the required resource. System.out.println("Use CloudFormation to stand up the resource required for this scenario."); CloudFormationHelper.deployCloudFormationStack(STACK_NAME);