Skip to content

Commit

Permalink
MARA-43 : AWS S3 이미지 업로드 기능 구현 (#18)
Browse files Browse the repository at this point in the history
* S3 upload 기능 구현

* 배포 dev 프로파일 설정 적용
  • Loading branch information
jhkang1517 authored Feb 3, 2024
1 parent 15b7900 commit b219a1e
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 3 deletions.
13 changes: 10 additions & 3 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on:

env:
S3_BUCKET_NAME: mara-s3bucket
AWS_ACCESS_KEY_ID: ${{secrets.AWS_ACCESS_KEY_ID}}
AWS_SECRET_ACCESS_KEY: ${{secrets.AWS_SECRET_ACCESS_KEY}}
AWS_REGION: ${{secrets.AWS_REGION}}
RESOURCE_PATH: ./src/main/resources/application-dev.yml
CODE_DEPLOY_APPLICATION_NAME: mara-code-deploy
CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: mara-deploy-group
Expand Down Expand Up @@ -39,6 +42,10 @@ jobs:
oauth.google.client-id: ${{ secrets.GOOGLE_CLIENT_ID }}
oauth.google.client-secret: ${{ secrets.GOOGLE_CLIENT_SECRET }}
jwt.secret-key: ${{ secrets.JWT_SECRET_KEY }}
cloud.aws.credentials.accessKey: $AWS_ACCESS_KEY_ID
cloud.aws.credentials.secretKey: $AWS_SECRET_ACCESS_KEY
cloud.aws.region.static: $AWS_REGION


- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
Expand All @@ -58,9 +65,9 @@ jobs:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
aws-access-key-id: $AWS_ACCESS_KEY_ID
aws-secret-access-key: $AWS_SECRET_ACCESS_KEY
aws-region: $AWS_REGION

# [5]
- name: Upload to S3
Expand Down
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ dependencies {

// db
runtimeOnly("org.mariadb.jdbc:mariadb-java-client")

// s3
implementation("org.springframework.cloud:spring-cloud-starter-aws:2.0.1.RELEASE")
implementation("javax.xml.bind:jaxb-api:2.3.1")
}

tasks.withType<KotlinCompile> {
Expand Down
32 changes: 32 additions & 0 deletions src/main/kotlin/mara/server/config/s3/S3Config.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package mara.server.config.s3

import com.amazonaws.auth.AWSStaticCredentialsProvider
import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.AmazonS3ClientBuilder
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class S3Config {
@Value("\${cloud.aws.credentials.accessKey}")
private lateinit var accesskey: String

@Value("\${cloud.aws.credentials.secretKey}")
private lateinit var secretKey: String

@Value("\${cloud.aws.region.static}")
private lateinit var region: String

@Bean
fun amazonS3Client(): AmazonS3Client {
val basicAWSCredentials: BasicAWSCredentials = BasicAWSCredentials(accesskey, secretKey)
return AmazonS3ClientBuilder
.standard()
.withRegion(region)
.withCredentials(AWSStaticCredentialsProvider(basicAWSCredentials))
.build()
as AmazonS3Client
}
}
25 changes: 25 additions & 0 deletions src/main/kotlin/mara/server/domain/s3/S3Controller.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package mara.server.domain.s3

import mara.server.common.CommonResponse
import mara.server.common.success
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile

@RequestMapping("/s3")
@RestController
class S3Controller(
private val s3Service: S3Service
) {

@PostMapping("/upload", consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
fun fileUpload(
@RequestParam("image") multipartFile: MultipartFile,
@RequestParam("dir", required = false) customDir: String?
): CommonResponse<String> {
return success(s3Service.upload(multipartFile, customDir))
}
}
38 changes: 38 additions & 0 deletions src/main/kotlin/mara/server/domain/s3/S3Service.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package mara.server.domain.s3

import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.services.s3.model.CannedAccessControlList
import com.amazonaws.services.s3.model.ObjectMetadata
import com.amazonaws.services.s3.model.PutObjectRequest
import com.amazonaws.util.IOUtils
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import java.io.ByteArrayInputStream
import java.util.UUID

@Service
class S3Service(
private val s3Client: AmazonS3Client,
@Value("\${cloud.aws.s3.bucket}")
private val bucket: String,
@Value("\${cloud.aws.s3.dir}")
private val dir: String
) {
fun upload(file: MultipartFile, customDir: String? = dir): String {
val fileName = UUID.randomUUID().toString() + "-" + file.originalFilename
val objMeta = ObjectMetadata()

val bytes = IOUtils.toByteArray(file.inputStream)
objMeta.contentLength = bytes.size.toLong()

val byteArrayIs = ByteArrayInputStream(bytes)

s3Client.putObject(
PutObjectRequest(bucket, dir + fileName, byteArrayIs, objMeta)
.withCannedAcl(CannedAccessControlList.PublicRead)
)

return s3Client.getUrl(bucket, dir + fileName).toString()
}
}
13 changes: 13 additions & 0 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ oauth:
auth: https://oauth2.googleapis.com
api: https://www.googleapis.com

cloud:
aws:
credentials:
accessKey: ${aws-access-key-id}
secretKey: ${aws-secret-access-key}
s3:
bucket: mara-s3bucket
dir: upload/
region:
static: ${aws-region}
# stack:
# auto: false

jwt:
secret-key: ${jwt-secret-key}
access-duration-mils: 2147483647
Expand Down

0 comments on commit b219a1e

Please sign in to comment.