Skip to content

Commit

Permalink
refactor: storage client 를 상위 패키지로 이동하여 하위 패키지들에서 사용할 수 있도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
belljun3395 committed Jun 30, 2024
1 parent af1503c commit e4d79bd
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 45 deletions.
10 changes: 6 additions & 4 deletions api/src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions resources/docker/docker-compose-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
42 changes: 42 additions & 0 deletions storage/src/main/kotlin/com/few/storage/config/ClientConfig.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
}
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<ContextRefreshedEvent> {

var log: Logger = LoggerFactory.getLogger(S3ImageStoreConfig::class.java)
Expand All @@ -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)
}
}
10 changes: 6 additions & 4 deletions storage/src/main/resources/application-storage-local.yml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 7 additions & 4 deletions storage/src/main/resources/application-storage-prd.yml
Original file line number Diff line number Diff line change
@@ -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}

0 comments on commit e4d79bd

Please sign in to comment.