Skip to content

Commit

Permalink
Merge pull request #50 from GDSC-Ewha-5th/revert-44-LimYoungseo
Browse files Browse the repository at this point in the history
Revert "[임영서] 5주차 과제 - complete"
  • Loading branch information
yslim21 authored Dec 22, 2023
2 parents b537efd + 8e0494d commit 1600298
Showing 1 changed file with 14 additions and 40 deletions.
54 changes: 14 additions & 40 deletions 5주차 Server S-Day 과제/S3Controller.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@

package ServerStudy5Cloud.ServerStudy5Cloud.Controller;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
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 @@ -16,7 +14,6 @@
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 @@ -26,6 +23,7 @@

@Controller
@RequiredArgsConstructor
@Slf4j
public class S3Controller {

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

//업로드할 URL 파일 목록을 저장할 리스트 생성
List<String> fileUrls = new ArrayList<>();

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

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

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

model.addAttribute("fileUrls", files);
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 퍼블릭으로 만들기
//파일 이름가져오기
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);

amazonS3.putObject(bucketName, file.getOriginalFilename(), file.getInputStream(), null);
amazonS3.setObjectAcl(bucketName,file.getOriginalFilename(), CannedAccessControlList.PublicRead);
return "redirect:/";


}
}

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

}

0 comments on commit 1600298

Please sign in to comment.