From 265a11a0fbf84466422763ae97e2297545907def Mon Sep 17 00:00:00 2001 From: Lee junbeom Date: Fri, 24 May 2024 19:22:30 +0900 Subject: [PATCH 1/2] =?UTF-8?q?S3=20=EA=B8=B0=EB=B3=B8=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + .../com/example/fiurinee/S3Controller.java | 30 +++++++++++++++++ .../com/example/fiurinee/StorageConfig.java | 32 +++++++++++++++++++ src/main/resources/application.yml | 14 +++++++- 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/example/fiurinee/S3Controller.java create mode 100644 src/main/java/com/example/fiurinee/StorageConfig.java diff --git a/build.gradle b/build.gradle index b33a905..fe210a7 100644 --- a/build.gradle +++ b/build.gradle @@ -25,6 +25,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' // implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE' compileOnly 'org.projectlombok:lombok' runtimeOnly 'org.postgresql:postgresql' annotationProcessor 'org.projectlombok:lombok' diff --git a/src/main/java/com/example/fiurinee/S3Controller.java b/src/main/java/com/example/fiurinee/S3Controller.java new file mode 100644 index 0000000..c6c6127 --- /dev/null +++ b/src/main/java/com/example/fiurinee/S3Controller.java @@ -0,0 +1,30 @@ +package com.example.fiurinee; + +import com.amazonaws.services.s3.AmazonS3; +import lombok.AllArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import java.lang.String; + +import java.net.URL; + +@RestController +public class S3Controller { + + private final AmazonS3 s3Client; + private final String bucketName; + + public S3Controller(AmazonS3 s3Client, @Value("${cloud.aws.s3.bucket}") String bucketName) { + this.s3Client = s3Client; + this.bucketName = bucketName; + } + + @GetMapping("/image") + public ResponseEntity getImage(){ + URL img = s3Client.getUrl(bucketName, "fiurinnn"); + + return ResponseEntity.ok(img); + } +} diff --git a/src/main/java/com/example/fiurinee/StorageConfig.java b/src/main/java/com/example/fiurinee/StorageConfig.java new file mode 100644 index 0000000..2407ce2 --- /dev/null +++ b/src/main/java/com/example/fiurinee/StorageConfig.java @@ -0,0 +1,32 @@ +package com.example.fiurinee; + +import com.amazonaws.auth.AWSCredentials; +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.services.s3.AmazonS3; +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 +public class StorageConfig { + + @Value("${cloud.aws.credentials.accessKey}") + private String accessKey; + + @Value("${cloud.aws.credentials.secretKey}") + private String accessSecret; + @Value("${cloud.aws.region.static}") + private String region; + + @Bean + public AmazonS3 s3Client() { + AWSCredentials credentials = new BasicAWSCredentials(accessKey, accessSecret); + return AmazonS3ClientBuilder.standard() + .withCredentials(new AWSStaticCredentialsProvider(credentials)) + .withRegion(region).build(); + } + +} + diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 2612932..6ac1358 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -14,4 +14,16 @@ spring: logging.level: org.hibernate.SQL: debug - org.hibernate.type: trace \ No newline at end of file + org.hibernate.type: trace + +cloud: + aws: + credentials: + accessKey: ${cloud.aws.credentials.accessKey} + secretKey: ${cloud.aws.credentials.secretKey} + s3: + bucket: ${cloud.aws.s3.bucket} + region: + static: ap-northeast-2 + stack: + auto: 'false' \ No newline at end of file From 6e83900235cbc13a413184436b553d92518782d1 Mon Sep 17 00:00:00 2001 From: Lee junbeom Date: Sat, 25 May 2024 23:06:24 +0900 Subject: [PATCH 2/2] =?UTF-8?q?S3=20=ED=99=98=EA=B2=BD=EB=B3=80=EC=88=98?= =?UTF-8?q?=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/fiurinee/S3Controller.java | 2 +- src/main/java/com/example/fiurinee/StorageConfig.java | 4 ++-- src/main/resources/application.yml | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/example/fiurinee/S3Controller.java b/src/main/java/com/example/fiurinee/S3Controller.java index c6c6127..5212e70 100644 --- a/src/main/java/com/example/fiurinee/S3Controller.java +++ b/src/main/java/com/example/fiurinee/S3Controller.java @@ -16,7 +16,7 @@ public class S3Controller { private final AmazonS3 s3Client; private final String bucketName; - public S3Controller(AmazonS3 s3Client, @Value("${cloud.aws.s3.bucket}") String bucketName) { + public S3Controller(AmazonS3 s3Client, @Value("${AWS_BUCKET}") String bucketName) { this.s3Client = s3Client; this.bucketName = bucketName; } diff --git a/src/main/java/com/example/fiurinee/StorageConfig.java b/src/main/java/com/example/fiurinee/StorageConfig.java index 2407ce2..ada5b6b 100644 --- a/src/main/java/com/example/fiurinee/StorageConfig.java +++ b/src/main/java/com/example/fiurinee/StorageConfig.java @@ -12,10 +12,10 @@ @Configuration public class StorageConfig { - @Value("${cloud.aws.credentials.accessKey}") + @Value("${AWS_ACCESSKEY}") private String accessKey; - @Value("${cloud.aws.credentials.secretKey}") + @Value("${AWS_SECRETKEY}") private String accessSecret; @Value("${cloud.aws.region.static}") private String region; diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 6ac1358..9f6a1fc 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -19,10 +19,10 @@ logging.level: cloud: aws: credentials: - accessKey: ${cloud.aws.credentials.accessKey} - secretKey: ${cloud.aws.credentials.secretKey} + accessKey: ${AWS_ACCESSKEY} + secretKey: ${AWS_SECRETKEY} s3: - bucket: ${cloud.aws.s3.bucket} + bucket: ${AWS_BUCKET} region: static: ap-northeast-2 stack: