-
Notifications
You must be signed in to change notification settings - Fork 0
[Feature] 장소 이미지 업로드 #59
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
da8efab
chore: update git ignore
hoyadong1 2b2d26a
feat: s3 health check 추가
hoyadong1 4751bb8
chore: update gitignore
hoyadong1 19536bb
Merge remote-tracking branch 'origin/dev' into feature/57-place-image…
hoyadong1 af77db8
chore: 패키지 구조 변경
hoyadong1 1236d41
feat: 이미지 업로드 기능 추가
hoyadong1 0efdd7b
style: 컨트롤러 이름 변경
hoyadong1 3730a9f
chore: 패키지 구조 변경
hoyadong1 47ae283
feat: UploadPlaceImageResponse dto 추가
hoyadong1 73a768a
feat: 이미지 용량 제한 처리 추가
hoyadong1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,3 +1,8 @@ | ||
| DB_URL= | ||
| DB_NAME= | ||
| DB_USERNAME= | ||
| DB_PASSWORD= | ||
| DB_PASSWORD= | ||
|
|
||
| NAVER_S3_ACCESS_KEY= | ||
| NAVER_S3_SECRET_KEY= | ||
| NAVER_S3_BUCKET= |
This file contains hidden or 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
36 changes: 36 additions & 0 deletions
36
server/src/main/kotlin/com/andone/memorip/common/config/StorageConfig.kt
This file contains hidden or 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,36 @@ | ||
| package com.andone.memorip.common.config | ||
|
|
||
| import org.springframework.beans.factory.annotation.Value | ||
| import org.springframework.context.annotation.Bean | ||
| import org.springframework.context.annotation.Configuration | ||
| import software.amazon.awssdk.auth.credentials.AwsBasicCredentials | ||
| import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider | ||
| import software.amazon.awssdk.regions.Region | ||
| import software.amazon.awssdk.services.s3.S3Client | ||
| import software.amazon.awssdk.services.s3.S3Configuration | ||
| import java.net.URI | ||
|
|
||
| @Configuration | ||
| class StorageConfig { | ||
|
|
||
| @Bean | ||
| fun s3Client( | ||
| @Value("\${naver.s3.access-key}") accessKey: String, | ||
| @Value("\${naver.s3.secret-key}") secretKey: String, | ||
| @Value("\${naver.s3.endpoint}") endpoint: String, | ||
| @Value("\${naver.s3.region}") region: String | ||
| ): S3Client { | ||
| val credentials = AwsBasicCredentials.create(accessKey, secretKey) | ||
|
|
||
| return S3Client.builder() | ||
| .endpointOverride(URI.create(endpoint)) | ||
| .region(Region.of(region)) | ||
| .credentialsProvider(StaticCredentialsProvider.create(credentials)) | ||
| .serviceConfiguration( | ||
| S3Configuration.builder() | ||
| .pathStyleAccessEnabled(true) | ||
| .build() | ||
| ) | ||
| .build() | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
server/src/main/kotlin/com/andone/memorip/infra/storage/StorageController.kt
This file contains hidden or 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,31 @@ | ||
| package com.andone.memorip.infra.storage | ||
|
|
||
| import com.andone.memorip.common.response.ApiResult | ||
| import org.springframework.web.bind.annotation.GetMapping | ||
| import org.springframework.web.bind.annotation.PostMapping | ||
| import org.springframework.web.bind.annotation.RequestMapping | ||
| import org.springframework.web.bind.annotation.RequestPart | ||
| import org.springframework.web.bind.annotation.RestController | ||
| import org.springframework.web.multipart.MultipartFile | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/s3") | ||
| class StorageController( | ||
| private val service: StorageService | ||
| ) { | ||
|
|
||
| @GetMapping("/health") | ||
| fun health(): String { | ||
| service.check() | ||
| return "OK" | ||
| } | ||
|
|
||
| @PostMapping("/upload/place") | ||
| fun uploadPlaceImage( | ||
| @RequestPart file: MultipartFile | ||
| ): ApiResult<String> { | ||
| return ApiResult.success( | ||
| data = service.upload(file, dir = "place") | ||
| ) | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
server/src/main/kotlin/com/andone/memorip/infra/storage/StorageService.kt
This file contains hidden or 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,48 @@ | ||
| package com.andone.memorip.infra.storage | ||
|
|
||
| import org.springframework.beans.factory.annotation.Value | ||
| import org.springframework.stereotype.Service | ||
| import software.amazon.awssdk.services.s3.S3Client | ||
| import org.springframework.web.multipart.MultipartFile | ||
| import software.amazon.awssdk.core.sync.RequestBody | ||
| import software.amazon.awssdk.services.s3.model.PutObjectRequest | ||
| import java.util.UUID | ||
|
|
||
| @Service | ||
| class StorageService( | ||
| private val s3Client: S3Client, | ||
| @Value("\${naver.s3.bucket}") private val bucket: String | ||
| ) { | ||
| fun check() { | ||
| s3Client.headBucket { it.bucket(bucket) } | ||
| } | ||
|
|
||
| fun upload( | ||
| file: MultipartFile, | ||
| dir: String | ||
| ): String { | ||
|
|
||
| require(value = !file.isEmpty) { "빈 파일입니다" } | ||
|
|
||
| val extension = file.originalFilename | ||
| ?.substringAfterLast('.', "") | ||
| ?.lowercase() | ||
| ?: throw IllegalArgumentException("파일 확장자가 없습니다") | ||
|
|
||
| val key = "$dir/${UUID.randomUUID()}.$extension" | ||
|
|
||
| s3Client.putObject( | ||
| PutObjectRequest.builder() | ||
| .bucket(bucket) | ||
| .key(key) | ||
| .contentType(file.contentType) | ||
| .contentLength(file.size) | ||
| .build(), | ||
| RequestBody.fromInputStream(file.inputStream, file.size) | ||
| ) | ||
|
|
||
| return s3Client.utilities() | ||
| .getUrl { it.bucket(bucket).key(key) } | ||
| .toExternalForm() | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.