-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from GDSC-Ewha-5th/JoongHyunKim
[김중현] 5주차 과제 - complete
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:/"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |