Skip to content

Commit

Permalink
Merge pull request #45 from GDSC-Ewha-5th/JoongHyunKim
Browse files Browse the repository at this point in the history
[김중현] 5주차 과제  - complete
  • Loading branch information
JoongHyun-Kim authored Dec 22, 2023
2 parents 6be9cda + de57a53 commit 2f6bbfe
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
56 changes: 56 additions & 0 deletions 5주차 Server S-Day 과제/S3Controller_JoongHyunKim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ServerStudy5Cloud.ServerStudy5Cloud.Controller;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.*;
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.List;
import java.util.stream.Collectors;

/**
* 영상 인증: https://drive.google.com/file/d/1aoKdLbbNA8EHkJmxeYoLGfrx0gwYSfdg/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) {
// 버킷의 object list 조회
ObjectListing objectListing = amazonS3.listObjects(new ListObjectsRequest().withBucketName(bucketName));

// object list에서 각 object의 url을 조회해 list 생성
List<String> urls = objectListing.getObjectSummaries().stream()
.map(os -> amazonS3.getUrl(bucketName, os.getKey()).toString())
.collect(Collectors.toList());
model.addAttribute("urls", urls); // 모델에 추가해 뷰로 전달

return "index";
}

@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
// 전송한 파일 이름 조회
String fileName = file.getOriginalFilename();

// 버킷에 파일 업로드
amazonS3.putObject(new PutObjectRequest(bucketName, fileName, file.getInputStream(), null));
// 파일의 접근 권한을 public으로 설정
amazonS3.setObjectAcl(bucketName, fileName, CannedAccessControlList.PublicRead);

return "redirect:/";
}
}
9 changes: 9 additions & 0 deletions 5주차 Server S-Day 과제/application_JoongHyunKim.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cloud:
aws:
s3:
bucket: gdsc-server-prac
stack.auto: false
region.static: ap-northeast-2
credentials:
access-key: ${AWS_ACCESS_KEY}
secret-key: ${AWS_SECRET_KEY}
29 changes: 29 additions & 0 deletions 5주차 Server S-Day 과제/index_JoongHyunKim.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" th:href="@{/css/style.css}">
<title>GDSC Server</title>
</head>
<body>

<h2>AWS S3에 파일을 업로드해봐요!</h2>

<form method="post" action="/upload" enctype="multipart/form-data">
<input class="btn-gradient blue mini" type="file" name="file" />
<button class="btn-gradient blue mini" type="submit">Upload</button>
</form>

<h2>AWS S3의 파일을 확인해봐요!</h2>

<table>
<tr th:each="url : ${urls}">
<td>
<img th:src="${url}" style="max-height: 300px; overflow: hidden;">
</td>
</tr>
</table>

</body>
</html>

0 comments on commit 2f6bbfe

Please sign in to comment.