我正在尝试连接到S3存储桶并下载文件,但代码抛出异常:
在我的代码中,我有
ProfileCredentialsProvider pcp = new ProfileCredentialsProvider("assumed_role");
bucketName = "dev-data-extract-service-bucket";
s3Client = AmazonS3ClientBuilder.standard().withCredentials(pcp).withRegion(Regions.US_EAST_1.toString()).build();
我在调用以下行时遇到了NullPointerException异常:
fullObject = s3Client.getObject(new GetObjectRequest(bucketName, "TR09_20190205.detail"));
原因是Endpoint中的主机值为null,这是AmazonS3Client类的问题。
private boolean isStandardEndpoint(URI endpoint) {
return endpoint.getHost().endsWith("s3.amazonaws.com");
}
以下是堆栈跟踪
[main] DEBUG com.amazonaws.AmazonWebServiceClient - 成功配置内部日志记录到commons logger: true 15:43:45.183 [main] DEBUG com.amazonaws.metrics.AwsSdkMetrics - 管理MBean已注册为com.amazonaws.management:type=AwsSdkMetrics 15:43:45.952 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - 无法从 com.amazonaws.monitoring.EnvironmentVariableCsmConfigurationProvider@169e6180 加载配置: 无法从环境变量中加载客户端监视配置!15:43:45.952 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - 无法从 com.amazonaws.monitoring.SystemPropertyCsmConfigurationProvider@35aea049 加载配置: 无法从系统属性中加载客户端监视配置!15:43:45.952 [java-sdk-http-connection-reaper] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - 关闭超过60000毫秒空闲的连接15:43:45.952 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - 无法从com.amazonaws.monitoring.ProfileCsmConfigurationProvider@611889f4 加载配置文件 异常线程“main”中的“java.lang.NullPointerException” 在com.amazonaws.services.s3.AmazonS3Client.isStandardEndpoint(AmazonS3Client.java:3772) at com.amazonaws.services.s3.AmazonS3Client.noExplicitRegionProvided(AmazonS3Client.java:3767) at com.amazonaws.services.s3.AmazonS3Client.bucketRegionShouldBeCached(AmazonS3Client.java:4505) at com.amazonaws.services.s3.AmazonS3Client.shouldPerformHeadRequestToFindRegion(AmazonS3Client.java:4501) at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4426) at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4390) at com.amazonaws.services.s3.AmazonS3Client.getAcl(AmazonS3Client.java:3573) at com.amazonaws.services.s3.AmazonS3Client.getBucketAcl(AmazonS3Client.java:1186) at com.amazonaws.services.s3.AmazonS3Client.getBucketAcl(AmazonS3Client.java:1176) at com.amazonaws.services.s3.AmazonS3Client.doesBucketExistV2(AmazonS3Client.java:1312) at .AWSHelper.downloadFromS3Bucket(AWSHelper.java:32) 在 .AWSHelper.main(AWSHelper.java:59)