LoginSignup
5
4

More than 3 years have passed since last update.

Spring boot(Kotlin)でlocalstack(s3)を利用する(aws sdk 2対応)

Last updated at Posted at 2020-06-15

ローカル開発用にlocalstack(s3)を準備する。

docker-composeの準備

  localstack:
    image: "localstack/localstack"
    ports:
      - 4572:4572
    environment:
      - SERVICES=s3
      - AWS_ACCESS_KEY_ID=dummy
      - AWS_SECRET_ACCESS_KEY=dummy
      - AWS_DEFAULT_REGION=ap-northeast-1
    volumes:
      - ./aws:/docker-entrypoint-initaws.d

コンテナ立ち上げ時にバケット作成する用のスクリプト。
aws/buckets.shという名前でファイルを作成し、今回は起動時に2つのバケットを作成。

#!/bin/bash

set -x
awslocal s3 mb s3://user_images
set +x

set -x
awslocal s3 mb s3://assets
set +x

localstack専用の設定クラスapplication.yamlなどで環境を別けながら利用する。
環境毎に別クラスで用意するのがよい。
http://endok.hatenablog.com/entry/2016/06/12/181900

@Configuration
@Profile("local")
class LocalAmazonS3Configuration {
    @Bean
    fun localstackS3(): AmazonS3 {
        return AmazonS3ClientBuilder.standard()
            .enablePathStyleAccess()
            .withEndpointConfiguration(
                AwsClientBuilder.EndpointConfiguration(
                    "http://localhost:4572",
                    Regions.AP_NORTHEAST_1.name
                )
            )
            .build()
    }
}

(参考)
https://www.codota.com/code/java/methods/com.amazonaws.services.s3.AmazonS3ClientBuilder/standard

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4