From e4d79bd39b8e9072a2e85805643937c605e76378 Mon Sep 17 00:00:00 2001 From: belljun3395 <195850@jnu.ac.kr> Date: Sun, 30 Jun 2024 14:55:21 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20storage=20client=20=EB=A5=BC=20?= =?UTF-8?q?=EC=83=81=EC=9C=84=20=ED=8C=A8=ED=82=A4=EC=A7=80=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=B4=EB=8F=99=ED=95=98=EC=97=AC=20=ED=95=98=EC=9C=84=20?= =?UTF-8?q?=ED=8C=A8=ED=82=A4=EC=A7=80=EB=93=A4=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/src/test/resources/application-test.yml | 10 +++-- resources/docker/docker-compose-api.yml | 8 ++-- .../com/few/storage/config/ClientConfig.kt | 42 +++++++++++++++++++ .../image/config/ImageStorageConfig.kt | 3 ++ .../image/config/S3ImageStoreConfig.kt | 33 ++------------- .../resources/application-storage-local.yml | 10 +++-- .../resources/application-storage-prd.yml | 11 +++-- 7 files changed, 72 insertions(+), 45 deletions(-) create mode 100644 storage/src/main/kotlin/com/few/storage/config/ClientConfig.kt diff --git a/api/src/test/resources/application-test.yml b/api/src/test/resources/application-test.yml index a2699f80b..d00ec197b 100644 --- a/api/src/test/resources/application-test.yml +++ b/api/src/test/resources/application-test.yml @@ -24,13 +24,15 @@ spring: enable: true EnableSSL: enable: true +storage: + url: http://127.0.0.1:9000 + access-key: thisisroot + secret-key: thisisroot + region: ap-northeast-2 + image: store: - url: http://127.0.0.1:9000 - access-key: thisisroot - secret-key: thisisroot bucket-name: picture - region: ap-northeast-2 cdn: url: http://127.0.0.1:9000 diff --git a/resources/docker/docker-compose-api.yml b/resources/docker/docker-compose-api.yml index dba02dae6..26a83e62b 100644 --- a/resources/docker/docker-compose-api.yml +++ b/resources/docker/docker-compose-api.yml @@ -12,9 +12,9 @@ services: DB_PASSWORD: ${DB_PASSWORD} EMAIL_USERNAME: ${EMAIL_USERNAME} EMAIL_PASSWORD: ${EMAIL_PASSWORD} - IMAGE_STORE_URL: ${IMAGE_STORE_URL} - IMAGE_STORE_ACCESS_KEY: ${IMAGE_STORE_ACCESS_KEY} - IMAGE_STORE_SECRET_KEY: ${IMAGE_STORE_SECRET_KEY} + STORAGE_URL: ${STORAGE_URL} + STORAGE_ACCESS_KEY: ${STORAGE_ACCESS_KEY} + STORAGE_SECRET_KEY: ${STORAGE_SECRET_KEY} + STORAGE_REGION: ${STORAGE_REGION} IMAGE_STORE_BUCKET_NAME: ${IMAGE_STORE_BUCKET_NAME} - IMAGE_STORE_REGION: ${IMAGE_STORE_REGION} CDN_URL: ${CDN_URL} diff --git a/storage/src/main/kotlin/com/few/storage/config/ClientConfig.kt b/storage/src/main/kotlin/com/few/storage/config/ClientConfig.kt new file mode 100644 index 000000000..eae4aff84 --- /dev/null +++ b/storage/src/main/kotlin/com/few/storage/config/ClientConfig.kt @@ -0,0 +1,42 @@ +package com.few.storage.config + +import com.amazonaws.auth.AWSStaticCredentialsProvider +import com.amazonaws.auth.BasicAWSCredentials +import com.amazonaws.client.builder.AwsClientBuilder +import com.amazonaws.services.s3.AmazonS3Client +import com.amazonaws.services.s3.AmazonS3ClientBuilder +import org.springframework.beans.factory.annotation.Value +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + +@Configuration +class ClientConfig( + @Value("\${storage.url}") val url: String, + @Value("\${storage.access-key}") val accessKey: String, + @Value("\${storage.secret-key}") val secretKey: String, + @Value("\${storage.region}") val region: String +) { + + @Bean + fun s3StorageClient(): AmazonS3Client { + val builder = AmazonS3ClientBuilder.standard() + .withCredentials( + AWSStaticCredentialsProvider( + BasicAWSCredentials( + accessKey, + secretKey + ) + ) + ) + .withEndpointConfiguration( + AwsClientBuilder.EndpointConfiguration( + url, + region + ) + ) + + builder.build().let { client -> + return client as AmazonS3Client + } + } +} \ No newline at end of file diff --git a/storage/src/main/kotlin/com/few/storage/image/config/ImageStorageConfig.kt b/storage/src/main/kotlin/com/few/storage/image/config/ImageStorageConfig.kt index e42197f51..b4605d1d8 100644 --- a/storage/src/main/kotlin/com/few/storage/image/config/ImageStorageConfig.kt +++ b/storage/src/main/kotlin/com/few/storage/image/config/ImageStorageConfig.kt @@ -1,10 +1,13 @@ package com.few.storage.image.config +import com.few.storage.config.ClientConfig import org.springframework.context.annotation.ComponentScan import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.Import @Configuration @ComponentScan(basePackages = [ImageStorageConfig.BASE_PACKAGE]) +@Import(ClientConfig::class) class ImageStorageConfig { companion object { const val BASE_PACKAGE = "com.few.storage.image" diff --git a/storage/src/main/kotlin/com/few/storage/image/config/S3ImageStoreConfig.kt b/storage/src/main/kotlin/com/few/storage/image/config/S3ImageStoreConfig.kt index 5be02cdb6..659ac7693 100644 --- a/storage/src/main/kotlin/com/few/storage/image/config/S3ImageStoreConfig.kt +++ b/storage/src/main/kotlin/com/few/storage/image/config/S3ImageStoreConfig.kt @@ -1,10 +1,6 @@ package com.few.storage.image.config -import com.amazonaws.auth.AWSStaticCredentialsProvider -import com.amazonaws.auth.BasicAWSCredentials -import com.amazonaws.client.builder.AwsClientBuilder import com.amazonaws.services.s3.AmazonS3Client -import com.amazonaws.services.s3.AmazonS3ClientBuilder import com.few.storage.image.client.ImageStoreClient import com.few.storage.image.client.S3ImageStoreClient import org.slf4j.Logger @@ -17,11 +13,8 @@ import org.springframework.context.event.ContextRefreshedEvent @Configuration class S3ImageStoreConfig( - @Value("\${image.store.url}") val url: String, - @Value("\${image.store.access-key}") val accessKey: String, - @Value("\${image.store.secret-key}") val secretKey: String, @Value("\${image.store.bucket-name}") val bucket: String, - @Value("\${image.store.region}") val region: String + @Value("\${storage.region}") val region: String ) : ApplicationListener { var log: Logger = LoggerFactory.getLogger(S3ImageStoreConfig::class.java) @@ -41,26 +34,8 @@ class S3ImageStoreConfig( } @Bean - fun s3ImageStoreClient(): ImageStoreClient { - val builder = AmazonS3ClientBuilder.standard() - .withCredentials( - AWSStaticCredentialsProvider( - BasicAWSCredentials( - accessKey, - secretKey - ) - ) - ) - .withEndpointConfiguration( - AwsClientBuilder.EndpointConfiguration( - url, - region - ) - ) - - builder.build().let { client -> - this.client = client as AmazonS3Client - return S3ImageStoreClient(client, region) - } + fun s3ImageStoreClient(s3StorageClient: AmazonS3Client): ImageStoreClient { + client = s3StorageClient + return S3ImageStoreClient(client!!, region) } } \ No newline at end of file diff --git a/storage/src/main/resources/application-storage-local.yml b/storage/src/main/resources/application-storage-local.yml index 39dfe4a8d..3024181e5 100644 --- a/storage/src/main/resources/application-storage-local.yml +++ b/storage/src/main/resources/application-storage-local.yml @@ -1,10 +1,12 @@ +storage: + url: http://127.0.0.1:9000 + access-key: thisisroot + secret-key: thisisroot + region: ap-northeast-2 + image: store: - url: http://127.0.0.1:9000 - access-key: thisisroot - secret-key: thisisroot bucket-name: picture - region: ap-northeast-2 cdn: url: http://127.0.0.1:9000 diff --git a/storage/src/main/resources/application-storage-prd.yml b/storage/src/main/resources/application-storage-prd.yml index 37dffaaab..3f5114b6c 100644 --- a/storage/src/main/resources/application-storage-prd.yml +++ b/storage/src/main/resources/application-storage-prd.yml @@ -1,10 +1,13 @@ +storage: + url: ${STORAGE_URL} + access-key: ${STORAGE_ACCESS_KEY} + secret-key: ${STORAGE_SECRET_KEY} + region: ${STORAGE_REGION} + image: store: - url: ${IMAGE_STORE_URL} - access-key: ${IMAGE_STORE_ACCESS_KEY} - secret-key: ${IMAGE_STORE_SECRET_KEY} bucket-name: ${IMAGE_STORE_BUCKET_NAME} - region: ${IMAGE_STORE_REGION} + region: ${STORAGE_REGION} cdn: url: ${CDN_URL}