Trying to download files from S3 Bucket using Java SDK, Null pointer Exception on isStandardEndpoint

naderhat :

I'm trying to connect to S3 bucket and download files but the code throws exception:

In My code I have

ProfileCredentialsProvider pcp = new ProfileCredentialsProvider("assumed_role");
        bucketName = "dev-data-extract-service-bucket";
s3Client = AmazonS3ClientBuilder.standard().withCredentials(pcp).withRegion(Regions.US_EAST_1.toString()).build();

I get NullPointerException exception on calling the following line:

fullObject = s3Client.getObject(new GetObjectRequest(bucketName, "TR09_20190205.detail"));

The reason is that host value in Endpoint is null this is from AmazonS3Client class

private boolean isStandardEndpoint(URI endpoint) {
        return endpoint.getHost().endsWith("s3.amazonaws.com");
}

And following is the stack trace

[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)

redbirdo :

I think your issue stems from way you are setting the region. I think the region is resolving to null and that this is causing your endpoint to be null.

Instead of:

s3Client = AmazonS3ClientBuilder.standard().withCredentials(pcp).withRegion(Regions.US_EAST_1.toString()).build();

Try:

s3Client = AmazonS3ClientBuilder.standard().withCredentials(pcp).withRegion(Regions.US_EAST_1).build();

The Regions class does not override the toString() method so it will not return the region name, which is what would be required for your code to work as it is.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=160497&siteId=1