Spring AWS - 找不到所需的bean

asu*_*sus 2 java spring amazon-s3 maven spring-boot

我正在尝试启动我的 Spring 服务器,但不断收到此错误:

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.
Run Code Online (Sandbox Code Playgroud)

我努力了:

  1. 将键作为字符串直接粘贴到BasicAWSCredentials
  2. 将钥匙放入application.properties,然后使用@Value即可访问它们。通过这种方法,我得到这个错误:
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}"
Run Code Online (Sandbox Code Playgroud)

应用程序属性

AWS_ACCESS_KEY_ID=NOTAREALKEY
AWS_SECRET_ACCESS_KEY=NOTAREALSECRET
Run Code Online (Sandbox Code Playgroud)

文件存储.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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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>

Run Code Online (Sandbox Code Playgroud)

不太确定该怎么做...我正在遵循有关如何执行此操作的教程,并且我的配置几乎与该讲师的配置相匹配。有什么建议么?

Nul*_*ion 6

  1. 您缺少@Bean注释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();
    }
Run Code Online (Sandbox Code Playgroud)
  1. 关于属性值的第二个问题,不确定这是否是 local.AWS_ACCESS_KEY_ID日志中的拼写错误与application.AWS_ACCESS_KEY_ID代码示例中的拼写错误。您需要使用与属性文件中相同的键名称,因此您的配置类应该具有

    @Value("${AWS_ACCESS_KEY_ID}")
    private String AWS_ACCESS_KEY_ID;
    
    @Value("${AWS_SECRET_ACCESS_KEY}")
    private String AWS_SECRET_ACCESS_KEY;
    
    Run Code Online (Sandbox Code Playgroud)