Skip to content

Commit

Permalink
[MERGE/#155] 이미지 업로드 persigned url로 일괄 변경
Browse files Browse the repository at this point in the history
[REFACTOR] - #155 이미지 업로드 persigned url로 일괄 변경
  • Loading branch information
seokbeom00 authored Oct 29, 2024
2 parents 3c0ffb4 + 76dada6 commit 86cba02
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
public class S3Controller {
private final S3Service s3Service;

@GetMapping("/image")
public ResponseEntity<PreSignedUrlResponse> getPreSignedUrl() {
return ResponseEntity.ok(s3Service.getUploadPreSignedUrl());
@GetMapping("/image/profile")
public ResponseEntity<PreSignedUrlResponse> getProfilePreSignedUrl() {
return ResponseEntity.ok(s3Service.getImageUploadPreSignedUrl());
}

@GetMapping("/image/businesscard")
public ResponseEntity<PreSignedUrlResponse> getBusinesscardPreSignedUrl() {
return ResponseEntity.ok(s3Service.getUploadBusinesscardPreSignedUrl());
}

@PatchMapping("/profile-image")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class S3Service {
private final static String profilePath = "profiles/";
private final static String businessPath = "businessCard/";

public PreSignedUrlResponse getUploadPreSignedUrl() {
public PreSignedUrlResponse getImageUploadPreSignedUrl() {
try {
// UUID 파일명 생성
String uuidFileName = UUID.randomUUID().toString() + ".jpg";
Expand Down Expand Up @@ -78,6 +78,34 @@ public PreSignedUrlResponse getUploadPreSignedUrl() {
}
}

public PreSignedUrlResponse getUploadBusinesscardPreSignedUrl() {
try {
// UUID 파일명 생성
String uuidFileName = UUID.randomUUID().toString() + ".jpg";
// 경로 + 파일 이름
String key = businessPath + uuidFileName;

PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.key(key)
.build();

// S3에서 업로드는 PUT 요청
PutObjectPresignRequest preSignedUrlRequest = PutObjectPresignRequest.builder()
.signatureDuration(Duration.ofMinutes(PRE_SIGNED_URL_EXPIRE_MINUTE))
.putObjectRequest(putObjectRequest)
.build();

// Persigned URL 생성
URL url = s3Presigner.presignPutObject(preSignedUrlRequest).url();

return PreSignedUrlResponse.of(uuidFileName, url.toString());

} catch (RuntimeException e) {
throw new CustomException(ErrorType.GET_UPLOAD_PRESIGNED_URL_ERROR);
}
}

@Transactional
public void uploadProfile(MultipartFile profileImage) {
validateExtension(profileImage);
Expand Down

0 comments on commit 86cba02

Please sign in to comment.