有时无法使用适用于Java v2.0的AWS开发工具包获取s3对象

时间:2019-06-01 12:02:33

标签: java amazon-web-services amazon-s3

我在一些lambda函数上将适用于Java的AWS开发工具包从v1.11迁移到v2.0,并出现错误。 其中一项功能由S3 PUT操作触发。 当我通过同时上传多个文件来运行此功能时,一些文件成功,但另一些文件则无法将它们下载为字节数组。

这是我的代码。

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();
}

我得到了这个错误。

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可以完美运行,但v2.0有时会失败。有什么建议吗?

0 个答案:

没有答案