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 : s3 업로드 #82

Merged
merged 4 commits into from
Aug 5, 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
15 changes: 14 additions & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ jobs:
WAS_HOST: ${{ secrets.WAS_HOST }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
CLIENT_ID: ${{ secrets.CLIENT_ID }}
AWS_S3_BUCKET_NAME: ${{ secrets.AWS_S3_BUCKET_NAME }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ap-northeast-2

steps:
- name: Checkout repository
uses: actions/checkout@v3
Expand Down Expand Up @@ -62,6 +67,10 @@ jobs:
--build-arg DB_PROD_PASSWD=${{ secrets.DB_PROD_PASSWD }} \
--build-arg JWT_SECRET=${{ secrets.JWT_SECRET }} \
--build-arg CLIENT_ID=${{ secrets.CLIENT_ID }} \
--build-arg AWS_S3_BUCKET_NAME=${{ secrets.AWS_S3_BUCKET_NAME }} \
--build-arg AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }} \
--build-arg AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }} \
--build-arg AWS_REGION=ap-northeast-2 \
-t likelion12/likelion12baby:latest .
docker images
docker push likelion12/likelion12baby:latest
Expand Down Expand Up @@ -89,8 +98,12 @@ jobs:
-e DB_PROD_PASSWD=${{ secrets.DB_PROD_PASSWD }} \
-e JWT_SECRET=${{ secrets.JWT_SECRET }} \
-e CLIENT_ID=${{ secrets.CLIENT_ID }} \
-e AWS_S3_BUCKET_NAME=${{ secrets.AWS_S3_BUCKET_NAME }} \
-e AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }} \
-e AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }} \
-e AWS_REGION=ap-northeast-2 \
-p 8080:8080 ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_NAME }}

sudo docker logs $(sudo docker ps -q --filter ancestor=${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_NAME }})
sudo docker logs $(sudo docker ps -q filter ancestor=${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_NAME }})

sudo docker image prune -f
9 changes: 9 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ dependencies {
implementation 'org.json:json:20210307'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'javax.servlet:javax.servlet-api:4.0.1' // 서블릿 API 추가

// AWS SDK
implementation platform('com.amazonaws:aws-java-sdk-bom:1.11.228')
implementation 'com.amazonaws:aws-java-sdk-s3'
// JAXB API
implementation 'javax.xml.bind:jaxb-api:2.3.1'

// JAXB runtime dependencies
implementation 'org.glassfish.jaxb:jaxb-runtime:2.3.1'
}

tasks.named('test') {
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/example/likelion12/config/S3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.likelion12.config;

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

@Configuration
public class S3Config {

@Value("${spring.s3.bucket-name}")
private String bucketName;

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

@Value("${spring.s3.secret-key}")
private String secretKey;

@Value("${spring.s3.region}")
private String region;

@Bean
public AmazonS3 amazonS3() {
BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey);
return AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.build();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Slf4j
Expand All @@ -26,11 +27,12 @@ public class CrewController {
*/
@PostMapping("")
public BaseResponse<PostCrewResponse> createCrew(@RequestHeader("Authorization") String authorization,
@RequestBody PostCrewRequest postCrewRequest){
@ModelAttribute PostCrewRequest postCrewRequest) throws IOException {
log.info("[CrewController.createCrew]");
Long memberId = jwtProvider.extractIdFromHeader(authorization);
return new BaseResponse<>(crewService.createCrew(memberId, postCrewRequest));
}

/**
* 크루 조회
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.example.likelion12.common.response.BaseResponse;
import com.example.likelion12.common.response.status.BaseExceptionResponseStatus;
import com.example.likelion12.dto.crew.GetCrewInquiryResponse;
import com.example.likelion12.dto.socialring.*;
import com.example.likelion12.service.SocialringService;
import com.example.likelion12.util.*;
Expand All @@ -11,6 +10,7 @@
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

Expand All @@ -29,8 +29,7 @@ public class SocialringController {
*/
@PostMapping("")
public BaseResponse<PostSocialringResponse> createSocialring(@RequestHeader("Authorization") String authorization ,
@RequestBody PostSocialringRequest postSocialringRequest)
{ //헤더에서 소셜링 등록하는 멤버아이디 찾기
@RequestBody PostSocialringRequest postSocialringRequest) throws IOException { //헤더에서 소셜링 등록하는 멤버아이디 찾기
Long memberId = jwtProvider.extractIdFromHeader(authorization);
return new BaseResponse<>(socialringService.createSocialring(memberId,postSocialringRequest));
}
Expand All @@ -39,9 +38,9 @@ public BaseResponse<PostSocialringResponse> createSocialring(@RequestHeader("Aut
* 소셜링 수정
*/
@PatchMapping("")
public BaseResponse<Void> modifySocialring(@RequestHeader("Authorization") String authorization, @RequestParam String socialringName,
@RequestBody PatchSocialringModifyRequest patchSocialringModifyRequest)
{
public BaseResponse<Void> modifySocialring(@RequestHeader("Authorization") String authorization, @RequestParam long socialringId,
@RequestBody PatchSocialringModifyRequest patchSocialringModifyRequest) throws IOException {

Long memberId = jwtProvider.extractIdFromHeader(authorization);
socialringService.modifySocialring(memberId,socialringName,patchSocialringModifyRequest);
return new BaseResponse<>(BaseExceptionResponseStatus.SUCCESS, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.likelion12.domain.base.BaseLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.web.multipart.MultipartFile;

@Getter
@AllArgsConstructor
Expand All @@ -12,10 +13,10 @@ public class PostCrewRequest {
* 크루 등록 request dto
*/
private String crewName;
private String crewImg;
private Long activityRegionId;
private Long facilityId;
private Long exerciseId;
private MultipartFile crewImg;
private String activityRegionName;
private String facilityName;
private String exerciseName;
private int totalRecruits;
private int crewCost;
private String simpleComment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.likelion12.domain.base.BaseLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;

Expand All @@ -14,7 +15,7 @@ public class PatchSocialringModifyRequest {
* 소셜링 수정 request dto
*/
private String socialringName;
private String socialringImg;
private MultipartFile socialringImg;
private String activityRegionName;
private String facilityName;
private String exerciseName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
import com.example.likelion12.domain.base.BaseGender;
import com.example.likelion12.domain.base.BaseLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDate;

@Getter
@Data
@AllArgsConstructor
public class PostSocialringRequest {
/**
* 소셜링 등록 request dto
*/
private String socialringName;
private String socialringImg;
private MultipartFile socialringImg;
private long activityRegionId;
private long facilityId;
private long exerciseId;
Expand Down
21 changes: 12 additions & 9 deletions src/main/java/com/example/likelion12/service/CrewService.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.example.likelion12.service;

import com.example.likelion12.common.exception.*;
import com.example.likelion12.common.response.BaseResponse;
import com.example.likelion12.domain.*;
import com.example.likelion12.domain.base.BaseGender;
import com.example.likelion12.domain.base.BaseLevel;
import com.example.likelion12.domain.base.BaseRole;
import com.example.likelion12.domain.base.BaseStatus;
import com.example.likelion12.dto.crew.*;
import com.example.likelion12.repository.*;
import com.example.likelion12.util.S3Uploader;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
Expand All @@ -18,6 +18,7 @@
import org.springframework.transaction.annotation.Transactional;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand All @@ -38,38 +39,39 @@ public class CrewService {
private final MemberRepository memberRepository;
private final MemberCrewService memberCrewService;
private final MemberCrewRepository memberCrewRepository;
private final S3Uploader s3Uploader;

/**
* 크루 등록
*/
@Transactional
public PostCrewResponse createCrew(Long memberId, PostCrewRequest postCrewRequest){
public PostCrewResponse createCrew(Long memberId, PostCrewRequest postCrewRequest) throws IOException {
log.info("[CrewService.createCrew]");

Member member = memberRepository.findByMemberIdAndStatus(memberId, BaseStatus.ACTIVE)
.orElseThrow(()-> new MemberException(CANNOT_FOUND_MEMBER));

String crewName = postCrewRequest.getCrewName();
String crewImg = postCrewRequest.getCrewImg();
Long activityRegionId = postCrewRequest.getActivityRegionId();
Long facilityId = postCrewRequest.getFacilityId();
Long exerciseId = postCrewRequest.getExerciseId();
String activityRegionName = postCrewRequest.getActivityRegionName();
String facilityName = postCrewRequest.getFacilityName();
String exerciseName = postCrewRequest.getExerciseName();
int totalRecruits = postCrewRequest.getTotalRecruits();
int crewCost = postCrewRequest.getCrewCost();
String simpleComment = postCrewRequest.getSimpleComment();
String comment = postCrewRequest.getComment();
BaseGender gender = postCrewRequest.getGender();
BaseLevel level = postCrewRequest.getLevel();

ActivityRegion activityRegion = activityRegionRepository.findByActivityRegionIdAndStatus(activityRegionId, BaseStatus.ACTIVE)
ActivityRegion activityRegion = activityRegionRepository.findByActivityRegionNameAndStatus(activityRegionName, BaseStatus.ACTIVE)
.orElseThrow(()-> new ActivityRegionException(CANNOT_FOUND_ACTIVITYREGION));

Facility facility = facilityRepository.findByFacilityIdAndStatus(facilityId, BaseStatus.ACTIVE)
Facility facility = facilityRepository.findByFacilityNameAndStatus(facilityName, BaseStatus.ACTIVE)
.orElseThrow(()-> new FacilityException(CANOOT_FOUND_FACILITY));

Exercise exercise = exerciseRepository.findByExerciseIdAndStatus(exerciseId,BaseStatus.ACTIVE)
Exercise exercise = exerciseRepository.findByExerciseNameAndStatus(exerciseName,BaseStatus.ACTIVE)
.orElseThrow(()-> new ExerciseException(CANNOT_FOUND_EXERCISE));

String crewImg = s3Uploader.uploadFileToS3(postCrewRequest.getCrewImg(),"crew/");
Crew crew = new Crew(crewName, crewImg, totalRecruits, crewCost,simpleComment,comment
,gender,level,activityRegion,facility, exercise, BaseStatus.ACTIVE);
crewRepository.save(crew);
Expand All @@ -78,6 +80,7 @@ public PostCrewResponse createCrew(Long memberId, PostCrewRequest postCrewReques
return new PostCrewResponse(crew.getCrewId());
}


/**
* 크루 조회
*/
Expand Down
27 changes: 16 additions & 11 deletions src/main/java/com/example/likelion12/service/SocialringService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
import com.example.likelion12.dto.crew.GetCrewInquiryResponse;
import com.example.likelion12.dto.socialring.*;
import com.example.likelion12.repository.*;
import com.example.likelion12.util.S3Uploader;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -38,18 +41,19 @@ public class SocialringService {
private final MemberSocialringService memberSocialringService;
private final ActivityRegionRepository activityRegionRepository;
private final MemberSocialringRepository memberSocialringRepository;
private final S3Uploader s3Uploader;

/**
* 소셜링 등록
*/
@Transactional
public PostSocialringResponse createSocialring(Long memberId, PostSocialringRequest postSocialringRequest) {
public PostSocialringResponse createSocialring(Long memberId, PostSocialringRequest postSocialringRequest) throws IOException {
log.info("[SocialringService.createSocialring]");
Member member = memberRepository.findByMemberIdAndStatus(memberId, BaseStatus.ACTIVE)
.orElseThrow(() -> new MemberException(CANNOT_FOUND_MEMBER));

String socialringName = postSocialringRequest.getSocialringName();
String socialringImg = postSocialringRequest.getSocialringImg();
MultipartFile socialringImg = postSocialringRequest.getSocialringImg();
long activityRegionId = postSocialringRequest.getActivityRegionId();
long facilityId = postSocialringRequest.getFacilityId();
long exerciseId = postSocialringRequest.getExerciseId();
Expand All @@ -68,7 +72,8 @@ public PostSocialringResponse createSocialring(Long memberId, PostSocialringRequ
Exercise exercise = exerciseRepository.findByExerciseIdAndStatus(exerciseId, BaseStatus.ACTIVE)
.orElseThrow(() -> new ExerciseException(CANNOT_FOUND_EXERCISE));

Socialring socialring = new Socialring(socialringName, socialringImg, totalRecruits, socialringDate, socialringCost, comment, commentSimple,
String socialringImgUrl = s3Uploader.uploadFileToS3(socialringImg,"socialring/");
Socialring socialring = new Socialring(socialringName, socialringImgUrl, totalRecruits, socialringDate, socialringCost, comment, commentSimple,
gender, level, activityRegion, facility, exercise, BaseStatus.ACTIVE);
socialringRepository.save(socialring);

Expand All @@ -81,7 +86,8 @@ public PostSocialringResponse createSocialring(Long memberId, PostSocialringRequ
* 소셜링 수정
*/
@Transactional
public void modifySocialring(Long memberId, String socialringName, PatchSocialringModifyRequest patchSocialringModifyRequest) {
public void modifySocialring(Long memberId, Long socialringId, PatchSocialringModifyRequest patchSocialringModifyRequest) throws IOException {

log.info("[SocialringService.modifySocialring]");

//소셜링을 수정하고자 하는 멤버
Expand All @@ -101,8 +107,6 @@ public void modifySocialring(Long memberId, String socialringName, PatchSocialri

String newSocialringName = patchSocialringModifyRequest.getSocialringName()
== null ? socialring.getSocialringName() : patchSocialringModifyRequest.getSocialringName();
String newSocialringImg = patchSocialringModifyRequest.getSocialringImg()
== null ? socialring.getSocialringImg() : patchSocialringModifyRequest.getSocialringImg();
Integer newTotalRecruits = patchSocialringModifyRequest.getTotalRecruits()
== null ? socialring.getTotalRecruits() : patchSocialringModifyRequest.getTotalRecruits();
LocalDate newSocialringDate = patchSocialringModifyRequest.getSocialringDate()
Expand All @@ -121,10 +125,10 @@ public void modifySocialring(Long memberId, String socialringName, PatchSocialri
long newActivityRegionId =
patchSocialringModifyRequest.getActivityRegionName() == null ?
socialring.getActivityRegion().getActivityRegionId() :
//리퀘스트로 들어온값이 존재하는 값인지 네임으로 찾아서 아이디값 반환 후 아이디값 수정
activityRegionRepository.findByActivityRegionNameAndStatus(
patchSocialringModifyRequest.getActivityRegionName(), BaseStatus.ACTIVE)
.orElseThrow(() -> new ActivityRegionException(CANNOT_FOUND_ACTIVITYREGION)).getActivityRegionId();
//리퀘스트로 들어온값이 존재하는 값인지 네임으로 찾아서 아이디값 반환 후 아이디값 수정
activityRegionRepository.findByActivityRegionNameAndStatus(
patchSocialringModifyRequest.getActivityRegionName(), BaseStatus.ACTIVE)
.orElseThrow(() -> new ActivityRegionException(CANNOT_FOUND_ACTIVITYREGION)).getActivityRegionId();
long newFacilityId = patchSocialringModifyRequest.getFacilityName()
== null ? socialring.getFacility().getFacilityId() :
facilityRepository.findByFacilityNameAndStatus(
Expand All @@ -143,7 +147,8 @@ public void modifySocialring(Long memberId, String socialringName, PatchSocialri
Exercise exercise = exerciseRepository.findByExerciseIdAndStatus(newExerciseId, BaseStatus.ACTIVE)
.orElseThrow(() -> new ExerciseException(CANNOT_FOUND_EXERCISE));


String newSocialringImg = patchSocialringModifyRequest.getSocialringImg()
== null ? socialring.getSocialringImg() : s3Uploader.uploadFileToS3(patchSocialringModifyRequest.getSocialringImg(),"socialring/");
//변경사항 업데이트 및 저장
socialring.UpdateSocialringInfo(newSocialringName, newSocialringImg, newTotalRecruits, newSocialringDate, newSocialringCost,
newComment, newCommentSimple, newGender, newLevel, activityRegion, facility, exercise);
Expand Down
Loading
Loading