Skip to content
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

[FEAT] AWS S3를 이용한 이미지 업로드 구현 #13

Merged
merged 14 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions .github/workflows/CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,31 @@ jobs:
run: |
docker build --platform linux/amd64 -t yejunpark1/motivoo-server .
docker push yejunpark1/motivoo-server


- name: deploy.sh EC2 인스턴스로 전송
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.RELEASE_SERVER_IP }}
username: ${{ secrets.RELEASE_SERVER_USER }}
key: ${{ secrets.RELEASE_SERVER_KEY }}
source: "./scripts/deploy.sh"
target: "/home/ubuntu/"

- name: docker-compose.yml EC2 인스턴스로 전송
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.RELEASE_SERVER_IP }}
username: ${{ secrets.RELEASE_SERVER_USER }}
key: ${{ secrets.RELEASE_SERVER_KEY }}
source: "./docker-compose.yml"
target: "/home/ubuntu/"

deploy-cd: # job#2
needs: deploy-ci
runs-on: ubuntu-22.04

steps: # EC2 인스턴스 내부에서 이루어지는 작업
- name: 도커 컨테이너 실행
- name: 도커 컨테이너 실행 및 이미지 Pull
uses: appleboy/ssh-action@master

with:
Expand All @@ -65,5 +82,8 @@ jobs:
key: ${{ secrets.RELEASE_SERVER_KEY }}
script: |
cd ~
sudo docker pull yejunpark1/motivoo-server
sudo chmod 777 ./deploy.sh
cp ./scripts/deploy.sh ./deploy.sh
./deploy.sh
docker image prune -f
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ dependencies {
testImplementation "com.epages:restdocs-api-spec-mockmvc:${restdocsApiSpecVersion}"
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'

// AWS sdk
implementation("software.amazon.awssdk:bom:2.21.0")
implementation("software.amazon.awssdk:s3:2.21.0")

}

tasks.named('test') {
Expand Down
32 changes: 32 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version: '3.8'
services:
redis:
container_name: redis
image: redis:alpine
ports:
- "6379:6379"

blue:
container_name: blue
image: yejunpark1/motivoo-server
expose:
- 8080
ports:
- "8080:8080"
environment:
- TZ=Asia/Seoul
depends_on:
- redis


green:
container_name: green
image: yejunpark1/motivoo-server
expose:
- 8080
ports:
- "8081:8080"
environment:
- TZ=Asia/Seoul
depends_on:
- redis
98 changes: 98 additions & 0 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/bin/bash
NOW_TIME="$(date +%Y)-$(date +%m)-$(date +%d) $(date +%H):$(date +%M):$(date +%S)"


IS_GREEN_ACTIVATE=$(docker ps | grep green)
IS_REDIS_ACTIVATE=$(docker ps | grep redis)

# Redis Docker Image Pull
if [ -z $IS_REDIS_ACTIVATE ];then
echo "### REDIS ###"
echo "[$NOW_TIME] Redis 도커 이미지 pull"
docker-compose pull redis
echo "[$NOW_TIME] Redis 컨테이너 Up (빌드 & 실행)"
docker-compose up -d redis
fi

# Green Up
if [ -z $IS_GREEN_ACTIVATE ]; then
echo "[$NOW_TIME] 현재 구동중인 Port: Green(:8080)"

echo "[$NOW_TIME] 스위칭"
echo "[$NOW_TIME] BLUE -> GREEN ###"
echo "[$NOW_TIME] Green 도커 이미지 pull"
docker-compose pull green
echo "[$NOW_TIME] Green 컨테이너 Up (빌드 & 실행)"
docker-compose up -d green

for retry_count in {1..10}; do
echo "[$NOW_TIME] Green health check ..."
sleep 3

# REQUEST=$(curl http://localhost:8081)
RESPONSE=$(curl http://localhost:8081/actuator/health)
UP_COUNT=$(echo $RESPONSE | grep 'UP' | wc -l)

if [ $UP_COUNT -ge 1 ]; then # "UP" 문자열이 1개 이상 존재한다면 헬스체크 통과
echo "[$NOW_TIME] Health check 성공!"
break;
else
echo "[$NOW_TIME] Health check의 응답을 알 수 없거나 status가 UP이 아닙니다."
echo "[$NOW_TIME] Health check 응답: ${RESPONSE}"
fi

if [ $retry_count -eq 10 ]; then
echo "[$NOW_TIME] Health check 실패.."
echo "[$NOW_TIME] Nginx에 연결하지 않고 배포를 종료합니다."
exit 1
fi
done;
sleep 3

echo "[$NOW_TIME] Nginx Reload (Port 스위칭 적용)"
sudo cp /etc/nginx/conf.d/green-url.inc /etc/nginx/conf.d/service-url.inc
sudo nginx -s reload
echo "[$NOW_TIME] Blue 컨테이너 중단"
docker-compose stop blue

# Blue Up
else
echo "[$NOW_TIME] 현재 구동중인 Port: Blue(:8081)"

echo "[$NOW_TIME] 스위칭"
echo "[$NOW_TIME] GREEN -> BLUE ###"
echo "[$NOW_TIME] Blue 도커 이미지 pull"
docker-compose pull blue
echo "[$NOW_TIME] Blue 컨테이너 Up (빌드 & 실행)"
docker-compose up -d blue

for retry_count in {1..10}; do
echo "[$NOW_TIME] Blue health check ..."
sleep 3

# REQUEST=$(curl http://localhost:8080)
RESPONSE=$(curl http://localhost:8080/actuator/health)
UP_COUNT=$(echo $RESPONSE | grep 'UP' | wc -l)

if [ $UP_COUNT -ge 1 ]; then # "UP" 문자열이 1개 이상 존재한다면 헬스체크 통과
echo "[$NOW_TIME] Health check 성공!"
break;
else
echo "[$NOW_TIME] Health check의 응답을 알 수 없거나 status가 UP이 아닙니다."
echo "[$NOW_TIME] Health check 응답: ${RESPONSE}"
fi

if [ $retry_count -eq 10 ]; then
echo "[$NOW_TIME] Health check 실패.."
echo "[$NOW_TIME] Nginx에 연결하지 않고 배포를 종료합니다."
exit 1
fi
done;
sleep 3

echo "[$NOW_TIME] Nginx Reload (Port 스위칭 적용)"
sudo cp /etc/nginx/conf.d/blue-url.inc /etc/nginx/conf.d/service-url.inc
sudo nginx -s reload
echo "[$NOW_TIME] Green 컨테이너 중단"
docker-compose stop green
fi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.IOException;

import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
Expand Down Expand Up @@ -31,9 +32,9 @@ public class GlobalExceptionHandler {
*/
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(final Exception e, final HttpServletRequest request) throws IOException {
slackUtil.sendAlert(e, request);
// slackUtil.sendAlert(e, request);

log.error("🔔🚨 Slack에 전송된 Error Log: {}", e);
log.error("🔔🚨 Slack에 전송된 Error Log: {}", e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ErrorResponse.of(INTERNAL_SERVER_ERROR));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package sopt.org.motivooServer.global.config.aws;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import software.amazon.awssdk.auth.credentials.SystemPropertyCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;

@Configuration
public class AWSConfig {

private static String AWS_ACCESS_KEY_ID = "aws.accessKeyId";
private static String AWS_SECRET_ACCESS_KEY = "aws.secretAccessKey";

private final String accessKey;
private final String secretKey;
private final String regionString;

public AWSConfig(@Value("${aws-property.access-key}") final String accessKey,
@Value("${aws-property.secret-key}") final String secretKey,
@Value("${aws-property.aws-region}") final String regionString) {
this.accessKey = accessKey;
this.secretKey = secretKey;
this.regionString = regionString;
}

@Bean
public SystemPropertyCredentialsProvider systemPropertyCredentialsProvider() {
System.setProperty(AWS_ACCESS_KEY_ID, accessKey);
System.setProperty(AWS_SECRET_ACCESS_KEY, secretKey);
return SystemPropertyCredentialsProvider.create();
}

@Bean
public Region getRegion() {
return Region.of(regionString);
}

@Bean
public S3Client getS3Client() {
return S3Client.builder()
.region(getRegion())
.credentialsProvider(systemPropertyCredentialsProvider())
.build();
}

@Bean
public S3Presigner getS3PreSigner() {
return S3Presigner.builder()
.region(getRegion())
.credentialsProvider(systemPropertyCredentialsProvider())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,35 @@

import static sopt.org.motivooServer.global.response.SuccessType.*;

import java.io.IOException;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import sopt.org.motivooServer.global.response.ApiResponse;
import sopt.org.motivooServer.global.util.s3.PreSignedUrlResponse;
import sopt.org.motivooServer.global.util.s3.S3BucketDirectory;
import sopt.org.motivooServer.global.util.s3.S3Service;

@Slf4j
@RestController
@RequestMapping("/api/health")
@RequiredArgsConstructor
public class HealthCheckController implements HealthCheckApi {

private final S3Service s3Service;

@GetMapping
public ResponseEntity<ApiResponse<String>> healthCheck() {
return ApiResponse.success(HEALTH_CHECK_SUCCESS, "test success!");
Expand All @@ -26,4 +42,31 @@ public ResponseEntity<ApiResponse<String>> healthCheck() {
public ApiResponse<String> healthCheckV2() {
return ApiResponse.successV2(HEALTH_CHECK_SUCCESS);
}


//== AWS S3 버킷 이미지 업로드 테스트 ==//

// Multipart 요청 이용
@PatchMapping("/image")
public ApiResponse<String> uploadImage(@RequestPart MultipartFile image) throws IOException {
String imgUrl = s3Service.uploadImage(S3BucketDirectory.TEST_PREFIX, image);
log.info("S3 버킷에 이미지 업로드 성공!: {}", imgUrl);
return ApiResponse.successV2(IMAGE_S3_UPLOAD_SUCCESS);
}

// PreSigned Url 이용 (클라이언트에서 해당 URL로 업로드)
@PatchMapping("/image/pre")
public ResponseEntity<ApiResponse<PreSignedUrlResponse>> uploadImageByPreSigned(@RequestParam String prefix) throws IOException {
PreSignedUrlResponse preSignedUrl = s3Service.getUploadPreSignedUrl(S3BucketDirectory.of(prefix));
log.info("S3 버킷에 업로드할 이미지 PreSigned URL 반환 성공!: url={}, filename={}", preSignedUrl.url(), preSignedUrl.fileName());
return ApiResponse.success(IMAGE_S3_UPLOAD_SUCCESS, preSignedUrl);
}

// 버킷에서 이미지 삭제
@DeleteMapping("/image")
public ApiResponse<String> deleteImage(@RequestParam("img_url") String imageUrl) throws IOException {
s3Service.deleteImage(imageUrl);
log.info("S3 버킷에서 이미지 삭제 성공!");
return ApiResponse.successV2(IMAGE_S3_DELETE_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public enum SuccessType {
/**
* 200 Ok
*/
HEALTH_CHECK_SUCCESS(HttpStatus.OK, "헬스체크용 API 호출에 성공했습니다.")
HEALTH_CHECK_SUCCESS(HttpStatus.OK, "헬스체크용 API 호출에 성공했습니다."),
IMAGE_S3_UPLOAD_SUCCESS(HttpStatus.OK, "S3 버킷에 이미지를 업로드하는 데 성공했습니다."),
IMAGE_S3_DELETE_SUCCESS(HttpStatus.OK, "S3 버킷에 이미지를 삭제하는 데 성공했습니다.")



Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sopt.org.motivooServer.global.util.s3;

public record PreSignedUrlResponse(
String fileName,
String url
) {

public static PreSignedUrlResponse of(String fileName, String url) {
return new PreSignedUrlResponse(fileName, url);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package sopt.org.motivooServer.global.util.s3;

import static sopt.org.motivooServer.domain.health.exception.HealthExceptionType.*;

import java.util.Arrays;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import sopt.org.motivooServer.global.advice.BusinessException;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public enum S3BucketDirectory {

TEST_PREFIX("test/");

private final String name;

public String value() {
return this.name;
}

public static S3BucketDirectory of(String value) {
return Arrays.stream(S3BucketDirectory.values())
.filter(prefix -> value.equals(prefix.name))
.findFirst()
.orElseThrow(() -> new BusinessException(INVALID_HEALTH_NOTE));
}
}
Loading