Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[김리나] 5주차 과제 - complete #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions 5주차 Server S-Day 과제/S3Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package ServerStudy5Cloud.ServerStudy5Cloud.Controller;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectListing;
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) {
//getUrl로 객체 URL 가져온 후, List<String>에 넣어 index.html에 반환하기
ObjectListing objectListing = amazonS3.listObjects(bucketName);
List<S3ObjectSummary> s3objects = objectListing.getObjectSummaries();

List<String> imagesURL = new ArrayList<>();

for(S3ObjectSummary ob : s3objects){
String objectKey = ob.getKey();
imagesURL.add(amazonS3.getUrl(bucketName, objectKey).toString());
}

model.addAttribute("fileUrls", imagesURL);

return "index";
}

@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException{
//putObject와 setObjectAcl로 이미지 업로드하고 ACL 퍼블릭으로 만들기

//s3에 파일 업로드
amazonS3.putObject(bucketName, file.getOriginalFilename(), file.getInputStream(), null);
//업로드한 객체 ACL 설정
amazonS3.setObjectAcl(bucketName, file.getOriginalFilename(), CannedAccessControlList.PublicRead);

return "redirect:/";
}
}
23 changes: 23 additions & 0 deletions 5주차 Server S-Day 과제/김리나_5주차_과제.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## 📌 5주차 Server session 기본 과제
- S3에 객체 업로드가 가능한 `@PostMapping` 구현하기
- S3 객체 조회가 가능한 `@GetMapping` 구현하기

<br><br>

## ❓ 해결과정

### 1. 사진 리스트 가져오기
1. AmazonS3를 사용하여 S3 버킷의 객체 목록을 가져오는 ObjectListing 객체 생성하기
2. ObjectListing에서 가져온 객체 목록을 S3ObjectSummary 리스트로 저장하기
3. URL을 저장할 문자열 리스트 imagesURL 생성
4. for문을 이용해 객체 목록에서 각 객체의 정보를 가져오기
5. 객체의 key값을 이용해 객체의 url 가져와서 imagesURL 리스트에 추가
<br><br>

### 2️. 사진 업로드하기
1. S3에 파일 업로드하기
2. ACL객체 권한을 Public read로 설정하
<br><br>

## ❗ 과제 인증
https://drive.google.com/file/d/1cXQHfDU7yk9nmS88daIkPFKEamGRDNeG/view?usp=sharing