Spring Cloud AWS设置端点

时间:2018-07-08 20:01:21

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

我使用Spring Cloud AWS来连接到OpenStack中的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

希望为您提供帮助。

1 个答案:

答案 0 :(得分:0)

我在尝试在计算机上本地配置的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();
}