From d2b62244d7a693286a335bd5fdefc881873c24cf Mon Sep 17 00:00:00 2001 From: 3uomlkh <3uomlkh@gmail.com> Date: Fri, 28 Mar 2025 23:30:31 +0900 Subject: [PATCH] =?UTF-8?q?refactor(config):=20S3=20=EC=84=A4=EC=A0=95=20?= =?UTF-8?q?=EC=A0=95=EB=A6=AC=20=EB=B0=8F=20application=20yml=20=EA=B5=AC?= =?UTF-8?q?=EC=A1=B0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### 변경사항 - application.yml, application-local.yml, application-ci.yml 설정 정리 - S3 bucket 이름과 region은 서비스단에서 상수 처리 - 중복 spring 블록 제거 및 YAML 구조 개선 - ci.yml 디버그 모드 해제 --- .github/workflows/ci.yml | 7 +++--- .../product/service/ProductImageService.java | 15 ++++-------- .../eightyage/global/config/S3Config.java | 10 ++++---- src/main/resources/application-ci.yml | 21 ++++++++-------- src/main/resources/application-local.yml | 24 ++++++++++--------- src/main/resources/application.yml | 16 +------------ 6 files changed, 38 insertions(+), 55 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6a531fc..58dc29b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,10 +44,11 @@ jobs: - name: Grant execute permission for gradlew run: chmod +x ./gradlew - - name: Clean Gradle Cache (Optional but good) + - name: Clean Gradle Cache run: ./gradlew clean --refresh-dependencies - - name: Test And Build with Gradle (Debug Mode) + + - name: Test And Build with Gradle env: JWT_SECRET_KEY: ${{ secrets.JWT_SECRET_KEY }} - run: ./gradlew build --stacktrace --info -Dspring.profiles.active=ci + run: ./gradlew build -Dspring.profiles.active=ci diff --git a/src/main/java/com/example/eightyage/domain/product/service/ProductImageService.java b/src/main/java/com/example/eightyage/domain/product/service/ProductImageService.java index d5ed35e..0a3599c 100644 --- a/src/main/java/com/example/eightyage/domain/product/service/ProductImageService.java +++ b/src/main/java/com/example/eightyage/domain/product/service/ProductImageService.java @@ -3,7 +3,6 @@ import com.example.eightyage.domain.product.entity.Product; import com.example.eightyage.domain.product.entity.ProductImage; import com.example.eightyage.domain.product.repository.ProductImageRepository; -import com.example.eightyage.domain.product.repository.ProductRepository; import com.example.eightyage.global.exception.NotFoundException; import com.example.eightyage.global.exception.ProductImageUploadException; import lombok.RequiredArgsConstructor; @@ -14,9 +13,8 @@ import software.amazon.awssdk.core.sync.RequestBody; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.services.s3.model.PutObjectRequest; -import software.amazon.awssdk.services.s3.model.DeleteObjectRequest; + import java.io.IOException; -import java.time.LocalDateTime; import java.util.UUID; @Service @@ -27,11 +25,8 @@ public class ProductImageService { private final ProductImageRepository productImageRepository; private final ProductService productService; - @Value("${aws.s3.bucket}") - private String bucket; - - @Value("${aws.region}") - private String region; + private static final String BUCKET_NAME = "my-gom-bucket"; + private static final String REGION = "ap-northeast-2"; // 제품 이미지 업로드 @Transactional @@ -42,7 +37,7 @@ public String uploadImage(Long productId, MultipartFile file) { // S3에 업로드 s3Client.putObject( PutObjectRequest.builder() - .bucket(bucket) + .bucket(BUCKET_NAME) .key(fileName) .contentType(file.getContentType()) .build(), @@ -50,7 +45,7 @@ public String uploadImage(Long productId, MultipartFile file) { ); // S3 이미지 URL 생성 - String imageUrl = String.format("https://%s.s3.%s.amazonaws.com/%s", bucket, region, fileName); + String imageUrl = String.format("https://%s.s3.%s.amazonaws.com/%s", BUCKET_NAME, REGION, fileName); // DB 저장 Product product = productService.findProductByIdOrElseThrow(productId); diff --git a/src/main/java/com/example/eightyage/global/config/S3Config.java b/src/main/java/com/example/eightyage/global/config/S3Config.java index b62e367..5d48b01 100644 --- a/src/main/java/com/example/eightyage/global/config/S3Config.java +++ b/src/main/java/com/example/eightyage/global/config/S3Config.java @@ -11,19 +11,19 @@ @Configuration public class S3Config { - @Value("${aws.region}") - private String region; + private static final String REGION = "ap-northeast-2"; - @Value("${aws.credentials.access-key}") + @Value("${aws.access-key}") private String accessKey; - @Value("${aws.credentials.secret-key}") + @Value("${aws.secret-key}") private String secretKey; + @Bean public S3Client s3Client() { return S3Client.builder() - .region(Region.of(region)) + .region(Region.of(REGION)) .credentialsProvider(StaticCredentialsProvider.create( AwsBasicCredentials.create(accessKey, secretKey) )) diff --git a/src/main/resources/application-ci.yml b/src/main/resources/application-ci.yml index 1110e9e..17459d6 100644 --- a/src/main/resources/application-ci.yml +++ b/src/main/resources/application-ci.yml @@ -3,27 +3,26 @@ server: spring: datasource: - url: jdbc:mysql://mysql:3306/team8_test + url: jdbc:mysql://localhost:3306/team8_test username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver jpa: hibernate: - ddl-auto: create + ddl-auto: update properties: hibernate: dialect: org.hibernate.dialect.MySQLDialect show_sql: true format_sql: true + + cloud: + aws: + credentials: + access-key: ${AWS_ACCESS_KEY} + secret-key: ${AWS_SECRET_KEY} + jwt: secret: - key: ${JWT_SECRET_KEY} - -aws: - credentials: - access-key: ${AWS_ACCESS_KEY} - secret-key: ${AWS_SECRET_KEY} - region: ap-northeast-2 - s3: - bucket: my-gom-bucket \ No newline at end of file + key: ${JWT_SECRET_KEY} \ No newline at end of file diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 610611b..f89a31b 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -1,18 +1,20 @@ spring: + config: + import: optional:file:.env[.properties] + cloud: aws: credentials: access-key: ${AWS_ACCESS_KEY} secret-key: ${AWS_SECRET_KEY} - region: - static: ap-northeast-2 - s3: - bucket: my-gom-bucket -aws: - credentials: - access-key: ${AWS_ACCESS_KEY} - secret-key: ${AWS_SECRET_KEY} - region: ap-northeast-2 - s3: - bucket: my-gom-bucket \ No newline at end of file + datasource: + url: ${DB_URL} + username: ${DB_USER} + password: ${DB_PASSWORD} + driver-class-name: com.mysql.cj.jdbc.Driver + + data: + redis: + host: localhost + port: 6379 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index f87da91..768de84 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -9,24 +9,9 @@ server: session: timeout: 1800 -spring: - config: - import: optional:file:.env[.properties] - data: - redis: - host: localhost - port: 6379 - - application: name: eightyage - datasource: - url: ${DB_URL} - username: ${DB_USER} - password: ${DB_PASSWORD} - driver-class-name: com.mysql.cj.jdbc.Driver - jpa: hibernate: ddl-auto: update @@ -40,3 +25,4 @@ spring: jwt: secret: key: ${JWT_SECRET_KEY} +