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" new file mode 100644 index 0000000..1187e15 --- /dev/null +++ "b/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/S3Controller.java" @@ -0,0 +1,60 @@ +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.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; +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.File; +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + + +@Controller +@RequiredArgsConstructor +@Slf4j +public class S3Controller { + + private final AmazonS3 amazonS3; + + @Value("${cloud.aws.s3.bucket}") + private String bucketName; + + @GetMapping("/") + public String listFiles(Model model) { + //getUrl로 객체 URL 가져온 후, List에 넣어 index.html에 반환하기 + List files = new ArrayList<>(); + + ListObjectsV2Result result = amazonS3.listObjectsV2(bucketName); + List objects = result.getObjectSummaries(); + + for(S3ObjectSummary os : objects){ + files.add(amazonS3.getUrl(bucketName, os.getKey()).toString()); + //log.info("{}",files.get(files.size()-1)); + } + + model.addAttribute("fileUrls", files); + return "index"; + } + + @PostMapping("/upload") + 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); + return "redirect:/"; + + } +} \ 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\204\234\354\247\200\354\233\220_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\204\234\354\247\200\354\233\220_5\354\243\274\354\260\250_\352\263\274\354\240\234.md" new file mode 100644 index 0000000..a41906a --- /dev/null +++ "b/5\354\243\274\354\260\250 Server S-Day \352\263\274\354\240\234/\354\204\234\354\247\200\354\233\220_5\354\243\274\354\260\250_\352\263\274\354\240\234.md" @@ -0,0 +1,19 @@ +1. IAM에서 AmazonS3FullAccess 권한을 가진 사용자 생성 -> application.yml의 accessKey와 secretKey에 입력
+2. ```getUrl```로 객체 URL 가져온 후, List에 넣어 index.html에 반환하기
+ a. ```listObjects```메서드로 버킷의 ```ObjectListing```객체에 대한 정보를 제공하는 객체를 반환
+ b. ```getObjectSummaries``` 메서드를 사용하여 각 객체가 버킷의 단일 ```ObjectSummary``` 객체를 나타내는 S3 객체 목록을 가져옴
+ c . 반복문을 돌며 summary에서 key 가져와 url을 list에 저장
+
+⚠️발생한 문제 + +![사진 안뜸](https://github.com/GDSC-Ewha-5th/GDSC-Server-5th/assets/63919973/0191904f-9bb4-4e96-97c3-51ba5a0cc96b) +- 사진이 안뜸->버킷 읽기 권한이 없기 때문이었음 + ![access denied](https://github.com/GDSC-Ewha-5th/GDSC-Server-5th/assets/63919973/eb333e7d-5f10-46d3-94b4-8b128a0af747) +- 버킷 권한 설정 + ![image](https://github.com/GDSC-Ewha-5th/GDSC-Server-5th/assets/63919973/9abcb391-b84c-4880-b698-a75374f8b235) + +------>해결
+3. putObject와 setObjectAcl로 이미지 업로드하고 ACL 퍼블릭으로 만들기 + +[화면] +![image](https://github.com/GDSC-Ewha-5th/GDSC-Server-5th/assets/63919973/062ed883-ecde-4f1c-9d98-e014b7eb3dca)