如何在配置类中定义'com.amazonaws.services.s3.AmazonS3'的bean类型?

时间:2019-09-06 15:15:29

标签: spring spring-boot spring-mvc

我正在尝试在Spring引导应用程序中配置AWS,但是在配置中无法识别AmazonS3 bean。结果是我收到此错误:
Consider defining a bean of type com.amazonaws.services.s3.AmazonS3'在您的配置中。
我该如何解决这个问题?

AWSConfiguration.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;


@Configuration
public class AWSConfiguration {
    @Value("${accessKey}")
    private String accessKey;

    @Value("${secretKey}")
    private String secretKey;

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

    @Bean
    public BasicAWSCredentials basicAWSCredentials() {
        return new BasicAWSCredentials(accessKey, secretKey);
    }

    @Bean
    public AmazonS3 amazonS3Client(AWSCredentials awsCredentials) {
        return AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                .withRegion(region).build();
}}

ExampleService.java

import com.amazonaws.services.s3.AmazonS3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;


@Service
public class ExampleService {

    @Autowired
    private AmazonS3 amazonClient;

}

0 个答案:

没有答案