Skip to content

Instantly share code, notes, and snippets.

@lobster1234
Created May 19, 2019 20:34
Show Gist options
  • Save lobster1234/893331bae351626d93e6da16268065cb to your computer and use it in GitHub Desktop.
Save lobster1234/893331bae351626d93e6da16268065cb to your computer and use it in GitHub Desktop.
S3Async Driver Quick Test
package org.lobster1234;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import software.amazon.awssdk.services.s3.model.DeleteObjectResponse;
import software.amazon.awssdk.services.s3.model.ListBucketsResponse;
import java.util.concurrent.CompletableFuture;
public class S3AsyncTest {
public static void main(String[] args) throws Exception{
S3AsyncClient client = S3AsyncClient.create();
CompletableFuture<ListBucketsResponse> response = client.listBuckets();
System.out.println(">>>> List buckets request fired async");
response.whenComplete((resp, ex) -> {
System.out.println("<<<< Here are the buckets");
resp.buckets().stream().forEach(x -> System.out.println(" <<<< " + x.name()));
});
//now lets try to create an exception
DeleteObjectRequest request = DeleteObjectRequest.builder().bucket("nonexistent").key("invalid").build();
CompletableFuture<DeleteObjectResponse> deleteResponse = client.deleteObject(request);
System.out.println(">>>> Delete object request fired async");
deleteResponse.whenComplete((resp,ex ) ->{
if(ex!=null){
System.out.println("<<<< Exception when deleting the object " + ex.getMessage());
}else System.out.println("<<<< Successfulluy deleted " + resp);
});
Thread.sleep(20*1000); //Otherwise our code will exit, let the async things finish
}
}
@lobster1234
Copy link
Author

>>>> List buckets request fired async
>>>> Delete object request fired async
<<<< Exception when deleting the object software.amazon.awssdk.services.s3.model.NoSuchBucketException: The specified bucket does not exist (Service: S3, Status Code: 404, Request ID: C482DFA1AB960FD0)
<<<< Here are the buckets
 <<<< aws-java-maven-test-serverlessdeploymentbucket-1jt3eyy0xc3f0
 <<<< cf-templates-olp5joy4pfd0-us-east-1
 <<<< deeplens-sagemaker-0683b5b0-d940-4279-a88f-e2a648479c57
 <<<< lobster1234-cloudtrail-audit-log
 <<<< lobster1234-infrastructure
 <<<< mpandit-versioned
 <<<< myblog-jekyll-output
 <<<< writefromlambda

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment