-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BE] feat: S3 pre-singed-url 반환 기능 구현 (#591)
* feat: S3 pre-singed-url 반환 기능 구현 * refactor: swagger 명세 수정 * fix: test 실패 수정 * fix: test 실패 expiration time -> 숫자로 수정 * refactor: 리뷰 반영 * refactor: swagger 명세 수정
- Loading branch information
Showing
12 changed files
with
201 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
28 changes: 28 additions & 0 deletions
28
backend/src/main/java/com/funeat/common/controller/PreSingedApiController.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,28 @@ | ||
package com.funeat.common.controller; | ||
|
||
import com.funeat.common.dto.S3UrlRequest; | ||
import com.funeat.common.dto.S3UrlResponse; | ||
import com.funeat.common.s3.S3UploadUrlGenerator; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class PreSingedApiController implements PreSingedController { | ||
|
||
private final S3UploadUrlGenerator s3UploadUrlGenerator; | ||
|
||
public PreSingedApiController(final S3UploadUrlGenerator s3UploadUrlGenerator) { | ||
this.s3UploadUrlGenerator = s3UploadUrlGenerator; | ||
} | ||
|
||
@PostMapping("/api/s3/presigned") | ||
public ResponseEntity<S3UrlResponse> getPreSingedUrl(@RequestBody final S3UrlRequest request) { | ||
final S3UrlResponse preSignedUrl = s3UploadUrlGenerator.getPreSignedUrl(request.getFileName()); | ||
|
||
return ResponseEntity.status(HttpStatus.CREATED) | ||
.body(preSignedUrl); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
backend/src/main/java/com/funeat/common/controller/PreSingedController.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 com.funeat.common.controller; | ||
|
||
import com.funeat.common.dto.S3UrlRequest; | ||
import com.funeat.common.dto.S3UrlResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
|
||
public interface PreSingedController { | ||
|
||
@Operation(summary = "S3 업로드 URL 요청", description = "S3 업로드 URL 요청한다.") | ||
@ApiResponse( | ||
responseCode = "201", | ||
description = "업로드 URL 요청 성공." | ||
) | ||
@PostMapping | ||
ResponseEntity<S3UrlResponse> getPreSingedUrl(@RequestBody final S3UrlRequest request); | ||
} |
20 changes: 20 additions & 0 deletions
20
backend/src/main/java/com/funeat/common/dto/S3UrlRequest.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 com.funeat.common.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import javax.validation.constraints.NotBlank; | ||
|
||
public class S3UrlRequest { | ||
|
||
@NotBlank(message = "파일명을 확인해주세요") | ||
private final String fileName; | ||
|
||
@JsonCreator | ||
public S3UrlRequest(@JsonProperty("fileName") final String fileName) { | ||
this.fileName = fileName; | ||
} | ||
|
||
public String getFileName() { | ||
return fileName; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
backend/src/main/java/com/funeat/common/dto/S3UrlResponse.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 com.funeat.common.dto; | ||
|
||
public class S3UrlResponse { | ||
|
||
private final String preSingedUrl; | ||
|
||
public S3UrlResponse(final String preSingedUrl) { | ||
this.preSingedUrl = preSingedUrl; | ||
} | ||
|
||
public String getPreSingedUrl() { | ||
return preSingedUrl; | ||
} | ||
} |
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,28 @@ | ||
package com.funeat.common.s3; | ||
|
||
import com.amazonaws.auth.InstanceProfileCredentialsProvider; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class AwsConfig { | ||
|
||
@Value("${cloud.aws.region.static}") | ||
private String region; | ||
|
||
@Bean | ||
public InstanceProfileCredentialsProvider awsCredentialsProvider() { | ||
return InstanceProfileCredentialsProvider.getInstance(); | ||
} | ||
|
||
@Bean | ||
public AmazonS3 amazonS3Client() { | ||
return AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(awsCredentialsProvider()) | ||
.build(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
backend/src/main/java/com/funeat/common/s3/S3UploadUrlGenerator.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,54 @@ | ||
package com.funeat.common.s3; | ||
|
||
import com.amazonaws.HttpMethod; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.Headers; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; | ||
import com.funeat.common.dto.S3UrlResponse; | ||
import java.util.Date; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class S3UploadUrlGenerator { | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
@Value("${cloud.aws.s3.folder}") | ||
private String folder; | ||
|
||
@Value("${cloud.aws.s3.expiration.time}") | ||
private long expirationTime; | ||
|
||
private final AmazonS3 amazonS3; | ||
|
||
public S3UploadUrlGenerator(final AmazonS3 amazonS3) { | ||
this.amazonS3 = amazonS3; | ||
} | ||
|
||
public S3UrlResponse getPreSignedUrl(final String fileName) { | ||
final GeneratePresignedUrlRequest generatePresignedUrlRequest = getPreSignedUrlRequest(bucket, fileName); | ||
|
||
return new S3UrlResponse(amazonS3.generatePresignedUrl(generatePresignedUrlRequest).toString()); | ||
} | ||
|
||
private GeneratePresignedUrlRequest getPreSignedUrlRequest(final String bucket, final String fileName) { | ||
final Date madeExpirationTime = getExpirationTime(); | ||
final GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(bucket, | ||
folder + fileName) | ||
.withMethod(HttpMethod.PUT) | ||
.withExpiration(madeExpirationTime); | ||
|
||
urlRequest.addRequestParameter(Headers.S3_CANNED_ACL, CannedAccessControlList.PublicRead.toString()); | ||
|
||
return urlRequest; | ||
} | ||
|
||
private Date getExpirationTime() { | ||
final Date madeExpirationTime = new Date(); | ||
madeExpirationTime.setTime(expirationTime); | ||
return madeExpirationTime; | ||
} | ||
} |
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
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