Skip to content

Commit b692fd7

Browse files
committed
updated S3 Java file
1 parent 19b49c9 commit b692fd7

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

.doc_gen/metadata/s3-control_metadata.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ s3-control_CreateJob:
2424
github: javav2/example_code/s3
2525
sdkguide:
2626
excerpts:
27-
- description: create an asynchronous S3 job
27+
- description: Create an asynchronous S3 job.
2828
snippet_tags:
2929
- s3control.java2.create_job.async.main
30-
- description: create a compliance retention job
30+
- description: Create a compliance retention job.
3131
snippet_tags:
3232
- s3control.java2.create_job.compliance.main
33-
- description: creates a legal hold off job
33+
- description: Create a legal hold off job.
3434
snippet_tags:
3535
- s3control.java2.create_job.compliance.main
3636
services:

javav2/example_code/s3/src/main/java/com/example/s3/batch/HelloS3Batch.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// snippet-start:[s3control.java2.list_jobs.main]
66
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
77
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
8+
import software.amazon.awssdk.core.retry.RetryMode;
89
import software.amazon.awssdk.core.retry.RetryPolicy;
910
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
1011
import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient;
@@ -30,9 +31,10 @@
3031
*/
3132
public class HelloS3Batch {
3233
private static S3ControlAsyncClient asyncClient;
33-
public static void main(String []args ) {
34+
35+
public static void main(String[] args) {
3436
S3BatchActions actions = new S3BatchActions();
35-
String accountId= actions.getAccountId();
37+
String accountId = actions.getAccountId();
3638
try {
3739
listBatchJobsAsync(accountId)
3840
.exceptionally(ex -> {
@@ -77,9 +79,7 @@ private static S3ControlAsyncClient getAsyncClient() {
7779
ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder()
7880
.apiCallTimeout(Duration.ofMinutes(2))
7981
.apiCallAttemptTimeout(Duration.ofSeconds(90))
80-
.retryPolicy(RetryPolicy.builder()
81-
.numRetries(3)
82-
.build())
82+
.retryStrategy(RetryMode.STANDARD)
8383
.build();
8484

8585
asyncClient = S3ControlAsyncClient.builder()
@@ -92,7 +92,6 @@ private static S3ControlAsyncClient getAsyncClient() {
9292
return asyncClient;
9393
}
9494

95-
9695
/**
9796
* Asynchronously lists batch jobs that have completed for the specified account.
9897
*

javav2/example_code/s3/src/main/java/com/example/s3/scenario/S3Actions.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import software.amazon.awssdk.services.s3.model.UploadPartRequest;
3939
import software.amazon.awssdk.services.s3.paginators.ListObjectsV2Publisher;
4040
import software.amazon.awssdk.services.s3.waiters.S3AsyncWaiter;
41+
4142
import java.io.IOException;
4243
import java.io.UnsupportedEncodingException;
4344
import 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

Comments
 (0)