-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MARA-43 : AWS S3 이미지 업로드 기능 구현 (#18)
* S3 upload 기능 구현 * 배포 dev 프로파일 설정 적용
- Loading branch information
1 parent
15b7900
commit b219a1e
Showing
6 changed files
with
122 additions
and
3 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
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,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 | ||
} | ||
} |
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,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)) | ||
} | ||
} |
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,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() | ||
} | ||
} |
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