Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor | CAKK-62 | external 모듈 kt 전환 #204

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
Loading