-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] s3 presignedUrl 발급 기능 구현 (#84)
- Loading branch information
Showing
13 changed files
with
208 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -175,3 +175,5 @@ gradle-app.setting | |
*.hprof | ||
|
||
redis/ | ||
|
||
.run |
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
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/com/mjucow/eatda/common/config/S3Config.kt
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,29 @@ | ||
package com.mjucow.eatda.common.config | ||
|
||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Profile | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials | ||
import software.amazon.awssdk.regions.Region | ||
import software.amazon.awssdk.services.s3.presigner.S3Presigner | ||
|
||
@Configuration | ||
@Profile("prod") | ||
class S3Config( | ||
@Value("\${aws.s3.credentials.access-key}") | ||
private val accessKey: String, | ||
@Value("\${aws.s3.credentials.secret-key}") | ||
private val secretKey: String, | ||
) { | ||
|
||
@Bean | ||
fun s3Presigner(): S3Presigner { | ||
val credential = AwsBasicCredentials.create(accessKey, secretKey) | ||
|
||
return S3Presigner.builder() | ||
.region(Region.AP_NORTHEAST_2) | ||
.credentialsProvider { credential } | ||
.build() | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/com/mjucow/eatda/domain/s3/dto/PresignedUrlDto.kt
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.mjucow.eatda.domain.s3.dto | ||
|
||
import java.net.URL | ||
|
||
data class PresignedUrlDto( | ||
val url: URL, | ||
) { | ||
|
||
companion object { | ||
fun from(url: URL): PresignedUrlDto { | ||
return PresignedUrlDto(url) | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/kotlin/com/mjucow/eatda/domain/s3/service/S3Service.kt
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,59 @@ | ||
package com.mjucow.eatda.domain.s3.service | ||
|
||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Profile | ||
import org.springframework.stereotype.Service | ||
import software.amazon.awssdk.services.s3.model.GetObjectRequest | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest | ||
import software.amazon.awssdk.services.s3.presigner.S3Presigner | ||
import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest | ||
import software.amazon.awssdk.services.s3.presigner.model.PutObjectPresignRequest | ||
import java.net.URL | ||
import java.time.Duration | ||
|
||
@Service | ||
@Profile("prod") | ||
class S3Service( | ||
private val s3Presigner: S3Presigner, | ||
@Value("\${aws.s3.bucket}") | ||
private val bucket: String, | ||
) { | ||
|
||
fun createPutPresignedUrl(key: String, contentType: String): URL { | ||
val putObjectRequest = PutObjectRequest | ||
.builder() | ||
.bucket(bucket) | ||
.key(key) | ||
.contentType(contentType) | ||
.build() | ||
|
||
val presignRequest = PutObjectPresignRequest | ||
.builder() | ||
.signatureDuration(Duration.ofMinutes(UPLOAD_DURATION_MINUTES)) | ||
.putObjectRequest(putObjectRequest) | ||
.build() | ||
|
||
return s3Presigner.presignPutObject(presignRequest).url() | ||
} | ||
|
||
fun createGetPresignedUrl(key: String): URL { | ||
val getObjectRequest = GetObjectRequest | ||
.builder() | ||
.bucket(bucket) | ||
.key(key) | ||
.build() | ||
|
||
val presignRequest = GetObjectPresignRequest | ||
.builder() | ||
.signatureDuration(Duration.ofHours(DOWNLOAD_DURATION_HOURS)) | ||
.getObjectRequest(getObjectRequest) | ||
.build() | ||
|
||
return s3Presigner.presignGetObject(presignRequest).url() | ||
} | ||
|
||
companion object { | ||
const val UPLOAD_DURATION_MINUTES = 3L | ||
const val DOWNLOAD_DURATION_HOURS = 24L | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/com/mjucow/eatda/presentation/s3/S3ApiPresentation.kt
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,16 @@ | ||
package com.mjucow.eatda.presentation.s3 | ||
|
||
import com.mjucow.eatda.domain.s3.dto.PresignedUrlDto | ||
import com.mjucow.eatda.presentation.common.ApiResponse | ||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.tags.Tag | ||
|
||
@Tag(name = "S3 API", description = "S3 관련 API") | ||
interface S3ApiPresentation { | ||
|
||
@Operation( | ||
summary = "이미지 업로드용 presigned URL 발급", | ||
description = "*key: 버킷의 폴더 경로" | ||
) | ||
fun getPutPresignedUrl(key: String, contentType: String): ApiResponse<PresignedUrlDto> | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/com/mjucow/eatda/presentation/s3/S3Controller.kt
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.mjucow.eatda.presentation.s3 | ||
|
||
import com.mjucow.eatda.domain.s3.dto.PresignedUrlDto | ||
import com.mjucow.eatda.domain.s3.service.S3Service | ||
import com.mjucow.eatda.presentation.common.ApiResponse | ||
import org.springframework.context.annotation.Profile | ||
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 | ||
|
||
@RequestMapping("/api/v1/s3") | ||
@RestController | ||
@Profile("prod") | ||
class S3Controller( | ||
private val s3Service: S3Service, | ||
) : S3ApiPresentation { | ||
|
||
@GetMapping("/presigned-url") | ||
override fun getPutPresignedUrl( | ||
@RequestParam key: String, | ||
@RequestParam contentType: String, | ||
): ApiResponse<PresignedUrlDto> { | ||
return ApiResponse.success( | ||
PresignedUrlDto( | ||
s3Service.createPutPresignedUrl( | ||
key = key, | ||
contentType = contentType | ||
) | ||
) | ||
) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
spring: | ||
jpa: | ||
hibernate: | ||
ddl-auto: validate | ||
properties: | ||
hibernate: | ||
format_sql: true | ||
show_sql: true | ||
database-platform: org.hibernate.dialect.PostgreSQLDialect | ||
database: postgresql | ||
liquibase: | ||
change-log: classpath:/db/changelog-master.yml | ||
enabled: true | ||
jpa: | ||
hibernate: | ||
ddl-auto: validate | ||
properties: | ||
hibernate: | ||
format_sql: true | ||
show_sql: true | ||
database-platform: org.hibernate.dialect.PostgreSQLDialect | ||
database: postgresql | ||
liquibase: | ||
change-log: classpath:/db/changelog-master.yml | ||
enabled: true | ||
|
||
datasource: | ||
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver | ||
url: jdbc:tc:postgresql:15.4-alpine://test-database | ||
datasource: | ||
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver | ||
url: jdbc:tc:postgresql:15.4-alpine://test-database | ||
|
||
data: | ||
redis: | ||
host: 127.0.0.1 | ||
port: 6379 | ||
data: | ||
redis: | ||
host: 127.0.0.1 | ||
port: 6379 | ||
|
||
logging.config: classpath:logback-test.xml |
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 |
---|---|---|
@@ -1,20 +1,20 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<Logger level="debug" name="org.jooq"> | ||
<AppenderRef ref="Console"/> | ||
</Logger> | ||
|
||
<appender class="ch.qos.logback.core.ConsoleAppender" name="STDOUT"> | ||
<!-- encoders are assigned the type | ||
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> | ||
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<!-- Other jOOQ related debug log output --> | ||
<logger level="INFO" name="org.testcontainers"/> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
|
||
<!-- Other jOOQ related debug log output --> | ||
<Logger name="org.jooq" level="debug"> | ||
<AppenderRef ref="Console"/> | ||
</Logger> | ||
|
||
<logger name="org.testcontainers" level="INFO"/> | ||
</configuration> |