|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package com.example.s3.directorybucket; |
| 4 | + |
| 5 | +// snippet-start:[s3directorybuckets.java2.completedirectorybucketmultipartupload.import] |
| 6 | + |
| 7 | +import com.example.s3.util.S3DirectoryBucketUtils; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | +import software.amazon.awssdk.regions.Region; |
| 11 | +import software.amazon.awssdk.services.s3.S3Client; |
| 12 | +import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest; |
| 13 | +import software.amazon.awssdk.services.s3.model.CompleteMultipartUploadResponse; |
| 14 | +import software.amazon.awssdk.services.s3.model.CompletedMultipartUpload; |
| 15 | +import software.amazon.awssdk.services.s3.model.CompletedPart; |
| 16 | +import software.amazon.awssdk.services.s3.model.S3Exception; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.file.Path; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucket; |
| 23 | +import static com.example.s3.util.S3DirectoryBucketUtils.createDirectoryBucketMultipartUpload; |
| 24 | +import static com.example.s3.util.S3DirectoryBucketUtils.createS3Client; |
| 25 | +import static com.example.s3.util.S3DirectoryBucketUtils.deleteAllObjectsInDirectoryBucket; |
| 26 | +import static com.example.s3.util.S3DirectoryBucketUtils.deleteDirectoryBucket; |
| 27 | +import static com.example.s3.util.S3DirectoryBucketUtils.getFilePath; |
| 28 | +import static com.example.s3.util.S3DirectoryBucketUtils.multipartUploadForDirectoryBucket; |
| 29 | +// snippet-end:[s3directorybuckets.java2.completedirectorybucketmultipartupload.import] |
| 30 | + |
| 31 | +/** |
| 32 | + * Before running this example: |
| 33 | + * <p> |
| 34 | + * The SDK must be able to authenticate AWS requests on your behalf. If you have |
| 35 | + * not configured |
| 36 | + * authentication for SDKs and tools, see |
| 37 | + * https://docs.aws.amazon.com/sdkref/latest/guide/access.html in the AWS SDKs |
| 38 | + * and Tools Reference Guide. |
| 39 | + * <p> |
| 40 | + * You must have a runtime environment configured with the Java SDK. |
| 41 | + * See |
| 42 | + * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html in |
| 43 | + * the Developer Guide if this is not set up. |
| 44 | + * <p> |
| 45 | + * To use S3 directory buckets, configure a gateway VPC endpoint. This is the |
| 46 | + * recommended method to enable directory bucket traffic without |
| 47 | + * requiring an internet gateway or NAT device. For more information on |
| 48 | + * configuring VPC gateway endpoints, visit |
| 49 | + * https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-express-networking.html#s3-express-networking-vpc-gateway. |
| 50 | + * <p> |
| 51 | + * Directory buckets are available in specific AWS Regions and Zones. For |
| 52 | + * details on Regions and Zones supporting directory buckets, see |
| 53 | + * https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-express-networking.html#s3-express-endpoints. |
| 54 | + */ |
| 55 | + |
| 56 | +public class CompleteDirectoryBucketMultipartUpload { |
| 57 | + private static final Logger logger = LoggerFactory.getLogger(CompleteDirectoryBucketMultipartUpload.class); |
| 58 | + |
| 59 | + // snippet-start:[s3directorybuckets.java2.completedirectorybucketmultipartupload.main] |
| 60 | + |
| 61 | + /** |
| 62 | + * This method completes the multipart upload request by collating all the |
| 63 | + * upload parts. |
| 64 | + * |
| 65 | + * @param s3Client The S3 client used to interact with S3 |
| 66 | + * @param bucketName The name of the directory bucket |
| 67 | + * @param objectKey The key (name) of the object to be uploaded |
| 68 | + * @param uploadId The upload ID used to track the multipart upload |
| 69 | + * @param uploadParts The list of completed parts |
| 70 | + * @return True if the multipart upload is successfully completed, false |
| 71 | + * otherwise |
| 72 | + */ |
| 73 | + public static boolean completeDirectoryBucketMultipartUpload(S3Client s3Client, String bucketName, String objectKey, |
| 74 | + String uploadId, List<CompletedPart> uploadParts) { |
| 75 | + try { |
| 76 | + CompletedMultipartUpload completedMultipartUpload = CompletedMultipartUpload.builder() |
| 77 | + .parts(uploadParts) |
| 78 | + .build(); |
| 79 | + CompleteMultipartUploadRequest completeMultipartUploadRequest = CompleteMultipartUploadRequest.builder() |
| 80 | + .bucket(bucketName) |
| 81 | + .key(objectKey) |
| 82 | + .uploadId(uploadId) |
| 83 | + .multipartUpload(completedMultipartUpload) |
| 84 | + .build(); |
| 85 | + |
| 86 | + CompleteMultipartUploadResponse response = s3Client.completeMultipartUpload(completeMultipartUploadRequest); |
| 87 | + logger.info("Multipart upload completed. ETag: {}", response.eTag()); |
| 88 | + return true; |
| 89 | + } catch (S3Exception e) { |
| 90 | + logger.error("Failed to complete multipart upload: {} - Error code: {}", e.awsErrorDetails().errorMessage(), |
| 91 | + e.awsErrorDetails().errorCode(), e); |
| 92 | + return false; |
| 93 | + } |
| 94 | + } |
| 95 | + // snippet-end:[s3directorybuckets.java2.completedirectorybucketmultipartupload.main] |
| 96 | + |
| 97 | + // Main method for testing |
| 98 | + public static void main(String[] args) { |
| 99 | + Region region = Region.US_WEST_2; |
| 100 | + S3Client s3Client = createS3Client(region); |
| 101 | + String zone = "usw2-az1"; |
| 102 | + String bucketName = "test-bucket-" + System.currentTimeMillis() + "--" + zone + "--x-s3"; |
| 103 | + String uploadId; |
| 104 | + String objectKey = "largeObject"; |
| 105 | + Path filePath = getFilePath("directoryBucket/sample-large-object.jpg"); |
| 106 | + |
| 107 | + try { |
| 108 | + // Create the directory bucket |
| 109 | + createDirectoryBucket(s3Client, bucketName, zone); |
| 110 | + // Create a multipart upload |
| 111 | + uploadId = createDirectoryBucketMultipartUpload(s3Client, bucketName, objectKey); |
| 112 | + // Perform multipart upload for the directory bucket |
| 113 | + List<CompletedPart> uploadedParts = multipartUploadForDirectoryBucket(s3Client, bucketName, objectKey, |
| 114 | + uploadId, filePath); |
| 115 | + logger.info("Uploaded parts: {}", uploadedParts); |
| 116 | + // Complete Multipart Uploads |
| 117 | + boolean completed = completeDirectoryBucketMultipartUpload(s3Client, bucketName, objectKey, uploadId, |
| 118 | + uploadedParts); |
| 119 | + if (completed) { |
| 120 | + logger.info("Multipart upload successfully completed for bucket: {}", bucketName); |
| 121 | + } else { |
| 122 | + logger.error("Failed to complete multipart upload for bucket: {}", bucketName); |
| 123 | + } |
| 124 | + } catch (S3Exception e) { |
| 125 | + logger.error("An error occurred during S3 operations: {} - Error code: {}", |
| 126 | + e.awsErrorDetails().errorMessage(), e.awsErrorDetails().errorCode()); |
| 127 | + } catch (IOException e) { |
| 128 | + logger.error("An I/O error occurred: {}", e.getMessage()); |
| 129 | + } finally { |
| 130 | + // Error handling |
| 131 | + try { |
| 132 | + logger.info("Starting cleanup for bucket: {}", bucketName); |
| 133 | + S3DirectoryBucketUtils.abortDirectoryBucketMultipartUploads(s3Client, bucketName); |
| 134 | + deleteAllObjectsInDirectoryBucket(s3Client, bucketName); |
| 135 | + deleteDirectoryBucket(s3Client, bucketName); |
| 136 | + logger.info("Cleanup completed for bucket: {}", bucketName); |
| 137 | + } catch (S3Exception e) { |
| 138 | + logger.error("Error during cleanup: {} - Error code: {}", e.awsErrorDetails().errorMessage(), |
| 139 | + e.awsErrorDetails().errorCode(), e); |
| 140 | + } catch (RuntimeException e) { |
| 141 | + logger.error("Unexpected error during cleanup: {}", e.getMessage(), e); |
| 142 | + } finally { |
| 143 | + // Close the S3 client |
| 144 | + s3Client.close(); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments