Skip to content

Commit

Permalink
Merge pull request #42 from GDSC-Ewha-5th/JeewonSeo
Browse files Browse the repository at this point in the history
[서지원] 5주차 과제 -complete
  • Loading branch information
seojeewon authored Nov 18, 2023
2 parents 8cd64b8 + a48acf3 commit ebccc3c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
60 changes: 60 additions & 0 deletions 5주차 Server S-Day 과제/S3Controller.java
Original file line number Diff line number Diff line change
@@ -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<String>에 넣어 index.html에 반환하기
List<String> files = new ArrayList<>();

ListObjectsV2Result result = amazonS3.listObjectsV2(bucketName);
List<S3ObjectSummary> 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:/";

}
}
19 changes: 19 additions & 0 deletions 5주차 Server S-Day 과제/서지원_5주차_과제.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
1. IAM에서 AmazonS3FullAccess 권한을 가진 사용자 생성 -> application.yml의 accessKey와 secretKey에 입력<br>
2. ```getUrl```로 객체 URL 가져온 후, List<String>에 넣어 index.html에 반환하기<br>
a. ```listObjects```메서드로 버킷의 ```ObjectListing```객체에 대한 정보를 제공하는 객체를 반환<br>
b. ```getObjectSummaries``` 메서드를 사용하여 각 객체가 버킷의 단일 ```ObjectSummary``` 객체를 나타내는 S3 객체 목록을 가져옴<br>
c . 반복문을 돌며 summary에서 key 가져와 url을 list에 저장<br>
<br>
⚠️발생한 문제

![사진 안뜸](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)

------>해결<br>
3. putObject와 setObjectAcl로 이미지 업로드하고 ACL 퍼블릭으로 만들기

[화면]
![image](https://github.com/GDSC-Ewha-5th/GDSC-Server-5th/assets/63919973/062ed883-ecde-4f1c-9d98-e014b7eb3dca)

0 comments on commit ebccc3c

Please sign in to comment.