-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
S3 change to presigned url
- Loading branch information
Showing
9 changed files
with
140 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 3 additions & 8 deletions
11
api-module/src/main/java/com/foodgo/apimodule/cuisine/application/CuisineSaveUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
api-module/src/main/java/com/foodgo/apimodule/file/presentation/FileController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.foodgo.apimodule.file.presentation; | ||
|
||
import com.foodgo.commonmodule.common.ApplicationResponse; | ||
import com.foodgo.commonmodule.image.dto.PresignedUrlResponse; | ||
import com.foodgo.commonmodule.image.service.FileService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "파일 관리 API", description = "파일 업로드 및 다운로드 관련 API") | ||
@RestController | ||
@RequestMapping("/api/v1/files") | ||
@RequiredArgsConstructor | ||
public class FileController { | ||
|
||
private final FileService fileService; | ||
|
||
@Operation(summary = "프리사인드 URL 생성", description = "파일 업로드를 위한 프리사인드 URL을 생성합니다. 유효기간 15분 입니다.") | ||
@GetMapping("/presigned-url") | ||
public ApplicationResponse<PresignedUrlResponse> getPresignedUrl( | ||
@Parameter(description = "파일 경로의 prefix (이미지의 경우 images 필수)", example = "/images") @RequestParam(name = "prefix", defaultValue = "images") String prefix, | ||
@Parameter(description = "파일 이름", required = true, example = "example.txt") @RequestParam(name = "fileName") String fileName) { | ||
return ApplicationResponse.onSuccess(fileService.getUploadPresignedUrl(prefix, fileName)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
public record IngredientAddReq( | ||
String name, | ||
String quantity | ||
String quantity, | ||
String imageUrl | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
common-module/src/main/java/com/foodgo/commonmodule/image/dto/PresignedUrlResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.foodgo.commonmodule.image.dto; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
@Schema(description = "프리사인드 URL 응답 DTO") | ||
public class PresignedUrlResponse { | ||
|
||
@Schema(description = "생성된 프리사인드 URL", example = "https://example.com/presigned-url") | ||
private final String url; | ||
|
||
@Schema(description = "파일 경로", example = "images/example.txt") | ||
private final String filePath; | ||
} |
73 changes: 73 additions & 0 deletions
73
common-module/src/main/java/com/foodgo/commonmodule/image/service/FileService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.foodgo.commonmodule.image.service; | ||
|
||
import com.amazonaws.HttpMethod; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; | ||
import com.foodgo.commonmodule.image.dto.PresignedUrlResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.net.URL; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class FileService { | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
@Value("${cloud.aws.s3.expTime}") | ||
private Long expTime; | ||
|
||
|
||
private final AmazonS3 amazonS3; | ||
|
||
public PresignedUrlResponse getUploadPresignedUrl(String prefix, String originalFileName) { | ||
String filePath = createPath(prefix, originalFileName); | ||
GeneratePresignedUrlRequest generatePresignedUrlRequest = getGeneratePresignedUrlRequest(bucket, filePath, HttpMethod.PUT); | ||
URL url = amazonS3.generatePresignedUrl(generatePresignedUrlRequest); | ||
|
||
return new PresignedUrlResponse(url.toString(), filePath); | ||
} | ||
|
||
public String getDownloadPresignedUrl(String filePath) { | ||
if (filePath != null && filePath.startsWith("images/")) { | ||
GeneratePresignedUrlRequest generatePresignedUrlRequest = getGeneratePresignedUrlRequest(bucket, filePath, HttpMethod.GET); | ||
URL url = amazonS3.generatePresignedUrl(generatePresignedUrlRequest); | ||
return url.toString(); | ||
} | ||
|
||
return filePath; | ||
} | ||
|
||
private GeneratePresignedUrlRequest getGeneratePresignedUrlRequest(String bucket, String fileName, HttpMethod method) { | ||
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket, fileName) | ||
.withMethod(method) | ||
.withExpiration(getPresignedUrlExpiration()); | ||
|
||
return generatePresignedUrlRequest; | ||
} | ||
|
||
private Date getPresignedUrlExpiration() { | ||
Date expiration = new Date(); | ||
long expTimeMillis = expiration.getTime(); | ||
expTimeMillis += expTime; | ||
expiration.setTime(expTimeMillis); | ||
|
||
return expiration; | ||
} | ||
|
||
private String createFileId() { | ||
return UUID.randomUUID().toString(); | ||
} | ||
|
||
private String createPath(String prefix, String fileName) { | ||
String fileId = createFileId(); | ||
String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); | ||
return String.format("%s/%s-%s-%s", prefix, timestamp, fileId, fileName); | ||
} | ||
} |