diff --git a/src/main/java/com/ddabong/ddabongdotchiBE/domain/s3/S3Response.java b/src/main/java/com/ddabong/ddabongdotchiBE/domain/s3/S3Response.java new file mode 100644 index 0000000..9ace12b --- /dev/null +++ b/src/main/java/com/ddabong/ddabongdotchiBE/domain/s3/S3Response.java @@ -0,0 +1,16 @@ +package com.ddabong.ddabongdotchiBE.domain.s3; + +import lombok.Builder; + +@Builder +public record S3Response( + String preSignedUrl, + String imageUrl +) { + public static S3Response from(String preSignedUrl, String imageUrl) { + return S3Response.builder() + .preSignedUrl(preSignedUrl) + .imageUrl(imageUrl) + .build(); + } +} diff --git a/src/main/java/com/ddabong/ddabongdotchiBE/domain/s3/S3Service.java b/src/main/java/com/ddabong/ddabongdotchiBE/domain/s3/S3Service.java index 1a67214..fbd68dc 100644 --- a/src/main/java/com/ddabong/ddabongdotchiBE/domain/s3/S3Service.java +++ b/src/main/java/com/ddabong/ddabongdotchiBE/domain/s3/S3Service.java @@ -3,12 +3,15 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.util.Calendar; +import java.util.Date; import java.util.List; import java.util.UUID; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import com.amazonaws.HttpMethod; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.model.PutObjectRequest; import com.ddabong.ddabongdotchiBE.domain.s3.convert.webp.WebpConvertService; @@ -74,4 +77,16 @@ public List uploadFiles(List files) { private String getFileNamePrefix() { return UUID.randomUUID().toString().substring(0, 5) + "-"; } + + public S3Response getPreSignedUrl(String fileName) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(new Date()); + calendar.add(Calendar.MINUTE, 10); //validfy of 10 minutes + fileName = s3Config.getFolder() + getFileNamePrefix() + fileName; + return S3Response.from( + amazonS3.generatePresignedUrl(s3Config.getBucket(), fileName, calendar.getTime(), HttpMethod.PUT) + .toString(), + amazonS3.getUrl(s3Config.getBucket(), fileName).toString() + ); + } } diff --git a/src/main/java/com/ddabong/ddabongdotchiBE/domain/s3/S3TestController.java b/src/main/java/com/ddabong/ddabongdotchiBE/domain/s3/S3TestController.java index 1f8397e..e55c4a6 100644 --- a/src/main/java/com/ddabong/ddabongdotchiBE/domain/s3/S3TestController.java +++ b/src/main/java/com/ddabong/ddabongdotchiBE/domain/s3/S3TestController.java @@ -5,9 +5,11 @@ import java.util.List; import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; @@ -47,4 +49,9 @@ public ApiResponse> uploadImages( return ApiResponse.onSuccess(s3Service.uploadFiles(files)); } + @GetMapping(value = "/getPreSigned") + public S3Response getPreSignedUrl(@RequestParam String fileName) { + return s3Service.getPreSignedUrl(fileName); + } + }