From 2575034718ac9aa60aede112192b8d139d1c8907 Mon Sep 17 00:00:00 2001 From: minjoon-98 <4kmj54321@gmail.com> Date: Tue, 9 Jul 2024 20:18:36 +0900 Subject: [PATCH 1/2] feat : AWS S3 upload --- .../namanmoo/controller/MemberController.java | 20 +++++++--------- .../namanmoo/service/AwsS3Service.java | 24 ++++++++++++++----- .../namanmoo/service/MemberService.java | 5 +++- .../namanmoo/service/MemberServiceImpl.java | 14 +++++++---- 4 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/main/java/ongjong/namanmoo/controller/MemberController.java b/src/main/java/ongjong/namanmoo/controller/MemberController.java index aee5122..78a4adf 100644 --- a/src/main/java/ongjong/namanmoo/controller/MemberController.java +++ b/src/main/java/ongjong/namanmoo/controller/MemberController.java @@ -11,6 +11,7 @@ import ongjong.namanmoo.response.ApiResponse; import ongjong.namanmoo.service.AwsS3Service; import ongjong.namanmoo.service.MemberService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -19,6 +20,7 @@ import java.util.HashMap; import java.util.Map; +import java.util.Optional; @Slf4j @RestController @@ -56,27 +58,23 @@ public ResponseEntity>> duplicate(@Valid @Reque // 회원 정보 수정 @PostMapping("/users") @ResponseStatus(HttpStatus.OK) - public ApiResponse updateBasicInfo( - @RequestPart("userInfo") MemberUpdateDto memberUpdateDto, - @RequestPart(value = "userImg", required = false) MultipartFile userImg) throws Exception { + public ApiResponse updateBasicInfo(@RequestPart("userInfo") MemberUpdateDto memberUpdateDto, + @RequestPart("userImg") Optional userImg) throws Exception { - log.debug("Received MemberUpdateDto: {}", memberUpdateDto); - log.debug("Received MultipartFile: {}", userImg); - - String uploadImageUrl = null; + log.info("Received MemberUpdateDto: {}", memberUpdateDto); + log.info("Received MultipartFile: {}", userImg); // 파일을 전송했을 경우에만 S3 파일 업로드 수행 - if (userImg != null && !userImg.isEmpty()) { + if (userImg.isPresent() && !userImg.get().isEmpty()) { log.debug("Uploading file to S3..."); - uploadImageUrl = awsS3Service.uploadFile(userImg); + String uploadImageUrl = awsS3Service.uploadFile(userImg.get()); log.debug("File uploaded to S3: {}", uploadImageUrl); } log.debug("Updating member information..."); - memberService.update(memberUpdateDto); + memberService.update(memberUpdateDto, userImg); log.debug("Member information updated."); - // MemberService의 update 메소드에서 imagePath를 갱신해야 합니다. MemberInfoDto info = memberService.getMyInfo(); log.debug("Member info retrieved: {}", info); return new ApiResponse<>("200", "Update User Info Success", info); diff --git a/src/main/java/ongjong/namanmoo/service/AwsS3Service.java b/src/main/java/ongjong/namanmoo/service/AwsS3Service.java index 81d5acd..4ad31e7 100644 --- a/src/main/java/ongjong/namanmoo/service/AwsS3Service.java +++ b/src/main/java/ongjong/namanmoo/service/AwsS3Service.java @@ -6,7 +6,10 @@ import java.util.Optional; import java.util.UUID; +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3ClientBuilder; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -20,15 +23,24 @@ import lombok.RequiredArgsConstructor; @Slf4j -@RequiredArgsConstructor @Service public class AwsS3Service { - @Autowired - private AmazonS3 amazonS3Client; - - @Value("${cloud.aws.s3.bucket}") - private String bucket; + private final AmazonS3 amazonS3Client; + private final String bucket; + + public AwsS3Service( + @Value("${aws.accessKeyId}") String accessKeyId, + @Value("${aws.secretKey}") String secretKey, + @Value("${cloud.aws.s3.bucket}") String bucket, + @Value("${aws.region}") String region) { + BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKeyId, secretKey); + this.amazonS3Client = AmazonS3ClientBuilder.standard() + .withRegion(region) + .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) + .build(); + this.bucket = bucket; + } public String uploadFile(MultipartFile multipartFile) throws IOException { log.debug("Converting MultipartFile to File..."); diff --git a/src/main/java/ongjong/namanmoo/service/MemberService.java b/src/main/java/ongjong/namanmoo/service/MemberService.java index 96e56a0..a199776 100644 --- a/src/main/java/ongjong/namanmoo/service/MemberService.java +++ b/src/main/java/ongjong/namanmoo/service/MemberService.java @@ -6,6 +6,9 @@ import ongjong.namanmoo.dto.member.MemberSignUpDto; import ongjong.namanmoo.dto.member.MemberUpdateDto; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; + +import java.util.Optional; public interface MemberService { @@ -21,7 +24,7 @@ public interface MemberService { // 아이디 중복 체크 boolean isDuplicateId(LoginRequestDto loginRequestDto); - void update(MemberUpdateDto memberUpdateDto) throws Exception; + void update(MemberUpdateDto memberUpdateDto, Optional userImg) throws Exception; void updatePassword(String checkPassword, String toBePassword) throws Exception; diff --git a/src/main/java/ongjong/namanmoo/service/MemberServiceImpl.java b/src/main/java/ongjong/namanmoo/service/MemberServiceImpl.java index 3ed6e03..953681e 100644 --- a/src/main/java/ongjong/namanmoo/service/MemberServiceImpl.java +++ b/src/main/java/ongjong/namanmoo/service/MemberServiceImpl.java @@ -11,6 +11,7 @@ import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; import java.util.Optional; @@ -48,7 +49,7 @@ public boolean isDuplicateId(LoginRequestDto loginRequestDto) { // 회원 정보 수정 @Override - public void update (MemberUpdateDto memberUpdateDto) throws Exception { + public void update(MemberUpdateDto memberUpdateDto, Optional userImg) throws Exception { Member member = memberRepository .findByLoginId(SecurityUtil.getLoginLoginId()) .orElseThrow(() -> new Exception("회원이 존재하지 않습니다")); @@ -57,15 +58,18 @@ public void update (MemberUpdateDto memberUpdateDto) throws Exception { memberUpdateDto.name().ifPresent(member::setName); memberUpdateDto.nickname().ifPresent(member::setNickname); memberUpdateDto.role().ifPresent(member::setRole); + // 파일을 전송했을 경우에만 S3 파일 업로드 수행 - memberUpdateDto.userImg().ifPresent(image -> { + if (userImg.isPresent() && !userImg.get().isEmpty()) { try { - String imagePath = awsS3Service.uploadFile(image); + String imagePath = awsS3Service.uploadFile(userImg.get()); member.setMemberImage(imagePath); } catch (IOException e) { throw new RuntimeException("S3 업로드 중 에러가 발생했습니다.", e); } - }); + } + + memberRepository.save(member); } // 비밀번호 변경 -> 비밀번호를 입력 받는다 @@ -108,4 +112,4 @@ public MemberInfoDto getMyInfo() throws Exception { public Member findMemberByLoginId() throws Exception{ return memberRepository.findByLoginId(SecurityUtil.getLoginLoginId()).orElseThrow(() -> new Exception("회원이 없습니다")); } -} +} \ No newline at end of file From 67eb76c1242386d1379d4581e7f8b62bcf93501b Mon Sep 17 00:00:00 2001 From: minjoon-98 <4kmj54321@gmail.com> Date: Tue, 9 Jul 2024 20:36:34 +0900 Subject: [PATCH 2/2] feat : AWS S3 upload --- src/main/java/ongjong/namanmoo/service/AwsS3Service.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/ongjong/namanmoo/service/AwsS3Service.java b/src/main/java/ongjong/namanmoo/service/AwsS3Service.java index 4ad31e7..a7de7ed 100644 --- a/src/main/java/ongjong/namanmoo/service/AwsS3Service.java +++ b/src/main/java/ongjong/namanmoo/service/AwsS3Service.java @@ -30,10 +30,12 @@ public class AwsS3Service { private final String bucket; public AwsS3Service( - @Value("${aws.accessKeyId}") String accessKeyId, - @Value("${aws.secretKey}") String secretKey, + + @Value("${cloud.aws.credentials.access-key}") String accessKeyId, + @Value("${cloud.aws.credentials.secret-key}") String secretKey, @Value("${cloud.aws.s3.bucket}") String bucket, - @Value("${aws.region}") String region) { + @Value("${cloud.aws.region.static}") String region) { + BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKeyId, secretKey); this.amazonS3Client = AmazonS3ClientBuilder.standard() .withRegion(region)