Skip to content

Commit

Permalink
Refactor | CAKK-62 | external 모듈 kt 전환
Browse files Browse the repository at this point in the history
* Rename .java to .kt

* Refactor | CAKK-62 | kotlin으로 전환

* Test | CAKK-62 | 테스트 코드 수정
  • Loading branch information
lcomment authored Sep 3, 2024
1 parent 757f811 commit 75ed636
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void getImageUrl() throws Exception {
// when & then
mockMvc.perform(get("/aws/img"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.imagePath").value(presignedUrl.imagePath()))
.andExpect(jsonPath("$.data.imageUrl").value(presignedUrl.imageUrl()))
.andExpect(jsonPath("$.data.presignedUrl").value(presignedUrl.presignedUrl()));
.andExpect(jsonPath("$.data.imagePath").value(presignedUrl.getImagePath()))
.andExpect(jsonPath("$.data.imageUrl").value(presignedUrl.getImageUrl()))
.andExpect(jsonPath("$.data.presignedUrl").value(presignedUrl.getPresignedUrl()));
}
}
46 changes: 0 additions & 46 deletions cakk-external/src/main/java/com/cakk/external/config/S3Config.java

This file was deleted.

33 changes: 33 additions & 0 deletions cakk-external/src/main/java/com/cakk/external/config/S3Config.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.cakk.external.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.Primary

import com.amazonaws.auth.AWSStaticCredentialsProvider
import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.services.s3.AmazonS3
import com.amazonaws.services.s3.AmazonS3ClientBuilder

@Configuration
class S3Config(
@Value("\${cloud.aws.credentials.access-key}") private val accessKey: String,
@Value("\${cloud.aws.credentials.secret-key}") private val secretKey: String,
@Value("\${cloud.aws.region.static}") private val region: String
) {
@Bean
@Primary
fun awsCredentialsProvider(): BasicAWSCredentials {
return BasicAWSCredentials(accessKey, secretKey)
}

@Bean
fun amazonS3(): AmazonS3 {
return AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(AWSStaticCredentialsProvider(awsCredentialsProvider()))
.build()
}
}

This file was deleted.

70 changes: 70 additions & 0 deletions cakk-external/src/main/java/com/cakk/external/service/S3Service.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.cakk.external.service

import java.util.*

import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service

import com.amazonaws.AmazonServiceException
import com.amazonaws.HttpMethod
import com.amazonaws.SdkClientException
import com.amazonaws.services.s3.AmazonS3
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest

import com.cakk.common.enums.ReturnCode
import com.cakk.common.exception.CakkException
import com.cakk.external.vo.PresignedUrl

@Service
class S3Service(
private val amazonS3: AmazonS3,
@Value("\${cloud.aws.s3.bucket}") private val bucket: String,
@Value("\${cloud.aws.s3.expire-in}") private val expiredIn: String,
@Value("\${cloud.aws.s3.object-key}") private val objectKey: String
) {

fun getPresignedUrlWithImagePath(): PresignedUrl {
try {
val imagePath = makeObjectKey()
val imageUrl = getImageUrl(imagePath)
val generatePresignedUrlRequest = createGeneratePresignedUrlRequestInstance(imagePath)
val presignedUrl = generatePresignedUrlRequest(generatePresignedUrlRequest)

return PresignedUrl(imagePath, imageUrl, presignedUrl)
} catch (e: SdkClientException) {
throw CakkException(ReturnCode.EXTERNAL_SERVER_ERROR)
}
}

fun deleteObject(imagePath: String) {
try {
amazonS3.deleteObject(bucket, imagePath)
} catch (e: AmazonServiceException) {
throw CakkException(ReturnCode.EXTERNAL_SERVER_ERROR)
}
}

private fun createGeneratePresignedUrlRequestInstance(imagePath: String): GeneratePresignedUrlRequest {
val expiration = Date()
var expirationInMs = expiration.time
expirationInMs += expiredIn.toLong()
expiration.time = expirationInMs

return GeneratePresignedUrlRequest(bucket, imagePath)
.withMethod(HttpMethod.PUT)
.withExpiration(expiration)
}

private fun generatePresignedUrlRequest(generatePresignedUrlRequest: GeneratePresignedUrlRequest): String {
return amazonS3.generatePresignedUrl(generatePresignedUrlRequest).toString()
}

private fun makeObjectKey(): String {
return StringBuffer().append(objectKey).append("/").append(UUID.randomUUID()).append(".jpeg").toString()
}

private fun getImageUrl(imagePath: String): String {
return amazonS3.getUrl(bucket, imagePath).toString()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cakk.external.vo

data class CertificationMessage(
val businessRegistrationImageUrl: String,
val idCardImageUrl: String,
val emergencyContact: String,
val message: String,
val userId: Long,
val userEmail: String,
val shopName: String,
val latitude: Double,
val longitude: Double
)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.cakk.external.vo

data class PresignedUrl(
val imagePath: String,
val imageUrl: String,
val presignedUrl: String
)

0 comments on commit 75ed636

Please sign in to comment.