-
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.
* feat: S3 presigned URL 발급 구현 * refactor: SonarLint에 따라 BasicAWSCredentials를 바로 반환하도록 리팩토링
- Loading branch information
1 parent
cc9ba25
commit 2cfc982
Showing
8 changed files
with
155 additions
and
2 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
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
33 changes: 33 additions & 0 deletions
33
packy-api/src/main/java/com/dilly/file/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,33 @@ | ||
package com.dilly.file; | ||
|
||
import java.util.Map; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.dilly.application.FileService; | ||
import com.dilly.global.response.DataResponseDto; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Tag(name = "파일 관련 API") | ||
@RestController | ||
@RequestMapping("/api/v1/file") | ||
@RequiredArgsConstructor | ||
public class FileController { | ||
|
||
private final FileService fileService; | ||
|
||
@Operation(summary = "Presigned URL 생성") | ||
@GetMapping("/presigned-url/{fileName}") | ||
public DataResponseDto<Map<String, String>> getPresignedUrl( | ||
@PathVariable(name = "fileName") @Schema(description = "확장자명을 포함해주세요") | ||
String fileName) { | ||
return DataResponseDto.from(fileService.getPresignedUrl("images", 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
69 changes: 69 additions & 0 deletions
69
packy-infra/src/main/java/com/dilly/application/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,69 @@ | ||
package com.dilly.application; | ||
|
||
import java.net.URL; | ||
import java.util.Date; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
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 lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class FileService { | ||
|
||
@Value("${cloud.s3.bucket}") | ||
private String bucket; | ||
|
||
private final AmazonS3 amazonS3; | ||
|
||
public Map<String, String> getPresignedUrl(String prefix, String fileName) { | ||
if (!prefix.isEmpty()) { | ||
fileName = createPath(prefix, fileName); | ||
} | ||
|
||
GeneratePresignedUrlRequest generatePresignedUrlRequest = getGeneratePresignedUrlRequest(bucket, fileName); | ||
URL url = amazonS3.generatePresignedUrl(generatePresignedUrlRequest); | ||
|
||
return Map.of("url", url.toString()); | ||
} | ||
|
||
private GeneratePresignedUrlRequest getGeneratePresignedUrlRequest(String bucket, String fileName) { | ||
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket, fileName) | ||
.withMethod(HttpMethod.PUT) | ||
.withExpiration(getPresignedUrlExpiration()); | ||
|
||
generatePresignedUrlRequest.addRequestParameter( | ||
Headers.S3_CANNED_ACL, | ||
CannedAccessControlList.PublicRead.toString() | ||
); | ||
|
||
return generatePresignedUrlRequest; | ||
} | ||
|
||
private Date getPresignedUrlExpiration() { | ||
Date expiration = new Date(); | ||
long expTimeMillis = expiration.getTime(); | ||
expTimeMillis += 1000 * 60 * 2; | ||
expiration.setTime(expTimeMillis); | ||
|
||
return expiration; | ||
} | ||
|
||
private String createFileId() { | ||
return UUID.randomUUID().toString(); | ||
} | ||
|
||
private String createPath(String prefix, String fileName) { | ||
String fileId = createFileId(); | ||
return String.format("%s/%s", prefix, fileId + "-" + 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.dilly.global; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
|
||
@Configuration | ||
public class S3Config { | ||
|
||
@Value("${cloud.aws.credentials.access-key}") | ||
private String accessKey; | ||
|
||
@Value("${cloud.aws.credentials.secret-key}") | ||
private String secretKey; | ||
|
||
@Value("${cloud.aws.region}") | ||
private String region; | ||
|
||
@Bean | ||
@Primary | ||
public BasicAWSCredentials awsCredentialsProvider() { | ||
return new BasicAWSCredentials(accessKey, secretKey); | ||
} | ||
|
||
@Bean | ||
public AmazonS3 amazonS3() { | ||
return AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentialsProvider())) | ||
.build(); | ||
} | ||
} |
Submodule packy-submodule
updated
from 619803 to 5f0fac