【问题标题】:How to connect two AWS S3 Buckets from spring boot application如何从 Spring Boot 应用程序连接两个 AWS S3 存储桶
【发布时间】:2020-03-06 04:54:14
【问题描述】:

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

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;
    }
}

【问题讨论】:

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


    【解决方案1】:

    是否有必要将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 上有一个很好的教程可用于此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-27
      • 2019-04-18
      • 2014-04-22
      • 2023-01-18
      • 2022-10-04
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      相关资源
      最近更新 更多