S3Mock icon indicating copy to clipboard operation
S3Mock copied to clipboard

Setup S3Mock for AWS SDKV2 AsyncS3Client

Open akshaya-kp opened this issue 3 years ago • 2 comments

Hi,

I am trying to use S3Mock as part of my JUnit5 tests. I am trying to figure out how to setup S3Mock for S3AsyncClient.

I saw an issue that mentioned its possible to have S3Mock does work for async APIs. Link attached here. But I am not very clear how to instantiate an S3AsyncClient object.

Any help appreciated. Thanks!

akshaya-kp avatar May 16 '23 00:05 akshaya-kp

@akshayakp97 not sure where the problem is. The async part is in the client API, not the service API. S3Mock will behave exactly the same, just as S3 itself does. It's just you using async methods in your code.

If you want to use the async version of the client in your test (which makes the code much more complicated from my point of view), just create it in your test...

You can look at our own tests on how to use the JUnit5 extension: https://github.com/adobe/S3Mock/blob/main/testsupport/junit5/src/test/java/com/adobe/testing/s3mock/junit5/sdk2/S3MockExtensionProgrammaticTest.java

And just create the client:

S3AsyncClient createAsyncS3ClientV2() {
    return S3AsyncClient.builder()
        .region(Region.of("us-east-1"))
        .credentialsProvider(
            StaticCredentialsProvider.create(AwsBasicCredentials.create("foo", "bar")))
        .endpointOverride(URI.create(S3_MOCK.getServiceEndpoint()))
            .httpClient(NettyNioAsyncHttpClient.builder().buildWithDefaults(AttributeMap.builder()
                .put(TRUST_ALL_CERTIFICATES, Boolean.TRUE)
                .build()))
            .build();
  }

However, I would advise to use Docker in your tests: https://github.com/adobe/S3Mock#s3mock-java

afranken avatar May 16 '23 10:05 afranken

This works! Thank you :)

akshaya-kp avatar May 30 '23 16:05 akshaya-kp