【发布时间】: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