asus
asus

Reputation: 1759

Spring AWS - required bean could not be found

I am trying to start my Spring server but keep getting this error:

Description:

Parameter 0 of constructor in com.rm.awsimageupload.filestore.FileStore required a bean of type 'com.amazonaws.services.s3.AmazonS3' that could not be found.


Action:

Consider defining a bean of type 'com.amazonaws.services.s3.AmazonS3' in your configuration.

I have tried:

  1. pasting in the keys as strings right into the BasicAWSCredentials
  2. putting the keys into application.properties and then using @Value to access them. With this approach, I get this error:
Error creating bean with name 'amazonConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'local.AWS_ACCESS_KEY_ID' in value "${local.AWS_ACCESS_KEY_ID}"

application.properties

AWS_ACCESS_KEY_ID=NOTAREALKEY
AWS_SECRET_ACCESS_KEY=NOTAREALSECRET

FileStore.java


@Configuration
public class AmazonConfig {

    @Value("${application.AWS_ACCESS_KEY_ID}")
    private String AWS_ACCESS_KEY_ID;

    @Value("${application.AWS_SECRET_ACCESS_KEY}")
    private String AWS_SECRET_ACCESS_KEY;

    public AmazonS3 S3() {
        AWSCredentials awsCredentials = new BasicAWSCredentials(
            AWS_ACCESS_KEY_ID, 
            AWS_SECRET_ACCESS_KEY
        );

        return AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).withRegion(Regions.SA_EAST_1).build();
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.rm</groupId>
    <artifactId>aws-image-upload</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>aws-image-upload</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk</artifactId>
            <version>1.11.787</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Not exactly sure what to do... I am following a tutorial on how to do this and my config pretty much matches this instructor's. Any suggestions?

Upvotes: 3

Views: 14893

Answers (1)

NullPointerException
NullPointerException

Reputation: 3814

  1. Your are missing @Bean annotation on public AmazonS3 S3()
    @Bean
     public AmazonS3 S3() {
        AWSCredentials awsCredentials = new BasicAWSCredentials(
            AWS_ACCESS_KEY_ID, 
            AWS_SECRET_ACCESS_KEY
        );

        return AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).withRegion(Regions.SA_EAST_1).build();
    }
  1. Regarding the 2nd issue for properties values, not sure if this the typo here local.AWS_ACCESS_KEY_ID in logs vs application.AWS_ACCESS_KEY_ID in code sample. You need to use the same keys name as in your properties file So you config class should be having

    @Value("${AWS_ACCESS_KEY_ID}")
    private String AWS_ACCESS_KEY_ID;
    
    @Value("${AWS_SECRET_ACCESS_KEY}")
    private String AWS_SECRET_ACCESS_KEY;
    

Upvotes: 7

Related Questions