From ac1f68ae4394f05809aa29d884e192a1c3aae2bb Mon Sep 17 00:00:00 2001 From: yslim21 <121086341+yslim21@users.noreply.github.com> Date: Fri, 22 Dec 2023 09:34:37 +0900 Subject: [PATCH] =?UTF-8?q?Revert=20"Revert=20"[=EC=9E=84=EC=98=81?= =?UTF-8?q?=EC=84=9C]=205=EC=A3=BC=EC=B0=A8=20=EA=B3=BC=EC=A0=9C=20-=20com?= =?UTF-8?q?plete""?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../S3Controller.java" | 54 ++++++++++++++----- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git "a/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/S3Controller.java" "b/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/S3Controller.java" index 1187e15..bca173a 100644 --- "a/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/S3Controller.java" +++ "b/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/S3Controller.java" @@ -1,11 +1,13 @@ + package ServerStudy5Cloud.ServerStudy5Cloud.Controller; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.CannedAccessControlList; -import com.amazonaws.services.s3.model.ListObjectsV2Result; +import com.amazonaws.services.s3.model.ObjectListing; +import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.S3ObjectSummary; import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; + import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -14,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; + import java.io.File; import java.io.IOException; import java.net.URL; @@ -23,7 +26,6 @@ @Controller @RequiredArgsConstructor -@Slf4j public class S3Controller { private final AmazonS3 amazonS3; @@ -34,27 +36,51 @@ public class S3Controller { @GetMapping("/") public String listFiles(Model model) { //getUrl로 객체 URL 가져온 후, List에 넣어 index.html에 반환하기 - List files = new ArrayList<>(); - ListObjectsV2Result result = amazonS3.listObjectsV2(bucketName); - List objects = result.getObjectSummaries(); + //업로드할 URL 파일 목록을 저장할 리스트 생성 + List fileUrls = new ArrayList<>(); + + //S3 내의 객체 목록 가져오기 + ObjectListing objectListing = amazonS3.listObjects(bucketName); + List objectSummaries = objectListing.getObjectSummaries(); - for(S3ObjectSummary os : objects){ - files.add(amazonS3.getUrl(bucketName, os.getKey()).toString()); - //log.info("{}",files.get(files.size()-1)); + // 객체 URL 가져와서 리스트에 추가 + for (S3ObjectSummary objectSummary : objectSummaries) { + String fileUrl = amazonS3.getUrl(bucketName, objectSummary.getKey()).toString(); + fileUrls.add(fileUrl); } - model.addAttribute("fileUrls", files); + // 모델에 URL 리스트 추가 + model.addAttribute("fileUrls", fileUrls); + return "index"; } + + @PostMapping("/upload") - public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException { + public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException{ //putObject와 setObjectAcl로 이미지 업로드하고 ACL 퍼블릭으로 만들기 - amazonS3.putObject(bucketName, file.getOriginalFilename(), file.getInputStream(), null); - amazonS3.setObjectAcl(bucketName,file.getOriginalFilename(), CannedAccessControlList.PublicRead); + //파일 이름가져오기 + String originalFilename = file.getOriginalFilename(); + + //메타데이터 설정 + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentLength(file.getSize()); + metadata.setContentType(file.getContentType()); + + // putObject로 s3에 파일 업로드 + amazonS3.putObject(bucketName, originalFilename, file.getInputStream(),metadata); + + //업로드한 객체에 ACL퍼블릭 설정 + amazonS3.setObjectAcl(bucketName, originalFilename, CannedAccessControlList.PublicRead); + return "redirect:/"; + } -} \ No newline at end of file +} + +//https://drive.google.com/file/d/1o9uRjxExZHYJOCKX6aTS8y8KKc-vic0d/view?usp=sharing +