Skip to content

Commit

Permalink
feat: s3 버킷 이미지 업로드 테스트 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
eckrin committed Nov 10, 2023
1 parent aba2946 commit 2b6ceb0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kusitms.jipbap.common;
package com.kusitms.jipbap.common.utils;

import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.kusitms.jipbap.store;

import com.kusitms.jipbap.common.QueryDslUtil;
import com.kusitms.jipbap.common.utils.QueryDslUtil;
import com.kusitms.jipbap.store.dto.StoreDetailResponseDto;
import com.kusitms.jipbap.store.dto.StoreDto;
import com.kusitms.jipbap.user.User;
Expand All @@ -17,7 +17,6 @@
import java.util.ArrayList;
import java.util.List;

import static com.kusitms.jipbap.store.QStore.*;
import static com.kusitms.jipbap.store.QStore.store;
import static org.springframework.util.ObjectUtils.isEmpty;

Expand Down
46 changes: 35 additions & 11 deletions src/main/java/com/kusitms/jipbap/test/TestController.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
package com.kusitms.jipbap.test;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.kusitms.jipbap.auth.exception.InvalidEmailException;
import com.kusitms.jipbap.common.response.CommonResponse;
import com.kusitms.jipbap.security.Auth;
import com.kusitms.jipbap.security.AuthInfo;
import com.kusitms.jipbap.user.User;
import com.kusitms.jipbap.user.UserRepository;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
@Slf4j
public class TestController {

private final UserRepository userRepository;
private final AmazonS3 amazonS3;

@Value("${cloud.aws.s3.bucket}")
private String bucket;

/**
* 커넥션 테스트용 컨트롤러
Expand All @@ -36,16 +48,28 @@ public String test2(@Auth AuthInfo authInfo) {
return "Healthy Connection";
}

@Operation(summary = "s3 이미지 저장 테스트")
@Operation(summary = "s3 이미지 저장, 조회 테스트용 컨트롤러")
@PostMapping("/image")
public String saveImageTest() {
return null;
public String saveImageTest(@RequestParam("file") MultipartFile file) {
try {
return saveFile(file);
} catch(IOException e) {
log.info("s3 이미지 저장 테스트 중 IOException 발생");
return null;
}
}

@Operation(summary = "s3 이미지 조회 테스트")
@GetMapping("/image")
public String getImageTest() {
return null;

// 파일 저장 후 uri 반환
private String saveFile(MultipartFile multipartFile) throws IOException {
String originalFilename = multipartFile.getOriginalFilename();

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(multipartFile.getSize());
metadata.setContentType(multipartFile.getContentType());

amazonS3.putObject(bucket, originalFilename, multipartFile.getInputStream(), metadata);
return amazonS3.getUrl(bucket, originalFilename).toString();
}

}

0 comments on commit 2b6ceb0

Please sign in to comment.