-
[Kotlin] AWS S3에 업로드하기프로젝트/미디어 스트리밍 서버 프로젝트 2022. 12. 19. 00:01반응형
목표
- API를 통해 S3 bucket에 업로드를 수행한다(또는 localstack 테스트)
locakstack이란?
오픈소스로 만들어진 프레임워크입니다.
가장 큰 특징으로 클라우드 환경을 로컬에서 직접 구현해서 테스트할 수 있습니다.
AWS 관련의 Application들을 Docker 컨테이너로 만들어서 Cloud환경과 동일한 환경을 구축할 수 있습니다.
사전 세팅
- 현재 S3 bucket이 public access 가능한 상황입니다.(참고 : https://zzang9ha.tistory.com/358)
- Spring initializr(Gradle Project, Kotlin, Spring Boot 2.7.4, Jar, Java 11) + dependencies(spring web)
- Postman
1. 의존성 추가하기
build.gradle.kts
implementation ("io.awspring.cloud:spring-cloud-starter-aws:2.3.1")
2. application.properties 설정
# AWS Account Credentials (AWS 접근 키) cloud.aws.credentials.accessKey={액세스키} cloud.aws.credentials.secretKey={액세스 시크릿 키} # AWS S3 bucket Info (S3 버킷정보) cloud.aws.s3.bucket={S3 버킷 이름) cloud.aws.s3.dir = {S3 버킷 디렉터리} (선택) cloud.aws.region.static=ap-northeast-2 (S3 버킷 지역) cloud.aws.stack.auto=false # file upload max size (파일 업로드 크기 설정) spring.servlet.multipart.max-file-size=20MB spring.servlet.multipart.max-request-size=20MB
실제 예시
# AWS Account Credentials (AWS 접근 키) cloud.aws.credentials.accessKey=yourAccessKey cloud.aws.credentials.secretKey=yourSecretKey # AWS S3 bucket Info (S3 버킷정보) cloud.aws.s3.bucket=yourBucketName cloud.aws.s3.dir=some-dir/ cloud.aws.region.static=ap-northeast-2 cloud.aws.stack.auto=false # file upload max size (파일 업로드 크기 설정) spring.servlet.multipart.max-file-size=20000MB spring.servlet.multipart.max-request-size=20000MB
3. Controller
@RestController class HelloController(private val helloService: HelloService) { @PostMapping("/upload") fun upload(@RequestPart("file") multipartFile: MultipartFile): String { return helloService.upload(multipartFile) } }
4. Service
@Service class HelloService() { @Value("\${cloud.aws.s3.bucket}") lateinit var bucket: String @Value("\${cloud.aws.s3.dir}") lateinit var dir: String @Throws(IOException::class) fun upload(file: MultipartFile): String { val s3Client: AmazonS3 = AmazonS3ClientBuilder.standard() .withRegion("ap-northeast-2") .build() 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)) return s3Client.getUrl(bucket, dir + fileName).toString() } }
PostMan 테스트
참고자료
https://www.sunny-son.space/spring/Springboot로%20S3%20파일%20업로드/
Springboot로 S3 파일 업로드하기
이번 포스팅은 스프링에서 AWS S3 파일 업로드하는 방법입니다. 주로 이미지 파일을 올릴 때 많이 사용되곤 합니다. 1. 의존성 추가하기 build.gradle awspring/spring-cloud-aws Spring-Cloud-AWS 의존성을 추가합
www.sunny-son.space
https://devlog-wjdrbs96.tistory.com/403
[Spring] Kotlin 으로 AWS S3 파일 업로드 하는 법
Kotlin, Spring Boot로 S3에 파일 업로드 하는 법 이번 글에서는 Kotlin 코드로 AWS S3에 파일 업로드 하는 방법에 대해서 알아보겠습니다. AWS S3 버킷을 생성하는 방법에 대해서는 다루지 않을 것이라 혹
devlog-wjdrbs96.tistory.com
'프로젝트 > 미디어 스트리밍 서버 프로젝트' 카테고리의 다른 글
Adaptive Bitrate와 MPD파일 (0) 2023.01.05 스프링과 스트리밍 서버 (0) 2022.12.23 AWS Foundation 기반 온디맨드 비디오 솔루션 구현 - 이론편 (0) 2022.12.12 OTT는 어떻게 콘텐츠를 보호할까? (0) 2022.12.08 리팩토링에 대하여 (0) 2022.10.08