From 2eeb60072b2cbea85c4741fc7010148e576718cf Mon Sep 17 00:00:00 2001 From: leeseohyun Date: Sat, 18 Nov 2023 16:23:58 +0900 Subject: [PATCH] =?UTF-8?q?[=EC=9D=B4=EC=84=9C=ED=98=84]=205=EC=A3=BC?= =?UTF-8?q?=EC=B0=A8=20=EA=B3=BC=EC=A0=9C=20-=20ing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../files_leeseohyun/S3Controller.java" | 53 +++++++++++++++++++ .../files_leeseohyun/application.yml" | 15 ++++++ ...4\354\260\250_\352\263\274\354\240\234.md" | 0 3 files changed, 68 insertions(+) create mode 100644 "5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/files_leeseohyun/S3Controller.java" create mode 100644 "5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/files_leeseohyun/application.yml" create mode 100644 "5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/\354\235\264\354\204\234\355\230\204_5\354\243\274\354\260\250_\352\263\274\354\240\234.md" diff --git "a/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/files_leeseohyun/S3Controller.java" "b/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/files_leeseohyun/S3Controller.java" new file mode 100644 index 0000000..c08ae6e --- /dev/null +++ "b/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/files_leeseohyun/S3Controller.java" @@ -0,0 +1,53 @@ +package ServerStudy5Cloud.ServerStudy5Cloud.Controller; + +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.model.CannedAccessControlList; +import com.amazonaws.services.s3.model.S3ObjectSummary; +import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + + +@Controller +@RequiredArgsConstructor +public class S3Controller { + + private final AmazonS3 amazonS3; + + @Value("${cloud.aws.s3.bucket}") + private String bucketName; + + @GetMapping("/") + public String listFiles(Model model) { + // S3 버킷의 객체 목록 가져오기 + List objectSummaries = amazonS3.listObjectsV2(bucketName).getObjectSummaries(); + // getUrl로 객체 URL 가져온 후, List에 넣어 index.html에 반환 + List fileUrls = new ArrayList<>(); + for (S3ObjectSummary os : objectSummaries) { + String url = amazonS3.getUrl(bucketName, os.getKey()).toString(); + fileUrls.add(url); + } + model.addAttribute("fileUrls", fileUrls); + return "index"; + } + + @PostMapping("/upload") + public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException { + String filename = file.getOriginalFilename(); + // putObject로 파일을 S3 버킷에 업로드 + amazonS3.putObject(bucketName, filename, file.getInputStream(), null); + // ACL 퍼블릭으로 설정 + amazonS3.setObjectAcl(bucketName, filename, CannedAccessControlList.PublicRead); + return "redirect:/"; + } +} +// 동작 영상 : https://drive.google.com/file/d/10wPjsBbnbIR5E6pKbwmr2sZ-GsLY-FWP/view?usp=sharing \ No newline at end of file diff --git "a/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/files_leeseohyun/application.yml" "b/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/files_leeseohyun/application.yml" new file mode 100644 index 0000000..4b7489c --- /dev/null +++ "b/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/files_leeseohyun/application.yml" @@ -0,0 +1,15 @@ +spring: + config: + import: optional:file:.env[.properties] +# 이렇게 .env 파일을 사용하려고 했지만 잘 동작하지 않아서... 빌드 Edit Configuration에서 Environment variables로 설정해서 했습니다...! +cloud: + aws: + s3: + bucket: ${S3_BUCKET} + stack.auto: false + region.static: ap-northeast-2 + credentials: + access-key: ${CREDENTIALS_ACCESS_KEY} + secret-key: ${CREDENTIALS_SECRET_KEY} +server: + port: 8080 \ No newline at end of file diff --git "a/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/\354\235\264\354\204\234\355\230\204_5\354\243\274\354\260\250_\352\263\274\354\240\234.md" "b/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/\354\235\264\354\204\234\355\230\204_5\354\243\274\354\260\250_\352\263\274\354\240\234.md" new file mode 100644 index 0000000..e69de29