AWS code-library documentation change
Summary
Refactored S3 multipart upload abort example to use parameterized methods, improve error handling, and remove static bucket/key generation
Security assessment
The changes are structural improvements to code examples (parameter passing, error handling, resource cleanup) without addressing vulnerabilities or adding security-specific content. No evidence of security patches or vulnerability fixes exists in the diff.
Diff
diff --git a/code-library/latest/ug/s3_example_s3_AbortMultipartUpload_section.md b/code-library/latest/ug/s3_example_s3_AbortMultipartUpload_section.md index 1314a5fff..97c7ddf3d 100644 --- a//code-library/latest/ug/s3_example_s3_AbortMultipartUpload_section.md +++ b//code-library/latest/ug/s3_example_s3_AbortMultipartUpload_section.md @@ -152,2 +152 @@ 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(); @@ -156 +154,0 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - static final S3Client s3Client = S3Client.create(); @@ -158 +156 @@ 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(); @@ -161,5 +159,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(); @@ -167,6 +162,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); + } @@ -177,3 +177,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 @@ -181 +180 @@ 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) { @@ -205,9 +204,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) { @@ -240,14 +237,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); @@ -264,13 +255,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) { @@ -284 +267,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. @@ -300 +283 @@ 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) { @@ -303 +286 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - AbortMultipartUploadExamples.doMultipartUpload(); + doMultipartUpload(bucketName, key); @@ -311 +294 @@ 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()); @@ -318,3 +301,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"); @@ -327 +309 @@ There's more on GitHub. Find the complete example and learn how to set up and ru - initiateAndInterruptMultiPartUpload("uploadThread2"); + initiateAndInterruptMultiPartUpload(bucketName, key, "uploadThread2"); @@ -331,4 +313,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); @@ -337 +319 @@ 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) { @@ -344 +326 @@ 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) { @@ -356 +338 @@ 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(); @@ -386 +368 @@ 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) { @@ -394,3 +376,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); @@ -401 +382 @@ 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) { @@ -404,2 +385,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); @@ -422 +403 @@ 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) { @@ -431 +412 @@ 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) {