-
Notifications
You must be signed in to change notification settings - Fork 6
Amazon S3 적용 및 이미지 저장 기능 구현 완료 #49
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
34 changes: 34 additions & 0 deletions
34
src/main/java/com/livable/server/core/config/S3Config.java
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,34 @@ | ||
| package com.livable.server.core.config; | ||
|
|
||
| import com.amazonaws.auth.AWSCredentials; | ||
| 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 S3Config { | ||
|
|
||
| @Value("${cloud.aws.credentials.accessKey}") | ||
| private String accessKey; | ||
|
|
||
| @Value("${cloud.aws.credentials.secretKey}") | ||
| private String secretKey; | ||
|
|
||
| @Value("${cloud.aws.region.static}") | ||
| private String region; | ||
|
|
||
| @Bean | ||
| public AmazonS3 amazonS3Client() { | ||
| AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); | ||
|
|
||
| return AmazonS3ClientBuilder | ||
| .standard() | ||
| .withCredentials(new AWSStaticCredentialsProvider(credentials)) | ||
| .withRegion(region) | ||
| .build(); | ||
| } | ||
| } |
75 changes: 75 additions & 0 deletions
75
src/main/java/com/livable/server/core/util/S3Uploader.java
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,75 @@ | ||
| package com.livable.server.core.util; | ||
|
|
||
| import com.amazonaws.services.s3.AmazonS3; | ||
| import com.amazonaws.services.s3.model.ObjectMetadata; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Component | ||
| public class S3Uploader { | ||
|
|
||
| private static final List<String> ALLOWED_EXTENSIONS = List.of("jpg", "png", "gif", "jpeg"); | ||
| private static final String EXTENSION_SEPARATOR = "."; | ||
|
|
||
| private final AmazonS3 amazonS3; | ||
|
|
||
| @Value("${cloud.aws.s3.bucket}") | ||
| private String bucket; | ||
|
|
||
| public List<String> saveFile(List<MultipartFile> files) throws IOException { | ||
|
|
||
| List<String> accessUrls = new ArrayList<>(); | ||
|
|
||
| for (MultipartFile file : files) { | ||
| String accessUrl = saveFile(file); | ||
| accessUrls.add(accessUrl); | ||
| } | ||
| return accessUrls; | ||
| } | ||
|
|
||
| public String saveFile(MultipartFile file) throws IOException { | ||
|
|
||
| final String originalFileName = file.getOriginalFilename(); | ||
| assert originalFileName != null; | ||
| final String fileExtension = getFileExtension(originalFileName); | ||
|
|
||
| validationAllowedFileExtension(fileExtension); | ||
| String randomFileName = generateRandomFileName(originalFileName, fileExtension); | ||
|
|
||
| ObjectMetadata metadata = new ObjectMetadata(); | ||
| metadata.setContentLength(file.getSize()); | ||
| metadata.setContentType(file.getContentType()); | ||
|
|
||
| amazonS3.putObject(bucket, randomFileName, file.getInputStream(), metadata); | ||
|
|
||
| return amazonS3.getUrl(bucket, randomFileName).toString(); | ||
| } | ||
|
|
||
| // 랜덤한 파일 이름을 생성하는 메서드 | ||
| private String generateRandomFileName(String originalFileName, String fileExtension) { | ||
| return UUID.randomUUID() + originalFileName + EXTENSION_SEPARATOR + fileExtension; | ||
| } | ||
|
|
||
| // 파일 이름에서 파일 확장자를 추출하는 메서드 | ||
| private String getFileExtension(String originalFileName) { | ||
| return originalFileName | ||
| .substring(originalFileName.lastIndexOf(EXTENSION_SEPARATOR) + 1) | ||
| .toLowerCase(); | ||
|
|
||
| } | ||
|
|
||
| // 파일 확장자를 검증하는 메서드 | ||
| private void validationAllowedFileExtension(String fileExtension) { | ||
| if (!ALLOWED_EXTENSIONS.contains(fileExtension)) { | ||
| throw new IllegalArgumentException("지원하지 않는 파일 확장자 입니다."); | ||
| } | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
생성자로 넣어주세요