-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: timezone seoul 설정 * chore: 깃 템플릿 추가, readme.md 추가 * chore: 예외처리, 전역 환경 설정 외 * feat: 데이터 모델링 * chore: 🐳 cicd 설정 * chore: 🐳 workflow lowercase 로 수정 * chore: 디스코드 webhook 테스트 * chore: :global: cors 설정 * chore: 🐳 cors 변경사항 yml 재적용 * feat: ✨ 팀(방) 생성 기능 구현 (#9) * feat: 목표 점검(캘린더) 기능 구현 (#11) * feat: ✨ 초대코드 이메일 전송 기능 구현 (#13) * feat: s3 설정 추가 (#15)
- Loading branch information
1 parent
7eb9c74
commit 3399b45
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/com/goormdari/global/config/s3/AmazonS3Config.java
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,28 @@ | ||
package com.goormdari.global.config.s3; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
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 | ||
public class AmazonS3Config { | ||
|
||
@Value("${cloud.aws.credentials.access-key}") | ||
private String accessKey; | ||
@Value("${cloud.aws.credentials.secret-key}") | ||
private String secretKey; | ||
@Value("${cloud.aws.region.static}") | ||
private String region; | ||
@Bean | ||
public AmazonS3 amazonS3() { | ||
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); | ||
return AmazonS3ClientBuilder.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) | ||
.build(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/goormdari/global/config/s3/S3Service.java
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,43 @@ | ||
package com.goormdari.global.config.s3; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import com.amazonaws.services.s3.model.PutObjectResult; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class S3Service { | ||
private final AmazonS3 amazonS3; | ||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
private String changedImageName(String originName) { //이미지 이름 중복 방지를 위해 랜덤으로 생성 | ||
String random = UUID.randomUUID().toString(); | ||
return random + originName; | ||
} | ||
|
||
public String uploadImageToS3(MultipartFile image) { //이미지를 S3에 업로드하고 이미지의 url을 반환 | ||
String originName = image.getOriginalFilename(); //원본 이미지 이름 | ||
String ext = originName.substring(originName.lastIndexOf(".")); //확장자 | ||
String changedName = changedImageName(originName); //새로 생성된 이미지 이름 | ||
ObjectMetadata metadata = new ObjectMetadata(); //메타데이터 | ||
metadata.setContentLength(image.getSize()); // 파일의 크기 설정 | ||
metadata.setContentType("image/" + ext); | ||
try { | ||
PutObjectResult putObjectResult = amazonS3.putObject(new PutObjectRequest( | ||
bucket, changedName, image.getInputStream(), metadata | ||
)); | ||
} catch (IOException e) { | ||
throw new RuntimeException("ImageUploadException"); //커스텀 예외 던짐. | ||
} | ||
return amazonS3.getUrl(bucket, changedName).toString(); //데이터베이스에 저장할 이미지가 저장된 주소 | ||
} | ||
} |