- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试连接到 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中的host值为null这是来自 AmazonS3Client 类
private boolean isStandardEndpoint(URI endpoint) {
return endpoint.getHost().endsWith("s3.amazonaws.com");
}
下面是堆栈跟踪
[main] DEBUG com.amazonaws.AmazonWebServiceClient - Internal logging successfully configured to commons logger: true 15:43:45.183 [main] DEBUG com.amazonaws.metrics.AwsSdkMetrics - Admin mbean registered under com.amazonaws.management:type=AwsSdkMetrics 15:43:45.952 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.EnvironmentVariableCsmConfigurationProvider@169e6180: Unable to load Client Side Monitoring configurations from environment variables! 15:43:45.952 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.SystemPropertyCsmConfigurationProvider@35aea049: Unable to load Client Side Monitoring configurations from system properties variables! 15:43:45.952 [java-sdk-http-connection-reaper] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Closing connections idle longer than 60000 MILLISECONDS 15:43:45.952 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.ProfileCsmConfigurationProvider@611889f4: Unable to load config file Exception in thread "main" java.lang.NullPointerException at 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) at .AWSHelper.main(AWSHelper.java:59)
最佳答案
我认为您的问题源于您设置区域的方式。我认为该区域正在解析为空,这导致您的端点为空。
代替:
s3Client = AmazonS3ClientBuilder.standard().withCredentials(pcp).withRegion(Regions.US_EAST_1.toString()).build();
尝试:
s3Client = AmazonS3ClientBuilder.standard().withCredentials(pcp).withRegion(Regions.US_EAST_1).build();
Regions 类不会重写 toString() 方法,因此它不会返回区域名称,这是您的代码按原样工作所必需的。
关于java - 尝试使用 Java SDK 从 S3 Bucket 下载文件,isStandardEndpoint 出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54583690/
我正在尝试连接到 S3 存储桶并下载文件,但代码抛出异常: 在我的代码中有 ProfileCredentialsProvider pcp = new ProfileCredentialsProvide
我是一名优秀的程序员,十分优秀!