Spring Cloud AWS 设置端点

6

我使用Spring Cloud AWS来连接我的Amazon S3, 默认情况下,端点是s3.amasonaws.com。但是由于我的S3存储桶在私有云中而不是公共的亚马逊云中,所以我想更改端点。

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-aws</artifactId>
 </dependency>

. . . .

<dependencyManagement>
 <dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring-cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
  </dependency>
 </dependencies>
</dependencyManagement>

在我的application.properties文件中
cloud.aws.stack.auto=false
cloud.aws.region.static=eu-west-3
storage.s3.accessKey=AKIAJNGI4VX4DTY4U24Q

感谢您的帮助。
谢谢您的协助。

不存在属性 cloud.aws.endpoint ??? - yahia mourad
不,它不是这样的。 - Joaquín L. Robles
2个回答

4
我曾经尝试在我的机器上使用配置了本地S3的LocalStack时遇到了类似的问题。由于Spring Boot没有配置端点的选项,我们不得不自己定义bean。然后,我们只需要使用新定义的bean即可。
/**
 * It must be configured, if we need to upload a file using the LocakStack configuration.
 * Because of it, we must define a new client since the default one has not an option to configure the Endpoint.
 *
 * @see org.springframework.cloud.aws.context.config.annotation.ContextResourceLoaderConfiguration.Registrar#registerBeanDefinitions(AnnotationMetadata, BeanDefinitionRegistry)
 */
@Bean(name = "amazonS3Client")
public AmazonS3 amazonS3Client(AWSCredentialsProvider credentialsProvider,
                               RegionProvider regionProvider,
                               @Value("${aws.s3.default-endpoint:https://s3.amazonaws.com}") String endpoint) {

    return AmazonS3ClientBuilder.standard()
        .withCredentials(credentialsProvider)
        .withEndpointConfiguration(
            new AwsClientBuilder.EndpointConfiguration(endpoint, regionProvider.getRegion().getName()))
        .build();
}

你如何告诉Spring使用这个bean而不是自动配置的客户端? - Joaquín L. Robles
你可以使用 @Primary 注解。 - Felipe Desiderati
你可以使用@Autowired AmazonS3 amazonS3Client - Le Quang Nhan
@LeQuangNhan Amazon默认的S3客户端没有配置使用Localstack的端点选项。 - Felipe Desiderati
1
@FelipeDesiderati 是的,我知道。我的评论是关于“你如何告诉Spring使用这个bean而不是自动配置的客户端?” - Le Quang Nhan

2
您可以使用cloud.aws.s3.endpoint属性来覆盖终端节点。

版本2.2.6.RELEASE中没有cloud.aws.s3.endpoint属性。不确定您使用的是哪个版本。 - Le Quang Nhan
好的,文档中写道 cloud.aws.s3.endpoint 可以“覆盖默认端点”。https://docs.awspring.io/spring-cloud-aws/docs/current/reference/html/appendix.html - Robert Strauch
但是.endpoint实际上不起作用,即使对于cloud.aws.sqs.endpoint也是如此,在Spring版本2.7.2上进行测试。 - Jie Wang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接