如何从Spring Boot应用程序连接两个AWS S3存储桶 [英] How to connect two AWS S3 Buckets from spring boot application

查看:176
本文介绍了如何从Spring Boot应用程序连接两个AWS S3存储桶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to connect two S3 Buckets from spring boot Application. I make two different beans with different credentials and make one @primary now my application runs properly but when I tried to access second bucket which is not @primary It gives me 403 Access Denied Exception

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

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

Below is my code any help will be highly appreciated Thanks in Advance

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

推荐答案

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

Is it necessary to expose both BasicAWSCredentials as beans for your application? Can't you just inline the credentials like the following?

@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存储桶实例,例如

If you want to stick to your current approach with exposing both credentials as beans, you can have a look at the @Qualifier annotation to specify the correct Credentials/AmzonS3 bucket instance while injecting them, e.g.

@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 上有一个很好的教程.

There is a good tutorial on Baeldung available for this.

这篇关于如何从Spring Boot应用程序连接两个AWS S3存储桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆