-
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.
Merge pull request #30 from Stumeet/dev ✨ [STMT-146] S3를 활용한 사용자 프로필 …
- Loading branch information
Showing
9 changed files
with
209 additions
and
4 deletions.
There are no files selected for viewing
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
20 changes: 20 additions & 0 deletions
20
src/main/java/com/stumeet/server/common/annotation/StorageAdapter.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,20 @@ | ||
package com.stumeet.server.common.annotation; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.springframework.core.annotation.AliasFor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Target({ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
@Component | ||
public @interface StorageAdapter { | ||
|
||
@AliasFor(annotation = Component.class) | ||
String value() default ""; | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/stumeet/server/common/config/AwsS3Config.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,42 @@ | ||
package com.stumeet.server.common.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.regions.Region; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class AwsS3Config { | ||
|
||
@Value("${spring.cloud.config.server.awss3.region}") | ||
private String region; | ||
|
||
@Value("${spring.cloud.config.server.awss3.credentials.access-key}") | ||
private String accessKey; | ||
|
||
@Value("${spring.cloud.config.server.awss3.credentials.secret-key}") | ||
private String secretKey; | ||
|
||
private AwsBasicCredentials awsBasicCredentials() { | ||
return AwsBasicCredentials.create(accessKey, secretKey); | ||
} | ||
|
||
@Bean | ||
public StaticCredentialsProvider staticCredentialsProvider() { | ||
return StaticCredentialsProvider.create(awsBasicCredentials()); | ||
} | ||
|
||
@Bean | ||
public S3Client s3Client() { | ||
return S3Client.builder() | ||
.credentialsProvider(staticCredentialsProvider()) | ||
.region(Region.of(region)) | ||
.build(); | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
src/main/java/com/stumeet/server/common/util/FileUtil.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,49 @@ | ||
package com.stumeet.server.common.util; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.HashSet; | ||
import java.util.Locale; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
import com.stumeet.server.common.exception.model.BusinessException; | ||
import com.stumeet.server.common.response.ErrorCode; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class FileUtil { | ||
|
||
private static final Set<String> VALID_CONTENT_TYPES = new HashSet<>(); | ||
|
||
static { | ||
VALID_CONTENT_TYPES.add("jpg"); | ||
VALID_CONTENT_TYPES.add("jpeg"); | ||
VALID_CONTENT_TYPES.add("png"); | ||
} | ||
|
||
public static String getContentType(String fileName) { | ||
if (fileName.isEmpty()) { | ||
throw new BusinessException(ErrorCode.INVALID_IMAGE_EXCEPTION); | ||
} | ||
|
||
String contentType = fileName | ||
.substring(fileName.lastIndexOf(".") + 1) | ||
.toLowerCase(Locale.ROOT); | ||
|
||
if (!VALID_CONTENT_TYPES.contains(contentType)) { | ||
throw new BusinessException(ErrorCode.INVALID_FILE_EXTENSION_EXCEPTION); | ||
} | ||
|
||
return contentType; | ||
} | ||
|
||
public static String createFileName(String directoryPath, String fileName) { | ||
String dateTime = LocalDateTime.now() | ||
.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")); | ||
|
||
return String.format("%s/%s%s-%s", directoryPath, dateTime, UUID.randomUUID(), fileName); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/com/stumeet/server/file/adapter/out/S3ImageStorageAdapter.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,66 @@ | ||
package com.stumeet.server.file.adapter.out; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import com.stumeet.server.common.annotation.StorageAdapter; | ||
import com.stumeet.server.common.exception.model.BusinessException; | ||
import com.stumeet.server.common.response.ErrorCode; | ||
import com.stumeet.server.common.util.FileUtil; | ||
import com.stumeet.server.file.application.port.out.FileCommandPort; | ||
import com.stumeet.server.file.application.port.out.FileUrl; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import software.amazon.awssdk.core.sync.RequestBody; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
|
||
@StorageAdapter | ||
@RequiredArgsConstructor | ||
public class S3ImageStorageAdapter implements FileCommandPort { | ||
|
||
@Value("${spring.cloud.config.server.awss3.bucket}") | ||
private String bucket; | ||
|
||
@Value("${spring.cloud.config.server.awss3.endpoint}") | ||
private String endpoint; | ||
|
||
private final S3Client s3Client; | ||
|
||
@Override | ||
public FileUrl uploadImageFile(MultipartFile multipartFile, String directoryPath) { | ||
String originalFileName = multipartFile.getOriginalFilename(); | ||
String key = FileUtil.createFileName(directoryPath, originalFileName); | ||
|
||
PutObjectRequest objectRequest = PutObjectRequest.builder() | ||
.contentType(FileUtil.getContentType(originalFileName)) | ||
.bucket(bucket) | ||
.key(key) | ||
.build(); | ||
|
||
try { | ||
s3Client.putObject( | ||
objectRequest, | ||
RequestBody | ||
.fromInputStream( | ||
multipartFile.getInputStream(), | ||
multipartFile.getSize())); | ||
} catch (IOException e) { | ||
throw new BusinessException(ErrorCode.UPLOAD_FILE_FAIL_ERROR); | ||
} | ||
|
||
return getS3FileUrl(key); | ||
} | ||
|
||
@Override | ||
public FileUrl uploadImageFiles(List<MultipartFile> multipartFileList, String directoryPath) { | ||
return null; | ||
} | ||
|
||
private FileUrl getS3FileUrl(String key) { | ||
return new FileUrl(String.format("%s/%s", endpoint, key)); | ||
} | ||
} |
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
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
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