Skip to content

Commit

Permalink
Merge pull request #8 from donggukthon/dev
Browse files Browse the repository at this point in the history
✨ [Feat] S3 관련 환경설정 파일과 Configuration 파일을 작성한다.
  • Loading branch information
kurtyoon authored Dec 19, 2023
2 parents 3ba7f6b + 2fde1d2 commit dbf6b74
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 7 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ dependencies {
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

// S3
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.1.RELEASE'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/donggukthon/volunmate/config/S3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package donggukthon.volunmate.config;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Getter
public class S3Config {
@Value("${cloud.aws.access_key}")
private String accessKey;

@Value("${cloud.aws.secret_key}")
private String secretKey;

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

@Value("${cloud.aws.s3_bucket}")
private String bucket;

@Bean
public AmazonS3Client amazonS3Client() {
return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withRegion(region).enablePathStyleAccess()
.build();
}
}
4 changes: 2 additions & 2 deletions src/main/java/donggukthon/volunmate/config/WebMvcConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package donggukthon.volunmate.config;

import donggukthon.volunmate.interceptor.SocialIdArgumentResolver;
import donggukthon.volunmate.interceptor.SocialIdInterceptor;
import donggukthon.volunmate.interceptor.pre.SocialIdArgumentResolver;
import donggukthon.volunmate.interceptor.pre.SocialIdInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/donggukthon/volunmate/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public enum ErrorCode {
* 404 Not Found
*/
NOT_FOUND_ERROR(404, HttpStatus.NOT_FOUND, "요청하신 리소스를 찾을 수 없습니다."),
FILE_NOT_FOUND(404, HttpStatus.NOT_FOUND, "요청하신 파일을 찾을 수 없습니다."),
FILE_UPLOAD_ERROR(404, HttpStatus.NOT_FOUND, "파일 업로드에 실패하였습니다."),

/**
* 405 Method Not Allowed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package donggukthon.volunmate.interceptor.post;

import donggukthon.volunmate.dto.exception.ResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

@RestControllerAdvice
@RequiredArgsConstructor
public class SuccessResponseAdvice implements ResponseBodyAdvice<Object> {

@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}

@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType,
ServerHttpRequest request, ServerHttpResponse response) {
if (body instanceof ResponseDto<?> responseDto) {
HttpStatus status = responseDto.getHttpStatus();
response.setStatusCode(status);
}
return body;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package donggukthon.volunmate.interceptor;
package donggukthon.volunmate.interceptor.pre;

import donggukthon.volunmate.annotation.SocialId;
import donggukthon.volunmate.constant.Constants;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package donggukthon.volunmate.interceptor;
package donggukthon.volunmate.interceptor.pre;

import donggukthon.volunmate.constant.Constants;
import jakarta.servlet.http.HttpServletRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import donggukthon.volunmate.domain.Heart;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface HeartRepository extends JpaRepository<Heart, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import donggukthon.volunmate.domain.Help;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface HelpRepository extends JpaRepository<Help, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import donggukthon.volunmate.domain.Tag;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface TagRepository extends JpaRepository<Tag, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import donggukthon.volunmate.type.EUserType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
@Query("select u.socialId as id, u.userType as userType, u.password as password from User u where u.socialId = :socialId")
Optional<UserSecurityForm> findBySecurityFormBySocialId(String socialId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import donggukthon.volunmate.domain.Volunmate;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface VolunmateRepository extends JpaRepository<Volunmate, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import donggukthon.volunmate.domain.Volunteer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface VolunteerRepository extends JpaRepository<Volunteer, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public class OAuth2Service {
private final JwtUtil jwtUtil;
private final JwtProvider jwtProvider;
private final GoogleOAuth2Util oAuth2Util;
@Value("${security.oauth2.main-url}")
@Value("${security.oauth2.main_url}")
private String MAIN_URL;
@Value("${security.oauth2.signup-url}")
@Value("${security.oauth2.signup_url}")
private String SIGNUP_URL;

public String getRedirectUrl(ELoginProvider provider) {
Expand Down
58 changes: 58 additions & 0 deletions src/main/java/donggukthon/volunmate/utils/S3Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package donggukthon.volunmate.utils;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.PutObjectRequest;
import donggukthon.volunmate.config.S3Config;
import donggukthon.volunmate.exception.CustomException;
import donggukthon.volunmate.exception.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.UUID;

@Component
@RequiredArgsConstructor
public class S3Util {
private final AmazonS3Client amazonS3Client;
private final S3Config s3Config;

public String upload(File file, String fileName, String dirName) {
try {
String fullPath = dirName + fileName;
amazonS3Client.putObject(new PutObjectRequest(s3Config.getBucket(), fullPath, file));

return amazonS3Client.getUrl(s3Config.getBucket(), fullPath).toString();
} catch (AmazonS3Exception e) {
throw new CustomException(ErrorCode.FILE_UPLOAD_ERROR);
}
}

/**
* 절대 경로를 상대 경로로 변환
* @param path
* @return
* @throws URISyntaxException
*/
public String convertPath(String path) throws URISyntaxException {
URI uri = new URI(path);
String res = uri.getPath();
return res.substring(s3Config.getBucket().length() + 2);
}

public void deleteS3File(String path) {
try {
String relativePath = convertPath(path);
amazonS3Client.deleteObject(s3Config.getBucket(), relativePath);
} catch (AmazonS3Exception | URISyntaxException e) {
throw new CustomException(ErrorCode.FILE_NOT_FOUND);
}
}

private String makeFileName(String originFileName) {
return originFileName + "~" + UUID.randomUUID() + ".jpg";
}
}
2 changes: 1 addition & 1 deletion volunmate_properties

0 comments on commit dbf6b74

Please sign in to comment.