无法使用Java从S3下载文件

时间:2018-12-18 07:33:09

标签: java spring-boot amazon-s3

我正在尝试从S3存储桶访问文件,但是在尝试获取文件时却不断收到NullPointerException。

我的application.properties文件:

aWSCredentials.aWSAccessKey=theAccessKey
aWSCredentials.aWSSecretKey=theSecretKey
aWS.bucket=my.bucket.name.com
aWS.file.name=path/to/file/filename.zip

我的配置类:

@SpringBootConfiguration
public class AppConfig extends DelegatingWebMvcConfiguration {

    @Value("${aWSCredentials.aWSAccessKey}")
    private String awsAccessKeyId;

    @Value("${aWSCredentials.aWSSecretKey}")
    private String awsSecretKey;

    @Bean
    public AmazonS3 amazonS3(){
        AWSCredentials awsCredentials = new BasicAWSCredentials(awsAccessKeyId, awsSecretKey);
        return AmazonS3ClientBuilder.standard()
                .withRegion(Regions.US_EAST_1.toString())
                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                .build();
    }
}

使用Amazon bean并尝试获取文件的类:

@Service
public class ModelWrapper {

    @Autowired
    private AmazonS3 amazonS3Client;

    @Value("${aWS.bucket}")
    protected String bucket;

    @Value("${aWS.file.name}")
    private String awsFileName;

    @PostConstruct
    public void setModel() {
        S3Object s3object = 
            amazonS3Client.getObject(new GetObjectRequest(bucket, modelName)); // Exception inside here!!!
    }
}

堆栈跟踪的底部:

Caused by: java.lang.NullPointerException: null
    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.getObject(AmazonS3Client.java:1427)
    at com.swipejobs.dispatchprediction.prediction.ModelWrapper.setModel(ModelWrapper.java:56)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)
    ... 31 common frames omitted

通过查看调试器,我可以清楚地看到引发异常的方法,以及为什么我的request.getEndpoint()主机中的主机为空:

enter image description here

但是,我无法弄清为什么主机为空。希望我遗漏了一些明显的东西,但一直试图解决这一问题。任何帮助表示赞赏。谢谢!

0 个答案:

没有答案