-
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.
Merge branch 'dev' of https://github.com/YAPP-Github/25th-Web-Team-2-BE…
… into feat/YS-31
- Loading branch information
Showing
15 changed files
with
243 additions
and
9 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
5 changes: 3 additions & 2 deletions
5
src/main/kotlin/com/dobby/backend/config/ApplicationConfig.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 |
---|---|---|
@@ -1,10 +1,11 @@ | ||
package com.dobby.backend.config | ||
|
||
import com.dobby.backend.infrastructure.config.TokenProperties | ||
import com.dobby.backend.infrastructure.config.properties.S3Properties | ||
import com.dobby.backend.infrastructure.config.properties.TokenProperties | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
@EnableConfigurationProperties(TokenProperties::class) | ||
@EnableConfigurationProperties(TokenProperties::class, S3Properties::class) | ||
class ApplicationConfig { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/com/dobby/backend/domain/gateway/S3Gateway.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,7 @@ | ||
package com.dobby.backend.domain.gateway | ||
|
||
import com.dobby.backend.presentation.api.dto.response.PreSignedUrlResponse | ||
|
||
interface S3Gateway { | ||
fun getPreSignedUrl(fileName: String): PreSignedUrlResponse | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/com/dobby/backend/infrastructure/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,35 @@ | ||
package com.dobby.backend.infrastructure.config | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider | ||
import com.amazonaws.auth.BasicAWSCredentials | ||
import com.amazonaws.services.s3.AmazonS3 | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder | ||
import com.dobby.backend.infrastructure.config.properties.S3Properties | ||
import lombok.RequiredArgsConstructor | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
@EnableConfigurationProperties(S3Properties::class) | ||
@RequiredArgsConstructor | ||
class S3Config { | ||
|
||
private val properties: S3Properties | ||
|
||
@Autowired | ||
constructor(properties: S3Properties) { | ||
this.properties = properties | ||
} | ||
|
||
@Bean | ||
fun amazonS3Client(): AmazonS3? { | ||
val awsCredentialsProvider = BasicAWSCredentials(properties.credentials.accessKey, properties.credentials.secretKey) | ||
|
||
return AmazonS3ClientBuilder.standard() | ||
.withRegion(properties.region.static) | ||
.withCredentials(AWSStaticCredentialsProvider(awsCredentialsProvider)) | ||
.build() | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/com/dobby/backend/infrastructure/config/properties/S3Properties.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,23 @@ | ||
package com.dobby.backend.infrastructure.config.properties | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
|
||
@ConfigurationProperties(prefix = "cloud.aws") | ||
data class S3Properties( | ||
val s3: S3, | ||
val region: Region, | ||
val credentials: Credentials | ||
) { | ||
data class S3( | ||
val bucket: String | ||
) | ||
|
||
data class Region( | ||
val static: String | ||
) | ||
|
||
data class Credentials( | ||
val accessKey: String, | ||
val secretKey: String | ||
) | ||
} |
2 changes: 1 addition & 1 deletion
2
.../infrastructure/config/TokenProperties.kt → ...ture/config/properties/TokenProperties.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
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/com/dobby/backend/infrastructure/gateway/S3GatewayImpl.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.dobby.backend.infrastructure.gateway | ||
|
||
import com.dobby.backend.domain.gateway.S3Gateway | ||
import com.dobby.backend.infrastructure.s3.S3PreSignedUrlProvider | ||
import com.dobby.backend.presentation.api.dto.response.PreSignedUrlResponse | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class S3GatewayImpl( | ||
private val s3PreSignedUrlProvider: S3PreSignedUrlProvider | ||
) : S3Gateway { | ||
|
||
override fun getPreSignedUrl(fileName: String): PreSignedUrlResponse { | ||
return s3PreSignedUrlProvider.getPreSignedUrl(fileName) | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/kotlin/com/dobby/backend/infrastructure/s3/S3PreSignedUrlProvider.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,60 @@ | ||
package com.dobby.backend.infrastructure.s3 | ||
|
||
import com.amazonaws.AmazonServiceException | ||
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.dobby.backend.domain.exception.InvalidInputException | ||
import com.dobby.backend.presentation.api.dto.response.PreSignedUrlResponse | ||
import com.dobby.backend.util.generateULID | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Component | ||
import java.util.* | ||
|
||
@Component | ||
class S3PreSignedUrlProvider( | ||
private val amazonS3Client: AmazonS3, | ||
) { | ||
|
||
@Value("\${cloud.aws.s3.bucket}") | ||
lateinit var bucket: String | ||
|
||
fun getPreSignedUrl(fileName: String): PreSignedUrlResponse { | ||
val generatePreSignedUrlRequest = getGenerateImagePreSignedUrlRequest(fileName) | ||
return PreSignedUrlResponse(generatePreSignedUrl(generatePreSignedUrlRequest)) | ||
} | ||
|
||
private fun generatePreSignedUrl(generatePresignedUrlRequest: GeneratePresignedUrlRequest): String { | ||
return try { | ||
amazonS3Client.generatePresignedUrl(generatePresignedUrlRequest).toString() | ||
} catch (e: AmazonServiceException) { | ||
throw IllegalStateException("Pre-signed Url 생성 실패했습니다.") | ||
} | ||
} | ||
|
||
private fun getGenerateImagePreSignedUrlRequest(fileName: String): GeneratePresignedUrlRequest { | ||
val savedImageName = generateUniqueImageName(fileName) | ||
val savedImagePath = "images/$savedImageName" | ||
return getPreSignedUrlRequest(bucket, savedImagePath) | ||
} | ||
|
||
private fun getPreSignedUrlRequest(bucket: String, fileName: String): GeneratePresignedUrlRequest { | ||
return GeneratePresignedUrlRequest(bucket, fileName) | ||
.withMethod(HttpMethod.PUT) | ||
.withExpiration(Date(System.currentTimeMillis() + 180000)) | ||
.apply { | ||
addRequestParameter(Headers.S3_CANNED_ACL, CannedAccessControlList.PublicRead.toString()) | ||
} | ||
} | ||
|
||
private fun generateUniqueImageName(fileName: String): String { | ||
val lastDotIndex = fileName.lastIndexOf(".") | ||
if (lastDotIndex == -1 || lastDotIndex == fileName.length - 1) { | ||
throw InvalidInputException() | ||
} | ||
val ext = fileName.substring(lastDotIndex) | ||
return "${generateULID()}$ext" | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/com/dobby/backend/presentation/api/dto/response/PreSignedUrlResponse.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,5 @@ | ||
package com.dobby.backend.presentation.api.dto.response | ||
|
||
data class PreSignedUrlResponse( | ||
val url: String | ||
) |
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
56 changes: 56 additions & 0 deletions
56
src/test/kotlin/com/dobby/backend/infrastructure/s3/S3PreSignedUrlProviderTest.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,56 @@ | ||
package com.dobby.backend.infrastructure.s3 | ||
|
||
import com.amazonaws.services.s3.AmazonS3 | ||
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest | ||
import com.dobby.backend.domain.exception.InvalidInputException | ||
import com.dobby.backend.util.generateULID | ||
import io.kotest.core.spec.style.BehaviorSpec | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.mockito.Mockito.* | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import java.net.URL | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
import kotlin.test.assertNotNull | ||
|
||
@SpringBootTest | ||
class S3PreSignedUrlProviderTest : BehaviorSpec({ | ||
|
||
val amazonS3Client = mock(AmazonS3::class.java) | ||
val provider = S3PreSignedUrlProvider(amazonS3Client) | ||
provider.bucket = "test-bucket" | ||
|
||
given("이미지 파일 이름이 주어지고") { | ||
val imageName = "test_image.jpg" | ||
val mockPresignedUrl = mock(URL::class.java) | ||
|
||
`when`("S3에서 PreSigned URL을 생성하면") { | ||
`when`(amazonS3Client.generatePresignedUrl(any(GeneratePresignedUrlRequest::class.java))).thenReturn(mockPresignedUrl) | ||
|
||
then("PreSignedUrlResponse가 반환된다") { | ||
val response = provider.getPreSignedUrl(imageName) | ||
assertNotNull(response) | ||
assertEquals(mockPresignedUrl.toString(), response.url) | ||
} | ||
} | ||
} | ||
|
||
given("파일 이름에 확장자가 없는 경우") { | ||
val imageName = "test_image" // 확장자가 없는 파일명 | ||
|
||
then("InvalidInputException이 발생한다") { | ||
assertFailsWith<InvalidInputException> { | ||
provider.getPreSignedUrl(imageName) | ||
} | ||
} | ||
} | ||
|
||
given("generateULID 함수가 호출되면") { | ||
val ulid = generateULID() | ||
|
||
then("ULID가 생성된다") { | ||
assertNotNull(ulid) | ||
assertTrue(ulid.isNotEmpty()) | ||
} | ||
} | ||
}) |
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