如何从Spring Boot应用程序连接两个AWS S3存储桶

时间:2020-03-06 04:54:14

标签: spring amazon-web-services spring-boot amazon-s3

我想从Spring Boot Application连接两个S3存储桶。我用不同的凭据制作了两个不同的bean,并制作了一个@primary现在我的应用程序可以正常运行,但是当我尝试访问不是@primary的第二个存储桶时,它给了我403访问被拒绝的异常

com.amazonaws.services.s3.model.AmazonS3Exception:访问被拒绝(服务:Amazon S3;状态代码:403;错误代码:AccessDenied;

以下是我的代码,对您的帮助将不胜感激 在此先感谢

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;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class AWSConfiguration {

    @Value("${One.cloud.aws.credentials.accessKey}")
    private String accessKeyOne;

    @Value("${One.cloud.aws.credentials.secretKey}")
    private String secretKeyOne;

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

    @Value("${Two.bucket.accessKey}")
    private String accessKeyTwo;

    @Value("${Two.bucket.secretKey}")
    private String secretKeyTwo;

    @Value("${Two.bucket.region}")
    private String regionTwo;

    @Bean
    @Primary
    public BasicAWSCredentials basicAWSCredentialsOne() {
        return new BasicAWSCredentials(accessKeyOne, secretKeyOne);
    }

    @Bean
    @Primary
    public AmazonS3 amazonS3ClientOne(AWSCredentials awsCredentials) {
        AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();
        builder.withCredentials(new AWSStaticCredentialsProvider(awsCredentials));
        builder.setRegion(regionOne);
        AmazonS3 amazonS3 = builder.build();
        return amazonS3;
    }

    @Bean
    public BasicAWSCredentials basicAWSCredentialsTwo() {
        return new BasicAWSCredentials(accessKeyTwo, secretKeyTwo);
    }

    @Bean
    public AmazonS3 amazonS3ClientTwo(AWSCredentials awsCredentials) {
        AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();
        builder.withCredentials(new AWSStaticCredentialsProvider(awsCredentials));
        builder.setRegion(regionTwo);
        AmazonS3 amazonS3 = builder.build();
        return amazonS3;
    }
}

1 个答案:

答案 0 :(得分:0)

是否有必要将BasicAWSCredentials都公开为应用程序的bean?您不能像以下那样内联凭据吗?

@Configuration
public class AWSConfiguration {

    @Value("${One.cloud.aws.credentials.accessKey}")
    private String accessKeyOne;

    @Value("${One.cloud.aws.credentials.secretKey}")
    private String secretKeyOne;

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

    @Value("${Two.bucket.accessKey}")
    private String accessKeyTwo;

    @Value("${Two.bucket.secretKey}")
    private String secretKeyTwo;

    @Value("${Two.bucket.region}")
    private String regionTwo;

    @Bean
    @Primary
    public AmazonS3 amazonS3ClientOne(AWSCredentials awsCredentials) {
        AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();
        builder.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKeyOne, secretKeyOne)));
        builder.setRegion(regionOne);
        AmazonS3 amazonS3 = builder.build();
        return amazonS3;
    }

    @Bean
    public AmazonS3 amazonS3ClientTwo(AWSCredentials awsCredentials) {
        AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();
        builder.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKeyTwo, secretKeyTwo)));
        builder.setRegion(regionTwo);
        AmazonS3 amazonS3 = builder.build();
        return amazonS3;
    }
}

如果您想通过将两个凭证公开为Bean来保持当前方法,则可以查看@Qualifier批注以在注入它们时指定正确的Credentials / AmzonS3存储桶实例,例如

@Bean
public AmazonS3 amazonS3ClientTwo(@Qualifier("basicAWSCredentialsTwo") AWSCredentials awsCredentials) {
    AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();
    builder.withCredentials(new AWSStaticCredentialsProvider(awsCredentials));
    builder.setRegion(regionTwo);
    AmazonS3 amazonS3 = builder.build();
    return amazonS3;
}

Baeldung上有一个很好的教程。