2016-11-24 19 views
0

嘗試使用spring啓動應用程序與aws-s3連接。當我使用工廠方法'amazonS3Client':訪問密鑰不能爲空

進口org.springframework.cloud.aws.context.support.io.ResourceLoaderBeanPostProcessor

獲得AWS鍵不能爲空。

@Configuration 
@EnableContextResourceLoader 
@EnableContextCredentials 
public class S3Configuration { 

     @Value("${cloud.aws.credentials.accessKey}") 
     private String ACCESS_KEY; 
     @Value("${cloud.aws.credentials.secretKey}") 
     private String SECRET_KEY; 

     @Value("${cloud.aws.region}") 
     private String region; 

@Bean 
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { 
    return new PropertySourcesPlaceholderConfigurer(); 
} 

@Bean 
public AmazonS3Client amazonS3Client() { 
    return new AmazonS3Client(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY)); 
} 

@Bean 
public ResourceLoaderBeanPostProcessor resourceLoaderBeanPostProcessor() { 
    return new ResourceLoaderBeanPostProcessor(amazonS3Client()); 
} 
} 

如果我不使用「ResourceLoaderBeanPostProcessor」類,然後AmazonS3Client對象被成功讀取性能形成application.properties創建。

有人能幫助我,我做錯了什麼?

回答

0

我很確定ResourceLoaderBeanPostProcessor實現的BeanFactoryPostProcessor類在application.properties的值由spring應用程序加載/注入之前執行。因此你的值(創建bean時)是null。 (這就是爲什麼當你不使用那個特殊類的時候它是有效的)。

我會建議使用默認憑證鏈,並將憑據放在~/.aws/文件夾中(在本地計算機上和服務器上)。有關更多詳細信息,請參閱http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html

+0

謝謝,它解決了。 –