스프링에서 파일 업로드 다루기(feat. S3) #6
seonminKim1122
started this conversation in
docs
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
스프링에서는 MultipartFile 이라는 인터페이스를 이용해 업로드 파일을 다룰 수 있도록 지원하고 있습니다.
이번 프로젝트에서는 이미지 파일의 업로드가 가능하도록 했는데 이를 아래와 같은 단계를 통해 처리해주었습니다.
build.gradle 파일에 S3를 이용하기 위한 aws dependency 추가
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6 RELEASEapplication.properties 에 설정 추가
MultipartFile 을 위한 설정 -
spring.servlet.multipart.enabled=truespring.servlet.multipart.location=spring.servlet.multipart.max-request-size=30MBspring.servlet.multipart.max-file-size=10MBS3 이용을 위한 설정 -
cloud.aws.credentials.accessKey=~cloud.aws.credentials.secretKey=~cloud.aws.s3.bucket=버킷이름cloud.aws.region.static=버킷을 생성한 aws regioncloud.aws.stack.auto=false유저가 업로드한 파일들의 이름이 서로 동일할 수가 있기에 식별 가능하도록 UUID를 사용해줍니다.
amazonS3.putObject() 메서드를 이용해 S3 버킷에 파일을 저장할 수 있고
amazonS3.getUrl() 메서드를 이용해 저장된 파일의 경로를 가져올 수 있습니다.
이번 프로젝트에서는 이렇게 가져온 경로를 DB에 저장하고 프론트에서 요청 시 경로를 전달해 이미지를 활용할 수 있도록 하였습니다.
S3 버켓에 저장된 파일(객체)을 제거하기 위해서는 저장된 파일의 이름을 알아야 합니다.
DB에는 http://~~~~/~~/~.jpeg 와 같이 경로가 저장되어 있기에 파일의 이름만을 추출하고자 "/"을 기준으로 split 해서 사용했습니다.
Beta Was this translation helpful? Give feedback.
All reactions