Skip to content

Commit

Permalink
Merge pull request #164 from BETTER-iTER/feature/163
Browse files Browse the repository at this point in the history
[FEATURE-163] s3 의존성 변경 및 적용
  • Loading branch information
choidongkuen authored Feb 5, 2024
2 parents 527007a + d915dcb commit 3d470e1
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 31 deletions.
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-mail'

// s3
implementation platform("io.awspring.cloud:spring-cloud-aws-dependencies:3.1.0")
implementation "io.awspring.cloud:spring-cloud-aws-starter-s3"

implementation 'io.awspring.cloud:spring-cloud-starter-aws:2.4.4'

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@SpringBootApplication(
exclude = {
io.awspring.cloud.autoconfigure.context.ContextInstanceDataAutoConfiguration.class,
io.awspring.cloud.autoconfigure.context.ContextStackAutoConfiguration.class,
io.awspring.cloud.autoconfigure.context.ContextRegionProviderAutoConfiguration.class
}
)
public class BetterIterApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public enum ErrorStatus implements BaseErrorCode {
_IMAGE_FILE_UPLOAD_FAILED(HttpStatus.BAD_REQUEST, "IMAGE_FILE_UPLOAD_FAILED", "이미지 파일 업로드에 실패했습니다."),
_IMAGE_FILE_UPLOAD_REQUEST_IS_NOT_VALID(HttpStatus.BAD_REQUEST, "IMAGE_FILE_UPLOAD_IS_NOT_VALID",
"이미지 파일 업로드 요청 형식이 올바르지 않습니다."),
_IMAGE_FILE_IS_NOT_EXIST(HttpStatus.BAD_REQUEST, "IMAGE_FILE_IS_NOT_EXISTED", "이미지 파일이 존재하지 않습니다."),

// REVIEW_LIKE
_REVIEW_LIKE_NOT_FOUND(HttpStatus.BAD_REQUEST, "REVIEW_LIKE_NOT_FOUND_400", "일치하는 리뷰 좋아요 정보를 찾을 수 없습니다."),
Expand All @@ -93,7 +94,6 @@ public enum ErrorStatus implements BaseErrorCode {
_FOLLOW_ALREADY(HttpStatus.BAD_REQUEST, "FOLLOW_ALREADY_400", "이미 팔로우한 유저입니다."),
_FOLLOW_NOT_MATCH(HttpStatus.BAD_REQUEST, "FOLLOW_NOT_MATCH_400", "팔로우한 유저가 아닙니다."),


;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;

@ConfigurationProperties(prefix = "jwt")
@Getter
@ConstructorBinding
@AllArgsConstructor
@ConfigurationProperties(prefix = "jwt")
public class JwtProperties {

private String bearer;
private String secret;
private String accessHeader;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/* config.properties 패키지 내부에 모든 설정 클래스 위치 */
@Configuration
@EnableJpaAuditing
@ConfigurationPropertiesScan("com.example.betteriter.global.config.properties")
@ConfigurationPropertiesScan({"com.example.betteriter.global.config.properties"})
public class WebConfig {
}

}
34 changes: 34 additions & 0 deletions src/main/java/com/example/betteriter/infra/s3/S3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.betteriter.infra.s3;

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 lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Getter
@Configuration
public class S3Config {

@Value("${spring.cloud.aws.credentials.access-key}")
private String accessKey;

@Value("${spring.cloud.aws.credentials.secret-key}")
private String accessSecret;

@Value("${spring.cloud.aws.region.static}")
private String region;

@Bean
public AmazonS3 s3Client() {
AWSCredentials credentials = new BasicAWSCredentials(accessKey, accessSecret);
return AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(region).build();
}

}
26 changes: 17 additions & 9 deletions src/main/java/com/example/betteriter/infra/s3/S3Service.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.example.betteriter.infra.s3;

import static com.example.betteriter.global.common.code.status.ErrorStatus._IMAGE_FILE_IS_NOT_EXIST;
import static com.example.betteriter.global.common.code.status.ErrorStatus._IMAGE_FILE_NAME_IS_NOT_EXIST;
import static com.example.betteriter.global.common.code.status.ErrorStatus._IMAGE_FILE_UPLOAD_FAILED;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.example.betteriter.fo_domain.review.domain.Review;
import com.example.betteriter.fo_domain.review.domain.ReviewImage;
import com.example.betteriter.fo_domain.review.exception.ReviewHandler;
Expand All @@ -14,9 +18,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;


@Slf4j
@Service
Expand All @@ -25,7 +27,7 @@ public class S3Service implements ImageUploadService {

private static final String FOLDER = "iter";

private final S3Client s3Client;
private final AmazonS3 s3Client;

@Value("${spring.cloud.aws.s3.bucket}")
private String bucketName;
Expand All @@ -39,13 +41,13 @@ public ReviewImage uploadImage(MultipartFile image, Review review, int orderNum)
String fileName = UUID.randomUUID().toString();
String key = FOLDER + "/" + review.getId().toString() + "/" + fileName + fileExtension;

PutObjectRequest request = PutObjectRequest.builder()
.bucket(bucketName)
.key(key)
.build();
ObjectMetadata objectMetaData = new ObjectMetadata();
objectMetaData.setContentType(image.getContentType());

try (InputStream inputStream = image.getInputStream()) {
s3Client.putObject(request, RequestBody.fromInputStream(inputStream, image.getSize()));

s3Client.putObject(new PutObjectRequest(bucketName, key, inputStream, objectMetaData));

} catch (Exception e) {
throw new ReviewHandler(_IMAGE_FILE_UPLOAD_FAILED);
}
Expand All @@ -57,6 +59,12 @@ public ReviewImage uploadImage(MultipartFile image, Review review, int orderNum)
.build();
}

private void validateImageFileExists(MultipartFile multipartFile) {
if (multipartFile.isEmpty()) {
throw new ReviewHandler(_IMAGE_FILE_IS_NOT_EXIST);
}
}


private String getImageUrl(String key) {
return "https://" + bucketName + ".s3.amazonaws.com/" + key;
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/com/example/betteriter/global/ConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.example.betteriter.global;

import com.example.betteriter.infra.s3.S3Config;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.test.context.ContextConfiguration;

@DisplayName("S3Config 설정 클래스는")
@ContextConfiguration(classes = {S3Config.class, ConfigTest.PropertyPlaceholderConfig.class})
public class ConfigTest {

@Autowired
private S3Config s3Config;

@TestConfiguration
static class PropertyPlaceholderConfig {

@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
return new PropertySourcesPlaceholderConfigurer();
}
}

@Nested
@DisplayName("각 필드 값을")
class Get_s3_config_values {

private String accessKey;
private String accessSecret;

@BeforeEach
void setUp() {
accessKey = s3Config.getAccessKey();
accessSecret = s3Config.getAccessSecret();
}

@Test
@DisplayName("정상적으로 가져온다.")
void With_successful() {
// then
System.out.println("accessKey = " + accessKey);
System.out.println("accessSecret = " + accessSecret);
}
}
}

0 comments on commit 3d470e1

Please sign in to comment.