Skip to content

Commit

Permalink
Merge pull request #20 from Ong-gi-Jong-gi/feature/TSK-38/face-challenge
Browse files Browse the repository at this point in the history
[AWS S3] 파일 업로드 설정
  • Loading branch information
minjoon-98 authored Jul 9, 2024
2 parents 8e4d61a + 67eb76c commit ff62c71
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
20 changes: 9 additions & 11 deletions src/main/java/ongjong/namanmoo/controller/MemberController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -19,6 +20,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Slf4j
@RestController
Expand Down Expand Up @@ -56,27 +58,23 @@ public ResponseEntity<ApiResponse<Map<String, Boolean>>> duplicate(@Valid @Reque
// 회원 정보 수정
@PostMapping("/users")
@ResponseStatus(HttpStatus.OK)
public ApiResponse<MemberInfoDto> updateBasicInfo(
@RequestPart("userInfo") MemberUpdateDto memberUpdateDto,
@RequestPart(value = "userImg", required = false) MultipartFile userImg) throws Exception {
public ApiResponse<MemberInfoDto> updateBasicInfo(@RequestPart("userInfo") MemberUpdateDto memberUpdateDto,
@RequestPart("userImg") Optional<MultipartFile> 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);
Expand Down
24 changes: 19 additions & 5 deletions src/main/java/ongjong/namanmoo/service/AwsS3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,15 +23,26 @@
import lombok.RequiredArgsConstructor;

@Slf4j
@RequiredArgsConstructor
@Service
public class AwsS3Service {

@Autowired
private AmazonS3 amazonS3Client;
private final AmazonS3 amazonS3Client;
private final String bucket;

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

@Value("${cloud.aws.credentials.access-key}") String accessKeyId,
@Value("${cloud.aws.credentials.secret-key}") String secretKey,
@Value("${cloud.aws.s3.bucket}") String bucket,
@Value("${cloud.aws.region.static}") 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...");
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/ongjong/namanmoo/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -21,7 +24,7 @@ public interface MemberService {
// 아이디 중복 체크
boolean isDuplicateId(LoginRequestDto loginRequestDto);

void update(MemberUpdateDto memberUpdateDto) throws Exception;
void update(MemberUpdateDto memberUpdateDto, Optional<MultipartFile> userImg) throws Exception;

void updatePassword(String checkPassword, String toBePassword) throws Exception;

Expand Down
14 changes: 9 additions & 5 deletions src/main/java/ongjong/namanmoo/service/MemberServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -48,7 +49,7 @@ public boolean isDuplicateId(LoginRequestDto loginRequestDto) {

// 회원 정보 수정
@Override
public void update (MemberUpdateDto memberUpdateDto) throws Exception {
public void update(MemberUpdateDto memberUpdateDto, Optional<MultipartFile> userImg) throws Exception {
Member member = memberRepository
.findByLoginId(SecurityUtil.getLoginLoginId())
.orElseThrow(() -> new Exception("회원이 존재하지 않습니다"));
Expand All @@ -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);
}

// 비밀번호 변경 -> 비밀번호를 입력 받는다
Expand Down Expand Up @@ -108,4 +112,4 @@ public MemberInfoDto getMyInfo() throws Exception {
public Member findMemberByLoginId() throws Exception{
return memberRepository.findByLoginId(SecurityUtil.getLoginLoginId()).orElseThrow(() -> new Exception("회원이 없습니다"));
}
}
}

0 comments on commit ff62c71

Please sign in to comment.