I migrated AWS SDK for Java from v1.11 to v2.0 on some lambda functions and got an error. One of the functions is triggered by S3 PUT operation. When I run this function by uploading multiple files at the same time, some files succeed but others fail to download them as a byte array.

Here is my code.

RetryPolicy retryPolicy = RetryPolicy.builder()
        .numRetries(3)
        .build();

ClientOverrideConfiguration overrideConfiguration = ClientOverrideConfiguration.builder()
        .retryPolicy(retryPolicy)
        .build();

SdkHttpClient.Builder<ApacheHttpClient.Builder> httpClientBuilder = ApacheHttpClient.builder()
        .connectionTimeout(Duration.ofMillis(30000L))
        .socketTimeout(Duration.ofMillis(30000L))
        .maxConnections(500);

S3ClientBuilder clientBuilder = S3Client.builder()
        .region(Region.of(region))
        .overrideConfiguration(overrideConfiguration)
        .httpClientBuilder(httpClientBuilder);

try (S3Client client = clientBuilder.build()) {
    GetObjectRequest request = GetObjectRequest.builder()
            .bucket(bucketName)
            .key(filePath)
            .build();

    return client.getObject(request, ResponseTransformer.toBytes()).asByteArray();
}

And I got this error.

Unable to execute HTTP request: Connect to somebucket.s3.ap-northeast-1.amazonaws.com:443 [somebucket.s3.ap-northeast-1.amazonaws.com/99.999.99.999] failed: connect timed out
software.amazon.awssdk.core.exception.SdkClientException$BuilderImpl.build(SdkClientException.java:97)
software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage$RetryExecutor.handleThrownException(RetryableStage.java:140)
software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage$RetryExecutor.execute(RetryableStage.java:96)
software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage.execute(RetryableStage.java:64)
software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage.execute(RetryableStage.java:44)
software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:205)
software.amazon.awssdk.core.internal.http.StreamManagingStage.execute(StreamManagingStage.java:51)
software.amazon.awssdk.core.internal.http.StreamManagingStage.execute(StreamManagingStage.java:33)
software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.executeWithTimer(ApiCallTimeoutTrackingStage.java:79)
software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.execute(ApiCallTimeoutTrackingStage.java:60)
software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.execute(ApiCallTimeoutTrackingStage.java:42)
software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:205)
software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:205)
software.amazon.awssdk.core.internal.http.pipeline.stages.ExecutionFailureExceptionReportingStage.execute(ExecutionFailureExceptionReportingStage.java:37)
software.amazon.awssdk.core.internal.http.pipeline.stages.ExecutionFailureExceptionReportingStage.execute(ExecutionFailureExceptionReportingStage.java:26)
software.amazon.awssdk.core.internal.http.AmazonSyncHttpClient$RequestExecutionBuilderImpl.execute(AmazonSyncHttpClient.java:240)
software.amazon.awssdk.core.client.handler.BaseSyncClientHandler.invoke(BaseSyncClientHandler.java:96)
software.amazon.awssdk.core.client.handler.BaseSyncClientHandler.execute(BaseSyncClientHandler.java:120)
software.amazon.awssdk.core.client.handler.BaseSyncClientHandler.execute(BaseSyncClientHandler.java:61)
software.amazon.awssdk.core.client.handler.SdkSyncClientHandler.execute(SdkSyncClientHandler.java:51)
software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler.execute(AwsSyncClientHandler.java:62)
software.amazon.awssdk.services.s3.DefaultS3Client.getObject(DefaultS3Client.java:1597)
common.library.S3Utility.download(S3Utility.java:262)

v1.11 work perfectly but v2.0 sometimes fail. Any suggestions?

0 Answers