-
Notifications
You must be signed in to change notification settings - Fork 1
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 #8 from donggukthon/dev
✨ [Feat] S3 관련 환경설정 파일과 Configuration 파일을 작성한다.
- Loading branch information
Showing
16 changed files
with
149 additions
and
7 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
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,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(); | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
src/main/java/donggukthon/volunmate/interceptor/post/SuccessResponseAdvice.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,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; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...interceptor/SocialIdArgumentResolver.java → ...rceptor/pre/SocialIdArgumentResolver.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
2 changes: 1 addition & 1 deletion
2
...mate/interceptor/SocialIdInterceptor.java → .../interceptor/pre/SocialIdInterceptor.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
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
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
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
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"; | ||
} | ||
} |
Submodule volunmate_properties
updated
from c30131 to 13e527