From 55617b83286335399485e20f6d36b1a8abf39ae6 Mon Sep 17 00:00:00 2001 From: SeoYeongU Date: Tue, 16 Jul 2024 01:17:04 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20S3=EC=97=90=EC=84=9C=20=EC=9D=B4?= =?UTF-8?q?=EB=AF=B8=EC=A7=80=20=EB=9E=9C=EB=8D=A4=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EA=B0=80=EC=A0=B8=EC=98=A4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/error/build.gradle | 7 +++ .../com/example/demo/ErrorApplication.java | 18 +------ .../example/demo/image/ImageController.java | 27 +++++++++++ .../com/example/demo/image/ImageService.java | 48 +++++++++++++++++++ .../com/example/demo/image/StorageConfig.java | 33 +++++++++++++ .../src/main/resources/application.properties | 32 ++++++++++--- 6 files changed, 142 insertions(+), 23 deletions(-) create mode 100644 BE/error/src/main/java/com/example/demo/image/ImageController.java create mode 100644 BE/error/src/main/java/com/example/demo/image/ImageService.java create mode 100644 BE/error/src/main/java/com/example/demo/image/StorageConfig.java diff --git a/BE/error/build.gradle b/BE/error/build.gradle index 6a7e60dc..f904fe87 100644 --- a/BE/error/build.gradle +++ b/BE/error/build.gradle @@ -28,6 +28,13 @@ dependencies { annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' runtimeOnly 'com.mysql:mysql-connector-j' + 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" + runtimeOnly 'com.h2database:h2' + implementation 'org.springframework.boot:spring-boot-starter-jdbc' } tasks.named('test') { diff --git a/BE/error/src/main/java/com/example/demo/ErrorApplication.java b/BE/error/src/main/java/com/example/demo/ErrorApplication.java index e96709b8..ce4440ba 100644 --- a/BE/error/src/main/java/com/example/demo/ErrorApplication.java +++ b/BE/error/src/main/java/com/example/demo/ErrorApplication.java @@ -2,26 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration; -import org.springframework.context.annotation.Bean; -import org.springframework.data.jpa.repository.config.EnableJpaAuditing; -import org.springframework.data.jpa.repository.config.EnableJpaRepositories; -import org.springframework.web.servlet.config.annotation.CorsRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -@EnableJpaRepositories @SpringBootApplication -@EnableJpaAuditing public class ErrorApplication { - public static void main(String[] args) { SpringApplication.run(ErrorApplication.class, args); } - - @Bean - public WebMvcConfigurer corsConfigurer() { - return new WebMvcConfigurer() { - @Override - public void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOrigins("http://localhost:5173");}}; - } -} \ No newline at end of file +} diff --git a/BE/error/src/main/java/com/example/demo/image/ImageController.java b/BE/error/src/main/java/com/example/demo/image/ImageController.java new file mode 100644 index 00000000..2bfefcba --- /dev/null +++ b/BE/error/src/main/java/com/example/demo/image/ImageController.java @@ -0,0 +1,27 @@ +package com.example.demo.image; + +import java.util.List; +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/image") +public class ImageController { + + private final ImageService imageService; + + public ImageController(ImageService imageService) { + this.imageService = imageService; + } + + @GetMapping + public List getProfileImages (){ + List profileImageList = imageService.getFileList("image0"); + return profileImageList; + } + +} diff --git a/BE/error/src/main/java/com/example/demo/image/ImageService.java b/BE/error/src/main/java/com/example/demo/image/ImageService.java new file mode 100644 index 00000000..e74cca34 --- /dev/null +++ b/BE/error/src/main/java/com/example/demo/image/ImageService.java @@ -0,0 +1,48 @@ +package com.example.demo.image; + +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.List; +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(); + + for (S3ObjectSummary objectSummary : objectSummaries) { + String key = objectSummary.getKey(); + if (!key.equals(directory + "/")) { + fileList.add("https://"+bucketName+".s3."+region+".amazonaws.com/" + key); + } + } + + Collections.shuffle(fileList); + return fileList.size() > 16 ? fileList.subList(0, 16) : fileList; + } +} diff --git a/BE/error/src/main/java/com/example/demo/image/StorageConfig.java b/BE/error/src/main/java/com/example/demo/image/StorageConfig.java new file mode 100644 index 00000000..927e8f66 --- /dev/null +++ b/BE/error/src/main/java/com/example/demo/image/StorageConfig.java @@ -0,0 +1,33 @@ +package com.example.demo.image; + +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +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("${cloud.aws.credentials.secretKey}") + 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/resources/application.properties b/BE/error/src/main/resources/application.properties index 862c00d2..bf804352 100644 --- a/BE/error/src/main/resources/application.properties +++ b/BE/error/src/main/resources/application.properties @@ -1,6 +1,26 @@ -spring.jpa.hibernate.ddl-auto=update -spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/example?serverTimezone=Asia/Seoul&characterEncoding=UTF-8 -spring.datasource.username=user -spring.datasource.password=UserPassword -spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQLDialect -cors.allow-origin.urls=true \ No newline at end of file +spring.application.name=error + +# H2 Database Configuration +spring.h2.console.enabled=true +spring.h2.console.path=/h2-console + +# DataSource Configuration +spring.datasource.username=sa +spring.datasource.url=jdbc:h2:mem:test;MODE=MYSQL;DB_CLOSE_DELAY=-1 +spring.datasource.driver-class-name=org.h2.Driver + +# JPA Configuration +spring.jpa.show-sql=true +spring.jpa.hibernate.ddl-auto=create +spring.jpa.properties.hibernate.format_sql=true +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect + +# Logging Configuration +logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE + +# AWS Configuration +cloud.aws.region.static=ap-northeast-2 +cloud.aws.stack.auto=false +cloud.aws.credentials.accessKey=${ACCESS_KEY} +cloud.aws.credentials.secretKey=${SECRET_KEY} +cloud.aws.s3.bucket=zepimage0 From 7bc6658e49444d37326eb5ce4247e973138e2635 Mon Sep 17 00:00:00 2001 From: SeoYeongU Date: Tue, 23 Jul 2024 13:48:44 +0900 Subject: [PATCH 2/3] =?UTF-8?q?refactor:=20id=EC=99=80=20image=20url?= =?UTF-8?q?=EC=9D=84=20=ED=95=A8=EA=BB=98=20=EB=B0=98=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/demo/image/ImageController.java | 6 +++--- .../java/com/example/demo/image/ImageService.java | 14 ++++++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/BE/error/src/main/java/com/example/demo/image/ImageController.java b/BE/error/src/main/java/com/example/demo/image/ImageController.java index 2bfefcba..b87b84c8 100644 --- a/BE/error/src/main/java/com/example/demo/image/ImageController.java +++ b/BE/error/src/main/java/com/example/demo/image/ImageController.java @@ -1,6 +1,7 @@ package com.example.demo.image; 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; @@ -19,9 +20,8 @@ public ImageController(ImageService imageService) { } @GetMapping - public List getProfileImages (){ - List profileImageList = imageService.getFileList("image0"); + public List> getProfileImages (){ + List> profileImageList = imageService.getFileList("styleganresult2"); return profileImageList; } - } diff --git a/BE/error/src/main/java/com/example/demo/image/ImageService.java b/BE/error/src/main/java/com/example/demo/image/ImageService.java index e74cca34..f74e2337 100644 --- a/BE/error/src/main/java/com/example/demo/image/ImageService.java +++ b/BE/error/src/main/java/com/example/demo/image/ImageService.java @@ -6,7 +6,9 @@ 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; @@ -25,20 +27,24 @@ public class ImageService { @Value("${cloud.aws.region.static}") private String region; - public List getFileList(String directory) { - List fileList = new ArrayList<>(); + public List> getFileList(String directory) { + List> fileList = new ArrayList<>(); ListObjectsV2Request listObjectsV2Request = new ListObjectsV2Request() .withBucketName(bucketName) - .withPrefix(directory+"/"); + .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 + "/")) { - fileList.add("https://"+bucketName+".s3."+region+".amazonaws.com/" + key); + Map fileInfo = new HashMap<>(); + fileInfo.put("id", id++); + fileInfo.put("url", "https://" + bucketName + ".s3." + region + ".amazonaws.com/" + key); + fileList.add(fileInfo); } } From d26138660a907a23736425cf9f4a0df88e4042bf Mon Sep 17 00:00:00 2001 From: SeoYeongU Date: Tue, 23 Jul 2024 14:47:45 +0900 Subject: [PATCH 3/3] =?UTF-8?q?refactor:=20url=20=EB=A6=AC=EC=86=8C?= =?UTF-8?q?=EC=8A=A4=20=EB=B3=B5=EC=88=98=ED=98=95=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=ED=91=9C=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/example/demo/image/ImageController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BE/error/src/main/java/com/example/demo/image/ImageController.java b/BE/error/src/main/java/com/example/demo/image/ImageController.java index b87b84c8..3dc89beb 100644 --- a/BE/error/src/main/java/com/example/demo/image/ImageController.java +++ b/BE/error/src/main/java/com/example/demo/image/ImageController.java @@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.RestController; @RestController -@RequestMapping("/api/image") +@RequestMapping("/api/images") public class ImageController { private final ImageService imageService;