通过 AWS Java SDK 访问 S3 时出现问题

Problems in accessing S3 via AWS Java SDK

我正在尝试通过 Java AWS SDK (v2) 获取 S3 对象大小,并通过 HTTP 响应将其发回(这一切都在使用 com.sun.net.httpserver.HttpServer 的 HTTP 服务器中)。但它不起作用并向我显示以下调试消息。

这里出了什么问题?我错过了什么吗?

        AwsBasicCredentials awsCreds = AwsBasicCredentials.create(
                AdapterMain.ACCESS_KEY,
                AdapterMain.SECRET_KEY);

        s3Client = S3Client.builder().region(region)
                                    .endpointOverride(URI.create(AdapterMain.S3server))
                                   .credentialsProvider(StaticCredentialsProvider.create(awsCreds))
                                   .build();
                    //TODO
                    HeadObjectRequest getObjectRequest = HeadObjectRequest.builder()
                            .bucket(bucketName).key("FILES/"+getMD5(id)+"/FILES/"+id+"/"+id+".txt").build();

                    HeadObjectResponse objectHead = s3Client.headObject(getObjectRequest);
                    long size = objectHead.contentLength();
                    System.out.println("=================================="+size);
                    response=size+"";
                    he.sendResponseHeaders(200, response.length());

这是日志:

18:44:14.898 [HTTP-Dispatcher] DEBUG software.amazon.awssdk.core.interceptor.ExecutionInterceptorChain - Creating an interceptor chain that will apply interceptors in the following order: [software.amazon.awssdk.core.internal.interceptor.HttpChecksumRequiredInterceptor@62ec69e1, software.amazon.awssdk.awscore.interceptor.HelpfulUnknownHostExceptionInterceptor@1d17dde7, software.amazon.awssdk.services.s3.internal.handlers.EnableChunkedEncodingInterceptor@339b31af, software.amazon.awssdk.services.s3.internal.handlers.DisableDoubleUrlEncodingInterceptor@3d96c2f6, software.amazon.awssdk.services.s3.internal.handlers.EnableTrailingChecksumInterceptor@3cb417c4, software.amazon.awssdk.services.s3.internal.handlers.CreateMultipartUploadRequestInterceptor@1f6f0d50, software.amazon.awssdk.services.s3.internal.handlers.GetObjectInterceptor@7513515b, software.amazon.awssdk.services.s3.internal.handlers.AsyncChecksumValidationInterceptor@7e99ac7d, software.amazon.awssdk.services.s3.internal.handlers.EndpointAddressInterceptor@620f9e5d, software.amazon.awssdk.services.s3.internal.handlers.ExceptionTranslationInterceptor@56d0ac1, software.amazon.awssdk.services.s3.internal.handlers.GetBucketPolicyInterceptor@6a1b0abe, software.amazon.awssdk.services.s3.internal.handlers.PutObjectInterceptor@5952b9c4, software.amazon.awssdk.services.s3.internal.handlers.SyncChecksumValidationInterceptor@473129c5, software.amazon.awssdk.services.s3.internal.handlers.DecodeUrlEncodedResponseInterceptor@2ae718e0, software.amazon.awssdk.services.s3.internal.handlers.CreateBucketInterceptor@181bb9f8]
18:44:14.939 [HTTP-Dispatcher] DEBUG software.amazon.awssdk.core.interceptor.ExecutionInterceptorChain - Interceptor 'software.amazon.awssdk.services.s3.internal.handlers.EndpointAddressInterceptor@620f9e5d' modified the message with its modifyHttpRequest method.
18:44:14.967 [HTTP-Dispatcher] DEBUG software.amazon.awssdk.request - Sending Request: DefaultSdkHttpFullRequest(httpMethod=HEAD, protocol=https, host=file-store.s3-server.dcstore.company.net, port=443, encodedPath=/FILES/f13e/FILES/id_1234/id_1234.txt, headers=[amz-sdk-invocation-id, User-Agent], queryParameters=[])
18:44:14.978 [HTTP-Dispatcher] DEBUG software.amazon.awssdk.auth.signer.Aws4Signer - AWS4 String to sign: AWS4-HMAC-SHA256
20210305T014414Z
20210305/us-east-1/s3/aws4_request
9bfed5fd14903f65ac34647985e2c8a4bbe0fbf311982cfbeb2e44b2b58a2390
18:44:14.991 [HTTP-Dispatcher] WARN software.amazon.awssdk.http.apache.internal.utils.ApacheUtils - NoSuchMethodException was thrown when disabling normalizeUri. This indicates you are using an old version (< 4.5.8) of Apache http client. It is recommended to use http client version >= 4.5.9 to avoid the breaking change introduced in apache client 4.5.7 and the latency in exception handling. See https://github.com/aws/aws-sdk-java/issues/1919 for more information
18:44:15.098 [HTTP-Dispatcher] DEBUG software.amazon.awssdk.http.apache.internal.conn.SdkTlsSocketFactory - socket.getSupportedProtocols(): [TLSv1.3, TLSv1.2, TLSv1.1, TLSv1, SSLv3, SSLv2Hello], socket.getEnabledProtocols(): [TLSv1.3, TLSv1.2, TLSv1.1, TLSv1]
18:44:15.099 [HTTP-Dispatcher] DEBUG software.amazon.awssdk.http.apache.internal.conn.SdkTlsSocketFactory - TLS protocol enabled for SSL handshake: [TLSv1.2, TLSv1.1, TLSv1, TLSv1.3]
18:44:15.506 [HTTP-Dispatcher] DEBUG software.amazon.awssdk.http.apache.internal.net.SdkSslSocket - created: file-store.s3-server.dcstore.company.net/10.111.111.20:443

warning 消息有点误导,技术上应该是 error 在这种特殊情况下,因为这是 httpclinet 库中的重大更改,可能导致意外行为该程序。这种依赖本身是来自 aws-java-sdk 的传递依赖。因此,要修复它,只需遵循警告消息中提供的建议,并在项目 pom 文件中明确定义所需的 httpclinet 版本:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.9</version>
</dependency>