AWS code-library documentation change
Summary
Refactored Java code examples for S3 multipart upload abort operations. Changed from using static fields for bucket and key to method parameters, added try-catch-finally for resource cleanup, and updated bucket names to fixed strings with replacement instructions.
Security assessment
The changes focus on code structure and clarity improvements without addressing any specific security vulnerabilities. Modifications include parameterizing methods for better reusability and adding error handling, but there's no evidence of patching security flaws or documenting security features. The abort operations themselves are standard S3 housekeeping functions unrelated to security weaknesses.
Diff
diff --git a/code-library/latest/ug/java_2_s3_code_examples.md b/code-library/latest/ug/java_2_s3_code_examples.md index 211f44e71..96a3ccf93 100644 --- a//code-library/latest/ug/java_2_s3_code_examples.md +++ b//code-library/latest/ug/java_2_s3_code_examples.md @@ -883,2 +883 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - static final String bucketName = "amzn-s3-demo-bucket" + UUID.randomUUID(); // Change bucket name. - static final String key = UUID.randomUUID().toString(); + static final S3Client s3Client = S3Client.create(); @@ -887 +885,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - static final S3Client s3Client = S3Client.create(); @@ -889 +887 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - private static String accountId = getAccountId(); + private static final String accountId = getAccountId(); @@ -892,5 +890,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - doAbortIncompleteMultipartUploadsFromList(); - doAbortMultipartUploadUsingUploadId(); - doAbortIncompleteMultipartUploadsOlderThan(); - doAbortMultipartUploadsUsingLifecycleConfig(); - } + String bucketName = "amzn-s3-demo-bucket"; // Replace with your bucket name. + String key = UUID.randomUUID().toString(); @@ -898,6 +893,11 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - // A wrapper method that sets up the multipart upload environment for abortIncompleteMultipartUploadsFromList(). - public static void doAbortIncompleteMultipartUploadsFromList() { - createBucket(); - initiateAndInterruptMultiPartUpload("uploadThread"); - abortIncompleteMultipartUploadsFromList(); - deleteResources(); + createBucket(bucketName); + try { + initiateAndInterruptMultiPartUpload(bucketName, key, "uploadThread"); + abortIncompleteMultipartUploadsFromList(bucketName); + abortMultipartUploadUsingUploadId(bucketName, key); + abortMultipartUploadsUsingLifecycleConfig(bucketName); + } catch (S3Exception e) { + logger.error(e.getMessage()); + } finally { + deleteResources(bucketName, key); + } @@ -908,3 +908,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - * <p> - * This method retrieves a list of all incomplete multipart uploads in the specified S3 bucket, - * and then aborts each of those uploads. + * + * @param bucketName the name of the S3 bucket @@ -912 +911 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - public static void abortIncompleteMultipartUploadsFromList() { + public static void abortIncompleteMultipartUploadsFromList(String bucketName) { @@ -936,9 +935,7 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - // A wrapper method that sets up the multipart upload environment for abortIncompleteMultipartUploadsOlderThan(). - static void doAbortIncompleteMultipartUploadsOlderThan() { - createBucket(); - Instant secondUploadInstant = initiateAndInterruptTwoUploads(); - abortIncompleteMultipartUploadsOlderThan(secondUploadInstant); - deleteResources(); - } - - static void abortIncompleteMultipartUploadsOlderThan(Instant pointInTime) { + /** + * Aborts incomplete multipart uploads older than the specified point in time. + * + * @param bucketName the name of the S3 bucket + * @param pointInTime the cutoff time; uploads initiated before this are aborted + */ + static void abortIncompleteMultipartUploadsOlderThan(String bucketName, Instant pointInTime) { @@ -971,14 +968,8 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - // A wrapper method that sets up the multipart upload environment for abortMultipartUploadUsingUploadId(). - static void doAbortMultipartUploadUsingUploadId() { - createBucket(); - try { - abortMultipartUploadUsingUploadId(); - } catch (S3Exception e) { - logger.error(e.getMessage()); - } finally { - deleteResources(); - } - } - - static void abortMultipartUploadUsingUploadId() { - String uploadId = startUploadReturningUploadId(); + /** + * Aborts a multipart upload using the upload ID. + * + * @param bucketName the name of the S3 bucket + * @param key the object key + */ + static void abortMultipartUploadUsingUploadId(String bucketName, String key) { + String uploadId = startUploadReturningUploadId(bucketName, key); @@ -995,13 +986,6 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - // A wrapper method that sets up the multipart upload environment for abortMultipartUploadsUsingLifecycleConfig(). - static void doAbortMultipartUploadsUsingLifecycleConfig() { - createBucket(); - try { - abortMultipartUploadsUsingLifecycleConfig(); - } catch (S3Exception e) { - logger.error(e.getMessage()); - } finally { - deleteResources(); - } - } - - static void abortMultipartUploadsUsingLifecycleConfig() { + /** + * Configures a lifecycle rule to abort incomplete multipart uploads after 7 days. + * + * @param bucketName the name of the S3 bucket + */ + static void abortMultipartUploadsUsingLifecycleConfig(String bucketName) { @@ -1015 +998,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - // If the action is successful, the service sends back an HTTP 200 response with an empty HTTP body. @@ -1031 +1014 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - static void initiateAndInterruptMultiPartUpload(String threadName) { + static void initiateAndInterruptMultiPartUpload(String bucketName, String key, String threadName) { @@ -1034 +1017 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - AbortMultipartUploadExamples.doMultipartUpload(); + doMultipartUpload(bucketName, key); @@ -1042 +1025 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - Thread.sleep(Duration.ofSeconds(1).toMillis()); // Give the multipart upload time to register. + Thread.sleep(Duration.ofSeconds(1).toMillis()); @@ -1049,3 +1032,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - static Instant initiateAndInterruptTwoUploads() { - Instant firstUploadInstant = Instant.now(); - initiateAndInterruptMultiPartUpload("uploadThread1"); + static Instant initiateAndInterruptTwoUploads(String bucketName, String key) { + initiateAndInterruptMultiPartUpload(bucketName, key, "uploadThread1"); @@ -1058 +1040 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - initiateAndInterruptMultiPartUpload("uploadThread2"); + initiateAndInterruptMultiPartUpload(bucketName, key, "uploadThread2"); @@ -1062,4 +1044,4 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - static void doMultipartUpload() { - String uploadId = step1CreateMultipartUpload(); - List<CompletedPart> completedParts = step2UploadParts(uploadId); - step3CompleteMultipartUpload(uploadId, completedParts); + static void doMultipartUpload(String bucketName, String key) { + String uploadId = step1CreateMultipartUpload(bucketName, key); + List<CompletedPart> completedParts = step2UploadParts(bucketName, key, uploadId); + step3CompleteMultipartUpload(bucketName, key, uploadId, completedParts); @@ -1068 +1050 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - static String step1CreateMultipartUpload() { + static String step1CreateMultipartUpload(String bucketName, String key) { @@ -1075 +1057 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - static List<CompletedPart> step2UploadParts(String uploadId) { + static List<CompletedPart> step2UploadParts(String bucketName, String key, String uploadId) { @@ -1087 +1069 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - bb.flip(); // Swap position and limit before reading from the buffer. + bb.flip(); @@ -1117 +1099 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - static void step3CompleteMultipartUpload(String uploadId, List<CompletedPart> completedParts) { + static void step3CompleteMultipartUpload(String bucketName, String key, String uploadId, List<CompletedPart> completedParts) { @@ -1125,3 +1107,3 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - static String startUploadReturningUploadId() { - String uploadId = step1CreateMultipartUpload(); - doMultipartUploadWithUploadId(uploadId); + static String startUploadReturningUploadId(String bucketName, String key) { + String uploadId = step1CreateMultipartUpload(bucketName, key); + doMultipartUploadWithUploadId(bucketName, key, uploadId); @@ -1132 +1113 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - static void doMultipartUploadWithUploadId(String uploadId) { + static void doMultipartUploadWithUploadId(String bucketName, String key, String uploadId) { @@ -1135,2 +1116,2 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - List<CompletedPart> completedParts = step2UploadParts(uploadId); - step3CompleteMultipartUpload(uploadId, completedParts); + List<CompletedPart> completedParts = step2UploadParts(bucketName, key, uploadId); + step3CompleteMultipartUpload(bucketName, key, uploadId, completedParts); @@ -1153 +1134 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - static void createBucket() { + static void createBucket(String bucketName) { @@ -1162 +1143 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - static void deleteResources() { + static void deleteResources(String bucketName, String key) { @@ -4426 +4407 @@ The following example shows a query using a JSON object. The [complete example]( - static final String BUCKET_NAME = "amzn-s3-demo-bucket-" + UUID.randomUUID(); + static String BUCKET_NAME = "amzn-s3-demo-bucket"; // Replace with your bucket name. @@ -4646 +4627 @@ You can use the following `doesBucketExists` method as a replacement for the the - final String bucketName = "amzn-s3-demo-bucket"; // Change to the bucket name that you want to check. + final String bucketName = "amzn-s3-demo-bucket"; // Replace with the bucket name that you want to check. @@ -5204,3 +5185,2 @@ To stop multipart uploads that are in-progress or incomplete for any reason, you - * <p> - * This method retrieves a list of all incomplete multipart uploads in the specified S3 bucket, - * and then aborts each of those uploads. + * + * @param bucketName the name of the S3 bucket @@ -5208 +5188 @@ To stop multipart uploads that are in-progress or incomplete for any reason, you - public static void abortIncompleteMultipartUploadsFromList() { + public static void abortIncompleteMultipartUploadsFromList(String bucketName) { @@ -5237 +5217,7 @@ To delete incomplete multipart uploads that were initiated before or after a dat - static void abortIncompleteMultipartUploadsOlderThan(Instant pointInTime) { + /** + * Aborts incomplete multipart uploads older than the specified point in time. + * + * @param bucketName the name of the S3 bucket + * @param pointInTime the cutoff time; uploads initiated before this are aborted + */ + static void abortIncompleteMultipartUploadsOlderThan(String bucketName, Instant pointInTime) { @@ -5269,2 +5255,8 @@ If you have access to the upload ID after you begin a multipart upload, you can - static void abortMultipartUploadUsingUploadId() { - String uploadId = startUploadReturningUploadId(); + /** + * Aborts a multipart upload using the upload ID. + *