Spring Cloud aws 设置端点

yah*_*rad 5 amazon-s3 amazon-web-services spring-boot 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>
Run Code Online (Sandbox Code Playgroud)

. . . .

<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>
Run Code Online (Sandbox Code Playgroud)

.... 在我的 application.properties 中

cloud.aws.stack.auto=false
cloud.aws.region.static=eu-west-3
storage.s3.accessKey=AKIAJNGI4VX4DTY4U24Q
Run Code Online (Sandbox Code Playgroud)

想你的帮助。

Fel*_*ati 4

我在尝试使用 LocalStack 和在我的机器上本地配置的 S3 时遇到了类似的问题。由于 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();
}
Run Code Online (Sandbox Code Playgroud)