Skip to content

Commit

Permalink
[이서현] 5주차 과제 - ing
Browse files Browse the repository at this point in the history
  • Loading branch information
seohyun-lee committed Nov 18, 2023
1 parent 4ed2199 commit 2eeb600
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
53 changes: 53 additions & 0 deletions 5주차 Server S-Day 과제/files_leeseohyun/S3Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ServerStudy5Cloud.ServerStudy5Cloud.Controller;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.CannedAccessControlList;
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) {
// S3 버킷의 객체 목록 가져오기
List<S3ObjectSummary> objectSummaries = amazonS3.listObjectsV2(bucketName).getObjectSummaries();
// getUrl로 객체 URL 가져온 후, List<String>에 넣어 index.html에 반환
List<String> fileUrls = new ArrayList<>();
for (S3ObjectSummary os : objectSummaries) {
String url = amazonS3.getUrl(bucketName, os.getKey()).toString();
fileUrls.add(url);
}
model.addAttribute("fileUrls", fileUrls);
return "index";
}

@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
String filename = file.getOriginalFilename();
// putObject로 파일을 S3 버킷에 업로드
amazonS3.putObject(bucketName, filename, file.getInputStream(), null);
// ACL 퍼블릭으로 설정
amazonS3.setObjectAcl(bucketName, filename, CannedAccessControlList.PublicRead);
return "redirect:/";
}
}
// 동작 영상 : https://drive.google.com/file/d/10wPjsBbnbIR5E6pKbwmr2sZ-GsLY-FWP/view?usp=sharing
15 changes: 15 additions & 0 deletions 5주차 Server S-Day 과제/files_leeseohyun/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
spring:
config:
import: optional:file:.env[.properties]
# 이렇게 .env 파일을 사용하려고 했지만 잘 동작하지 않아서... 빌드 Edit Configuration에서 Environment variables로 설정해서 했습니다...!
cloud:
aws:
s3:
bucket: ${S3_BUCKET}
stack.auto: false
region.static: ap-northeast-2
credentials:
access-key: ${CREDENTIALS_ACCESS_KEY}
secret-key: ${CREDENTIALS_SECRET_KEY}
server:
port: 8080
Empty file.

0 comments on commit 2eeb600

Please sign in to comment.