From d29d402fab48542d7d0a4d16c55ff9ceeb6b7f10 Mon Sep 17 00:00:00 2001 From: SeoYeongU Date: Wed, 31 Jul 2024 21:58:50 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20S3=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit .env 파일은 환경변수 깃허브에 설정해 놓았습니다. 커밋하지 않았습니다. --- BE/error/build.gradle | 6 +++ .../com/example/demo/config/EnvConfig.java | 9 ++++ .../example/demo/config/StorageConfig.java | 34 ++++++++++++ .../application/service/ImageService.java | 54 +++++++++++++++++++ .../image/presentation/ImageController.java | 28 ++++++++++ .../src/main/resources/application.properties | 3 -- BE/error/src/main/resources/application.yml | 33 ++++++++++++ 7 files changed, 164 insertions(+), 3 deletions(-) create mode 100644 BE/error/src/main/java/com/example/demo/config/EnvConfig.java create mode 100644 BE/error/src/main/java/com/example/demo/config/StorageConfig.java create mode 100644 BE/error/src/main/java/com/example/demo/image/application/service/ImageService.java create mode 100644 BE/error/src/main/java/com/example/demo/image/presentation/ImageController.java create mode 100644 BE/error/src/main/resources/application.yml diff --git a/BE/error/build.gradle b/BE/error/build.gradle index c2d85da1..d13f9c38 100644 --- a/BE/error/build.gradle +++ b/BE/error/build.gradle @@ -53,6 +53,12 @@ dependencies { implementation 'org.hibernate:hibernate-validator:5.2.4.Final' //implementation 'org.springframework.boot:spring-boot-starter-security' + + implementation 'com.amazonaws:aws-java-sdk-s3:1.12.3' + implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE' + implementation 'software.amazon.awssdk:s3:2.17.28' + implementation "me.paulschwarz:spring-dotenv:4.0.0" + implementation "com.amazonaws:aws-java-sdk-s3:1.12.281" } tasks.named('test') { diff --git a/BE/error/src/main/java/com/example/demo/config/EnvConfig.java b/BE/error/src/main/java/com/example/demo/config/EnvConfig.java new file mode 100644 index 00000000..c5053ce7 --- /dev/null +++ b/BE/error/src/main/java/com/example/demo/config/EnvConfig.java @@ -0,0 +1,9 @@ +package com.example.demo.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.annotation.PropertySources; + +@Configuration +@PropertySource("classpath:/.env") +public class EnvConfig {} diff --git a/BE/error/src/main/java/com/example/demo/config/StorageConfig.java b/BE/error/src/main/java/com/example/demo/config/StorageConfig.java new file mode 100644 index 00000000..ae760df2 --- /dev/null +++ b/BE/error/src/main/java/com/example/demo/config/StorageConfig.java @@ -0,0 +1,34 @@ +package com.example.demo.config; + +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.regions.Regions; +import com.amazonaws.services.s3.AmazonS3Client; +import com.amazonaws.services.s3.AmazonS3ClientBuilder; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Bean; + +@Configuration +public class StorageConfig { + + @Value("${cloud.aws.credentials.accessKey}") + private String accessKey; + + @Value("${SECRET_KEY}") + private String secretKey; + + @Value("${cloud.aws.region.static}") + private String region; + + @Bean + public AmazonS3Client amazonS3Client() { + BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); + + return (AmazonS3Client) AmazonS3ClientBuilder.standard() + .withRegion(region) + .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) + .build(); + } + +} diff --git a/BE/error/src/main/java/com/example/demo/image/application/service/ImageService.java b/BE/error/src/main/java/com/example/demo/image/application/service/ImageService.java new file mode 100644 index 00000000..ad85a6c4 --- /dev/null +++ b/BE/error/src/main/java/com/example/demo/image/application/service/ImageService.java @@ -0,0 +1,54 @@ +package com.example.demo.image.application.service; + +import com.amazonaws.services.s3.AmazonS3Client; +import com.amazonaws.services.s3.model.ListObjectsV2Request; +import com.amazonaws.services.s3.model.ListObjectsV2Result; +import com.amazonaws.services.s3.model.S3ObjectSummary; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +@Slf4j +@RequiredArgsConstructor +@Service +public class ImageService { + + private final AmazonS3Client amazonS3Client; + + @Value("${cloud.aws.s3.bucket}") + private String bucketName; + + @Value("${cloud.aws.region.static}") + private String region; + + public List> getFileList(String directory) { + List> fileList = new ArrayList<>(); + + ListObjectsV2Request listObjectsV2Request = new ListObjectsV2Request() + .withBucketName(bucketName) + .withPrefix(directory + "/"); + + ListObjectsV2Result result = amazonS3Client.listObjectsV2(listObjectsV2Request); + List objectSummaries = result.getObjectSummaries(); + + int id = 1; + for (S3ObjectSummary objectSummary : objectSummaries) { + String key = objectSummary.getKey(); + if (!key.equals(directory + "/")) { + Map fileInfo = new HashMap<>(); + fileInfo.put("id", id++); + fileInfo.put("url", "https://" + bucketName + ".s3." + region + ".amazonaws.com/" + key); + fileList.add(fileInfo); + } + } + + Collections.shuffle(fileList); + return fileList.size() > 16 ? fileList.subList(0, 16) : fileList; + } +} diff --git a/BE/error/src/main/java/com/example/demo/image/presentation/ImageController.java b/BE/error/src/main/java/com/example/demo/image/presentation/ImageController.java new file mode 100644 index 00000000..a17d8323 --- /dev/null +++ b/BE/error/src/main/java/com/example/demo/image/presentation/ImageController.java @@ -0,0 +1,28 @@ +package com.example.demo.image.presentation; + +import com.example.demo.image.application.service.ImageService; +import java.util.List; +import java.util.Map; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/images") +public class ImageController { + + private final ImageService imageService; + + public ImageController(ImageService imageService) { + this.imageService = imageService; + } + + @GetMapping + public List> getProfileImages (){ + List> profileImageList = imageService.getFileList("styleganresult2"); + return profileImageList; + } +} diff --git a/BE/error/src/main/resources/application.properties b/BE/error/src/main/resources/application.properties index ae150eaf..377f7a47 100644 --- a/BE/error/src/main/resources/application.properties +++ b/BE/error/src/main/resources/application.properties @@ -9,6 +9,3 @@ spring.config.activate.on-profile.oauth.provider.slack.clientId=437291124342.714 spring.config.activate.on-profile.oauth.provider.slack.cliientSecret=b7ea56364590159cd00916bd9e03bec3 spring.data.redis.host=redis spring.data.redis.port=6379 - - - diff --git a/BE/error/src/main/resources/application.yml b/BE/error/src/main/resources/application.yml new file mode 100644 index 00000000..a6328103 --- /dev/null +++ b/BE/error/src/main/resources/application.yml @@ -0,0 +1,33 @@ +spring: + application: + name: error + + jpa: + properties: + hibernate: + format_sql: true + dialect: org.hibernate.dialect.MySQLDialect + show-sql: true + hibernate: + ddl-auto: create + +logging: + level: + org: + hibernate: + type: + descriptor: + sql: + BasicBinder: TRACE + +cloud: + aws: + region: + static: ap-northeast-2 + stack: + auto: false + credentials: + accessKey: ${ACCESS_KEY} + secretKey: ${SECRET_KEY} + s3: + bucket: zepimage0