3838import software .amazon .awssdk .services .s3 .model .UploadPartRequest ;
3939import software .amazon .awssdk .services .s3 .paginators .ListObjectsV2Publisher ;
4040import software .amazon .awssdk .services .s3 .waiters .S3AsyncWaiter ;
41+
4142import java .io .IOException ;
4243import java .io .UnsupportedEncodingException ;
4344import java .net .URLEncoder ;
@@ -54,6 +55,7 @@ public class S3Actions {
5455
5556 private static final Logger logger = LoggerFactory .getLogger (S3Actions .class );
5657 private static S3AsyncClient s3AsyncClient ;
58+
5759 public static S3AsyncClient getAsyncClient () {
5860 if (s3AsyncClient == null ) {
5961 /*
@@ -86,6 +88,7 @@ public static S3AsyncClient getAsyncClient() {
8688 }
8789
8890 // snippet-start:[s3.java2.create_bucket_waiters.main]
91+
8992 /**
9093 * Creates an S3 bucket asynchronously.
9194 *
@@ -121,11 +124,12 @@ public CompletableFuture<Void> createBucketAsync(String bucketName) {
121124 // snippet-end:[s3.java2.create_bucket_waiters.main]
122125
123126 // snippet-start:[s3.java2.s3_object_upload.main]
127+
124128 /**
125129 * Uploads a local file to an AWS S3 bucket asynchronously.
126130 *
127131 * @param bucketName the name of the S3 bucket to upload the file to
128- * @param key the key (object name) to use for the uploaded file
132+ * @param key the key (object name) to use for the uploaded file
129133 * @param objectPath the local file path of the file to be uploaded
130134 * @return a {@link CompletableFuture} that completes with the {@link PutObjectResponse} when the upload is successful, or throws a {@link RuntimeException} if the upload fails
131135 */
@@ -138,19 +142,20 @@ public CompletableFuture<PutObjectResponse> uploadLocalFileAsync(String bucketNa
138142 CompletableFuture <PutObjectResponse > response = getAsyncClient ().putObject (objectRequest , AsyncRequestBody .fromFile (Paths .get (objectPath )));
139143 return response .whenComplete ((resp , ex ) -> {
140144 if (ex != null ) {
141- throw new RuntimeException ("Failed to upload file" , ex );
145+ throw new RuntimeException ("Failed to upload file" , ex );
142146 }
143147 });
144148 }
145149 // snippet-end:[s3.java2.s3_object_upload.main]
146150
147151 // snippet-start:[s3.java2.getobjectdata.main]
152+
148153 /**
149154 * Asynchronously retrieves the bytes of an object from an Amazon S3 bucket and writes them to a local file.
150155 *
151156 * @param bucketName the name of the S3 bucket containing the object
152- * @param keyName the key (or name) of the S3 object to retrieve
153- * @param path the local file path where the object's bytes will be written
157+ * @param keyName the key (or name) of the S3 object to retrieve
158+ * @param path the local file path where the object's bytes will be written
154159 * @return a {@link CompletableFuture} that completes when the object bytes have been written to the local file
155160 */
156161 public CompletableFuture <Void > getObjectBytesAsync (String bucketName , String keyName , String path ) {
@@ -178,6 +183,7 @@ public CompletableFuture<Void> getObjectBytesAsync(String bucketName, String key
178183 // snippet-end:[s3.java2.getobjectdata.main]
179184
180185 // snippet-start:[s3.java2.list_objects.main]
186+
181187 /**
182188 * Asynchronously lists all objects in the specified S3 bucket.
183189 *
@@ -204,12 +210,13 @@ public CompletableFuture<Void> listAllObjectsAsync(String bucketName) {
204210 // snippet-end:[s3.java2.list_objects.main]
205211
206212 // snippet-start:[s3.java2.copy_object.main]
213+
207214 /**
208215 * Asynchronously copies an object from one S3 bucket to another.
209216 *
210217 * @param fromBucket the name of the source S3 bucket
211- * @param objectKey the key (name) of the object to be copied
212- * @param toBucket the name of the destination S3 bucket
218+ * @param objectKey the key (name) of the object to be copied
219+ * @param toBucket the name of the destination S3 bucket
213220 * @return a {@link CompletableFuture} that completes with the copy result as a {@link String}
214221 * @throws RuntimeException if the URL could not be encoded or an S3 exception occurred during the copy
215222 */
@@ -239,7 +246,7 @@ public CompletableFuture<String> copyBucketObjectAsync(String fromBucket, String
239246 * Performs a multipart upload to an Amazon S3 bucket.
240247 *
241248 * @param bucketName the name of the S3 bucket to upload the file to
242- * @param key the key (name) of the file to be uploaded
249+ * @param key the key (name) of the file to be uploaded
243250 * @return a {@link CompletableFuture} that completes when the multipart upload is successful
244251 */
245252 public CompletableFuture <Void > multipartUpload (String bucketName , String key ) {
@@ -316,11 +323,12 @@ public CompletableFuture<Void> multipartUpload(String bucketName, String key) {
316323 }
317324
318325 // snippet-start:[s3.java2.delete_objects.main]
326+
319327 /**
320328 * Deletes an object from an S3 bucket asynchronously.
321329 *
322330 * @param bucketName the name of the S3 bucket
323- * @param key the key (file name) of the object to be deleted
331+ * @param key the key (file name) of the object to be deleted
324332 * @return a {@link CompletableFuture} that completes when the object has been deleted
325333 */
326334 public CompletableFuture <Void > deleteObjectFromBucketAsync (String bucketName , String key ) {
@@ -343,12 +351,13 @@ public CompletableFuture<Void> deleteObjectFromBucketAsync(String bucketName, St
343351 // snippet-end:[s3.java2.delete_objects.main]
344352
345353 // snippet-start:[s3.java2.bucket_deletion.main]
354+
346355 /**
347356 * Deletes an S3 bucket asynchronously.
348357 *
349358 * @param bucket the name of the bucket to be deleted
350359 * @return a {@link CompletableFuture} that completes when the bucket deletion is successful, or throws a {@link RuntimeException}
351- * if an error occurs during the deletion process
360+ * if an error occurs during the deletion process
352361 */
353362 public CompletableFuture <Void > deleteBucketAsync (String bucket ) {
354363 DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest .builder ()
@@ -369,7 +378,6 @@ public CompletableFuture<Void> deleteBucketAsync(String bucket) {
369378
370379 // snippet-start:[s3.java2.multi_copy.main]
371380 public CompletableFuture <String > performMultiCopy (String toBucket , String bucketName , String key ) {
372- // Step 1: Initiate multipart upload.
373381 CreateMultipartUploadRequest createMultipartUploadRequest = CreateMultipartUploadRequest .builder ()
374382 .bucket (toBucket )
375383 .key (key )
@@ -380,7 +388,6 @@ public CompletableFuture<String> performMultiCopy(String toBucket, String bucket
380388 String uploadId = createMultipartUploadResponse .uploadId ();
381389 System .out .println ("Upload ID: " + uploadId );
382390
383- // Step 2: Set the parameters for the upload part copy request.
384391 UploadPartCopyRequest uploadPartCopyRequest = UploadPartCopyRequest .builder ()
385392 .sourceBucket (bucketName )
386393 .destinationBucket (toBucket )
@@ -391,7 +398,6 @@ public CompletableFuture<String> performMultiCopy(String toBucket, String bucket
391398 .copySourceRange ("bytes=0-1023" ) // Adjust range as needed
392399 .build ();
393400
394- // Step 3: Perform the upload part copy operation asynchronously.
395401 return getAsyncClient ().uploadPartCopy (uploadPartCopyRequest );
396402 })
397403 .thenCompose (uploadPartCopyFuture -> uploadPartCopyFuture )
0 commit comments