gpt4 book ai didi

java - 尝试使用 Java SDK 从 S3 Bucket 下载文件,isStandardEndpoint 出现空指针异常

转载 作者:行者123 更新时间:2023-11-30 12:07:56 30 4
gpt4 key购买 nike

我正在尝试连接到 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com