-
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.
ygyg[v1.0.0-beta.15] - API 개발 - aws s3 파일 업로드를 위한 presignedUrl 생성 API 개발 - Fix/Modify - 타입별 내 소분글 조회 api에서 중복 데이터가 조회되는 버그 수정 - igthub actions workflow에서 환경변수 주입 방법 변경
- Loading branch information
Showing
10 changed files
with
225 additions
and
1 deletion.
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
53 changes: 53 additions & 0 deletions
53
src/main/java/foiegras/ygyg/aws/api/controller/AwsController.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,53 @@ | ||
package foiegras.ygyg.aws.api.controller; | ||
|
||
|
||
import foiegras.ygyg.aws.api.request.GetPutObjectPreSignedUrlRequest; | ||
import foiegras.ygyg.aws.api.response.GetPreSignedUrlResponse; | ||
import foiegras.ygyg.aws.application.dto.in.GetPutObjectPreSignedUrlInDto; | ||
import foiegras.ygyg.aws.application.dto.out.GetPutObjectPreSignedUrlOutDto; | ||
import foiegras.ygyg.aws.application.service.AwsService; | ||
import foiegras.ygyg.global.common.response.BaseResponse; | ||
import foiegras.ygyg.global.common.security.CustomUserDetails; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
|
||
@Validated | ||
@RestController | ||
@RequestMapping("/api/v1/aws") | ||
@RequiredArgsConstructor | ||
public class AwsController { | ||
|
||
// service | ||
private final AwsService awsService; | ||
// util | ||
private final ModelMapper modelMapper; | ||
|
||
|
||
/** | ||
* AwsController | ||
* 1. S3 PreSigned URL 생성 | ||
*/ | ||
|
||
// 1. S3 PreSigned URL 생성 | ||
@Operation(summary = "S3 PreSigned URL 생성", description = "S3 PreSigned URL 생성", tags = { "Aws" }) | ||
@GetMapping("/presigned-url") | ||
@SecurityRequirement(name = "Bearer Auth") | ||
public BaseResponse<GetPreSignedUrlResponse> getPutObjectPreSignedUrl(@Valid GetPutObjectPreSignedUrlRequest request, @AuthenticationPrincipal CustomUserDetails authentication) { | ||
GetPutObjectPreSignedUrlInDto inDto = modelMapper.map(request, GetPutObjectPreSignedUrlInDto.class); | ||
inDto = inDto.toBuilder() | ||
.userEmail(authentication.getUserEmail()) | ||
.build(); | ||
GetPutObjectPreSignedUrlOutDto outDto = awsService.getPutObjectPreSignedUrl(inDto); | ||
return new BaseResponse<>(modelMapper.map(outDto, GetPreSignedUrlResponse.class)); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/foiegras/ygyg/aws/api/request/GetPutObjectPreSignedUrlRequest.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,25 @@ | ||
package foiegras.ygyg.aws.api.request; | ||
|
||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class GetPutObjectPreSignedUrlRequest { | ||
|
||
@NotNull | ||
@NotEmpty | ||
@NotBlank | ||
private String fileName; | ||
|
||
@NotNull | ||
@NotEmpty | ||
@NotBlank | ||
private String contentType; | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/foiegras/ygyg/aws/api/response/GetPreSignedUrlResponse.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,14 @@ | ||
package foiegras.ygyg.aws.api.response; | ||
|
||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class GetPreSignedUrlResponse { | ||
|
||
private String preSignedUrl; | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/foiegras/ygyg/aws/application/dto/in/GetPutObjectPreSignedUrlInDto.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,20 @@ | ||
package foiegras.ygyg.aws.application.dto.in; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
|
||
@Getter | ||
@Builder(toBuilder = true) | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class GetPutObjectPreSignedUrlInDto { | ||
|
||
private String fileName; | ||
private String contentType; | ||
private String userEmail; | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/foiegras/ygyg/aws/application/dto/out/GetPutObjectPreSignedUrlOutDto.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,18 @@ | ||
package foiegras.ygyg.aws.application.dto.out; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
|
||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class GetPutObjectPreSignedUrlOutDto { | ||
|
||
private String preSignedUrl; | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/foiegras/ygyg/aws/application/service/AwsService.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,51 @@ | ||
package foiegras.ygyg.aws.application.service; | ||
|
||
|
||
import foiegras.ygyg.aws.application.dto.in.GetPutObjectPreSignedUrlInDto; | ||
import foiegras.ygyg.aws.application.dto.out.GetPutObjectPreSignedUrlOutDto; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
import software.amazon.awssdk.services.s3.presigner.S3Presigner; | ||
import software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest; | ||
|
||
import java.time.Duration; | ||
|
||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AwsService { | ||
|
||
// aws | ||
private final S3Presigner s3Presigner; | ||
@Value("${aws.s3.bucket}") | ||
private String bucket; | ||
@Value("${aws.s3.duration}") | ||
private Long duration; | ||
|
||
|
||
/** | ||
* AwsService | ||
* 1. S3 PutObject PreSigned URL 생성 | ||
*/ | ||
|
||
// 1. S3 PutObject PreSigned URL 생성 | ||
public GetPutObjectPreSignedUrlOutDto getPutObjectPreSignedUrl(GetPutObjectPreSignedUrlInDto inDto) { | ||
// key: 저장될 파일명 | ||
String key = inDto.getUserEmail() + "-" + inDto.getFileName(); | ||
// PutObjectRequest | ||
PutObjectRequest putObjectRequest = PutObjectRequest.builder() | ||
.bucket(bucket) | ||
.key(key) | ||
.contentType(inDto.getContentType()) | ||
.build(); | ||
// preSignRequest | ||
PutObjectPresignRequest preSignRequest = PutObjectPresignRequest.builder() | ||
.signatureDuration(Duration.ofMinutes(duration)) | ||
.putObjectRequest(putObjectRequest) | ||
.build(); | ||
return new GetPutObjectPreSignedUrlOutDto(s3Presigner.presignPutObject(preSignRequest).url().toString()); | ||
} | ||
|
||
} |
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,34 @@ | ||
package foiegras.ygyg.global; | ||
|
||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
import software.amazon.awssdk.auth.credentials.AwsCredentials; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.s3.presigner.S3Presigner; | ||
|
||
|
||
@Configuration | ||
public class S3Config { | ||
|
||
@Value("${spring.cloud.aws.region.static}") | ||
private String region; | ||
@Value("${spring.cloud.aws.credentials.access-key}") | ||
private String accessKey; | ||
@Value("${spring.cloud.aws.credentials.secret-key}") | ||
private String secretKey; | ||
|
||
|
||
// S3Presigner: s3 임시접근 url 생성 도구 | ||
@Bean | ||
public S3Presigner s3Presigner() { | ||
AwsCredentials awsCredentials = AwsBasicCredentials.create(accessKey, secretKey); | ||
return S3Presigner.builder() | ||
.region(Region.of(region)) | ||
.credentialsProvider(() -> awsCredentials) | ||
.build(); | ||
} | ||
|
||
} |
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