Skip to content

Commit

Permalink
Merge pull request #41 from GDSC-Ewha-5th/YangDongseon
Browse files Browse the repository at this point in the history
[양동선] 5주차 과제 -complete
  • Loading branch information
dongseon0 authored Nov 18, 2023
2 parents c2a885c + b5c06e8 commit 3c8e791
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
66 changes: 66 additions & 0 deletions 5주차 Sever S-Day 과제/S3Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ServerStudy5Cloud.ServerStudy5Cloud.Controller;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
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;

//영상 링크 : https://drive.google.com/file/d/17tmgRlWb4g0ZRuUKt6pvBlmmw-NqpDHR/view?usp=sharing
@Controller
@RequiredArgsConstructor
public class S3Controller {

private final AmazonS3 amazonS3;

@Value("${cloud.aws.s3.bucket}")
private String bucketName;

@GetMapping("/")
public String listFiles(Model model) {
List<String> fileUrls = new ArrayList<>();
List<S3ObjectSummary> objectSummaries = amazonS3.listObjects(bucketName).getObjectSummaries();
// 객체 URL을 리스트에 추가
for (S3ObjectSummary objectSummary : objectSummaries) {
fileUrls.add(amazonS3.getUrl(bucketName, objectSummary.getKey()).toString());
}
// 모델에 URL 리스트 추가
model.addAttribute("fileUrls", fileUrls);
return "index";
}

@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file) {
try {
/// ObjectMetadata 인스턴스 생성
ObjectMetadata metadata = new ObjectMetadata();

// content type (MIME type)세팅
metadata.setContentType(file.getContentType());

// S3에 파일 업로드
amazonS3.putObject(bucketName, file.getOriginalFilename(), file.getInputStream(), metadata);

// ACL를 public-read로
amazonS3.setObjectAcl(bucketName, file.getOriginalFilename(), CannedAccessControlList.PublicRead);


return "redirect:/";
} catch (IOException e) {
// Handle the exception appropriately (e.g., log it or show an error message)
return "error";
}

}
}
11 changes: 11 additions & 0 deletions 5주차 Sever S-Day 과제/양동선_5주차_과제.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## 과정
1. 인스턴스 생성(EC2)
2. 보안자격증명 > 키 생성
3. index.html과 S3Controller 파일 수정
4. main 메소드를 실행한 후 결과 확인 !

## 난관
해결 : test를 돌리는데 안돼서 당황했지만, 코어멤버 혜승님께서 main 메소드를 돌리는 것을 알려주셨다!! 서비스를 구동하려면 main메소드를 돌리는 것이구나..

귀여운 미리보기
<img width="348" alt="스크린샷 2023-11-17 오후 10 54 53" src="https://github.com/GDSC-Ewha-5th/GDSC-Server-5th/assets/78548833/b9f72842-ccfe-4d8d-b42d-2232edf27d5c">

0 comments on commit 3c8e791

Please sign in to comment.