【问题标题】:unable to load credentials from aws.properties无法从 aws.properties 加载凭据
【发布时间】:2015-09-09 12:57:41
【问题描述】:

我在 java 中使用 aws ses 并收到此错误。我在谷歌上搜索过,但得到了我的问题的任何答案。就是这样,我把我的问题放在这里。我已经尝试了很多次,并且完成了我从谷歌获得的所有更改,但没有任何帮助。这是我的例外..

Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
at com.artle.Application.createAwsCredentials(Application.java:48)
at com.artle.Application.createSimpleEmailService(Application.java:65)
at com.artle.Application.sendTestEmail(Application.java:70)
at com.artle.Application.main(Application.java:78)

这是我的 Application.java 文件

@EnableAutoConfiguration
@ComponentScan
@Configuration
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

@Bean
public FilterRegistrationBean SimpleCORSFilter() {
    final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
    registrationBean.setFilter(new SimpleCORSFilter());
    //registrationBean.addUrlPatterns("/api/*");
    return registrationBean;
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
} 

private AWSCredentials createAwsCredentials() throws IOException{

    Properties properties=new Properties();
    properties.load(getClass().getClassLoader().getResourceAsStream(
            "aws.properties"));
AWSCredentials credentials =new BasicAWSCredentials(
        properties.getProperty("aws.accessKey"),
        properties.getProperty("aws.secretKey"));   

AWSCredentials credentials2=new PropertiesCredentials(
        getClass().getClassLoader().getResourceAsStream(
                "aws.properties"));



    return credentials;
}

private AmazonSimpleEmailService createSimpleEmailService() throws IOException{

    return new AmazonSimpleEmailServiceClient(createAwsCredentials());

}

private void sendTestEmail() throws IOException{
 PostMan postMan= new AwsPostMan(createSimpleEmailService());   
 postMan.withFrom("ansh@artle.in").withTo("amit0133@gmail.com").
 withSubject("Email from aws!!").
 withBody("This is a test email from AWS").send();
}

public static void main(String[] args) throws IOException { 

new Application().sendTestEmail();

SpringApplication.run(Application.class, args); 

}

}

行号48是

properties.load(getClass().getClassLoader().getResourceAsStream(
            "aws.properties"));

我刚开始在亚马逊网络服务上工作,所以请帮助我。

【问题讨论】:

  • 顺便说一句:EnableAutoConfiguration、ComponentScan 和 Configuration 注释都是多余的,因为 SpringBootApplication 暗示

标签: java maven amazon-web-services spring-boot


【解决方案1】:

不确定您的属性文件到底发生了什么。有时在 IDE 中运行它与执行此类操作时实际部署它的方式之间存在一些差异。

但我认为您可能想要做的是改用 Spring Boot application.properties 文件,然后通过 Spring 使用 @Value 注释将其注入

但也许您真正想做的是查看为您设置这些东西的Spring Cloud AWS

【讨论】:

  • 我不知道如何加载凭据。
  • 其实我想在我的 Spring Boot 项目中运行 aws ses 来发送电子邮件
  • Spring Cloud AWS 使用两个密钥 aws.accessKeyId 和 aws.secretKey 作为凭证​​。它们将通过通常的 Spring 属性提供程序加载。对于 Spring Boot,请参见该页面:docs.spring.io/spring-boot/docs/current/reference/html/…
猜你喜欢
  • 1970-01-01
  • 2020-10-24
  • 1970-01-01
  • 2018-01-17
  • 1970-01-01
  • 2021-01-06
  • 2020-04-28
  • 2017-07-13
  • 1970-01-01
相关资源
最近更新 更多