Skip to content

Commit

Permalink
Merge pull request #51 from GDSC-Ewha-5th/revert-50-revert-44-LimYoun…
Browse files Browse the repository at this point in the history
…gseo

Revert "Revert "[임영서] 5주차 과제 - complete""
  • Loading branch information
yslim21 authored Dec 22, 2023
2 parents 1600298 + ac1f68a commit 6be9cda
Showing 1 changed file with 40 additions and 14 deletions.
54 changes: 40 additions & 14 deletions 5주차 Server S-Day 과제/S3Controller.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

package ServerStudy5Cloud.ServerStudy5Cloud.Controller;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
Expand All @@ -14,6 +16,7 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.io.IOException;
import java.net.URL;
Expand All @@ -23,7 +26,6 @@

@Controller
@RequiredArgsConstructor
@Slf4j
public class S3Controller {

private final AmazonS3 amazonS3;
Expand All @@ -34,27 +36,51 @@ public class S3Controller {
@GetMapping("/")
public String listFiles(Model model) {
//getUrl로 객체 URL 가져온 후, List<String>에 넣어 index.html에 반환하기
List<String> files = new ArrayList<>();

ListObjectsV2Result result = amazonS3.listObjectsV2(bucketName);
List<S3ObjectSummary> objects = result.getObjectSummaries();
//업로드할 URL 파일 목록을 저장할 리스트 생성
List<String> fileUrls = new ArrayList<>();

//S3 내의 객체 목록 가져오기
ObjectListing objectListing = amazonS3.listObjects(bucketName);
List<S3ObjectSummary> objectSummaries = objectListing.getObjectSummaries();

for(S3ObjectSummary os : objects){
files.add(amazonS3.getUrl(bucketName, os.getKey()).toString());
//log.info("{}",files.get(files.size()-1));
// 객체 URL 가져와서 리스트에 추가
for (S3ObjectSummary objectSummary : objectSummaries) {
String fileUrl = amazonS3.getUrl(bucketName, objectSummary.getKey()).toString();
fileUrls.add(fileUrl);
}

model.addAttribute("fileUrls", files);
// 모델에 URL 리스트 추가
model.addAttribute("fileUrls", fileUrls);

return "index";
}



@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException{

//putObject와 setObjectAcl로 이미지 업로드하고 ACL 퍼블릭으로 만들기
amazonS3.putObject(bucketName, file.getOriginalFilename(), file.getInputStream(), null);
amazonS3.setObjectAcl(bucketName,file.getOriginalFilename(), CannedAccessControlList.PublicRead);
//파일 이름가져오기
String originalFilename = file.getOriginalFilename();

//메타데이터 설정
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(file.getSize());
metadata.setContentType(file.getContentType());

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

//업로드한 객체에 ACL퍼블릭 설정
amazonS3.setObjectAcl(bucketName, originalFilename, CannedAccessControlList.PublicRead);

return "redirect:/";


}
}
}

//https://drive.google.com/file/d/1o9uRjxExZHYJOCKX6aTS8y8KKc-vic0d/view?usp=sharing

0 comments on commit 6be9cda

Please sign in to comment.