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

✨ S3이미지 업로드, 마이디자인 기능 구현 #91

Merged
merged 5 commits into from
Feb 3, 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
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ dependencies {
// model mapper
implementation 'org.modelmapper:modelmapper:3.1.0'

//s3
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
}

tasks.named('test') {
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/aromanticcat/umcproject/S3/S3Service.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package aromanticcat.umcproject.S3;

import aromanticcat.umcproject.S3.config.AmazonConfig;
import aromanticcat.umcproject.repository.UuidRepository;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Slf4j
@Component
@RequiredArgsConstructor
@Service
public class S3Service {
private final AmazonS3Client amazonS3;

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

private String dir1 = "/letter-paper";
private String dir2 = "/stamp";
private String defaultUrl = "https://airplanning-bucket.s3.ap-northeast-2.amazonaws.com";

public String uploadFile1(MultipartFile file) throws IOException {

String bucketDir = bucketName + dir1;
String dirUrl = defaultUrl + dir1 + "/";
String fileName = generateFileName(file);

amazonS3.putObject(bucketDir, fileName, file.getInputStream(), getObjectMetadata(file));
return dirUrl + fileName;

}

public String uploadFile2(MultipartFile file) throws IOException {

String bucketDir = bucketName + dir2;
String dirUrl = defaultUrl + dir2 + "/";
String fileName = generateFileName(file);

amazonS3.putObject(bucketDir, fileName, file.getInputStream(), getObjectMetadata(file));
return dirUrl + fileName;
}

private ObjectMetadata getObjectMetadata(MultipartFile file) {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType(file.getContentType());
objectMetadata.setContentLength(file.getSize());
return objectMetadata;
}

private String generateFileName(MultipartFile file) {
return UUID.randomUUID().toString() + "-" + file.getOriginalFilename();
}
}
32 changes: 32 additions & 0 deletions src/main/java/aromanticcat/umcproject/S3/config/AmazonConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package aromanticcat.umcproject.S3.config;

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.AmazonS3Client;
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 AmazonConfig {
@Value("${cloud.aws.credentials.accessKey}")
private String accessKey;
@Value("${cloud.aws.credentials.secretKey}")
private String secretKey;
@Value("${cloud.aws.s3.bucketName}")
private String bucketName;
@Value("${cloud.aws.region.static}")
private String region;

@Bean
public AmazonS3Client amazonS3Client() {
BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey);
return (AmazonS3Client) AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials))
.build();
}
}
39 changes: 39 additions & 0 deletions src/main/java/aromanticcat/umcproject/config/S3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package aromanticcat.umcproject.config;

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;
import javax.annotation.PostConstruct;

@Configuration
@Getter
public class S3Config {
private AWSCredentials awsCredentials;

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

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

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

@PostConstruct
public void init() { this.awsCredentials = new BasicAWSCredentials(accessKey, secretKey);}

@Bean
public AmazonS3 amazonS3() {
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
return AmazonS3ClientBuilder.standard()
.withRegion(region)
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.build();
}
}
36 changes: 36 additions & 0 deletions src/main/java/aromanticcat/umcproject/entity/MyLetterPaper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package aromanticcat.umcproject.entity;

import lombok.*;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Entity
@Getter
@Setter
@RequiredArgsConstructor
@Table(name = "myLetterPaper")
public class MyLetterPaper {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long mPaper_id;

@NotNull
private String imageUrl;

@NotNull
private String name;

@ManyToOne
@JoinColumn(name = "member_id")
private Member member;

@Builder
public MyLetterPaper(Long id, String imageUrl, String name, Member member) {
this.mPaper_id = id;
this.imageUrl = imageUrl;
this.name = name;
this.member = member;
}

}
39 changes: 39 additions & 0 deletions src/main/java/aromanticcat/umcproject/entity/MyStamp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package aromanticcat.umcproject.entity;

import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Entity
@Getter
@Setter
@Builder
@RequiredArgsConstructor
@Table(name = "myStamp")
public class MyStamp {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long mStamp_id;

@NotNull
private String imageUrl;

@NotNull
private String name;

@ManyToOne
@JoinColumn(name = "member_id")
private Member member;

@Builder
public MyStamp(Long id, String imageUrl, String name, Member member) {
this.mStamp_id = id;
this.imageUrl = imageUrl;
this.name = name;
this.member = member;
}
}
18 changes: 18 additions & 0 deletions src/main/java/aromanticcat/umcproject/entity/Uuid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package aromanticcat.umcproject.entity;

import lombok.*;

import javax.persistence.*;

@Entity
@Builder
@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Uuid extends BaseEntity{
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(unique = true)
private String uuid;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package aromanticcat.umcproject.repository;

import aromanticcat.umcproject.entity.Letter;
import aromanticcat.umcproject.entity.Letterbox;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface LetterRepository extends JpaRepository<Letter, Long> {
Optional<Letter> findByLetterId(Long id);

//List<Letter> findById(Long id);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package aromanticcat.umcproject.repository;

import aromanticcat.umcproject.entity.Letterbox;
import aromanticcat.umcproject.entity.Member;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface MemberRepository extends JpaRepository<Member, Long> {
Optional<Member> findById(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package aromanticcat.umcproject.repository;

import aromanticcat.umcproject.entity.Letterbox;
import aromanticcat.umcproject.entity.MyLetterPaper;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MyLetterPaperRepository extends JpaRepository<MyLetterPaper, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package aromanticcat.umcproject.repository;

import aromanticcat.umcproject.entity.Letterbox;
import aromanticcat.umcproject.entity.MyStamp;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MyStampRepository extends JpaRepository<MyStamp, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package aromanticcat.umcproject.repository;

import aromanticcat.umcproject.entity.Stamp;
import aromanticcat.umcproject.entity.Uuid;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UuidRepository extends JpaRepository<Uuid, Long> {
}
54 changes: 54 additions & 0 deletions src/main/java/aromanticcat/umcproject/service/MyDesignService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package aromanticcat.umcproject.service;

import aromanticcat.umcproject.S3.S3Service;
import aromanticcat.umcproject.entity.Member;
import aromanticcat.umcproject.entity.MyLetterPaper;
import aromanticcat.umcproject.entity.MyStamp;
import aromanticcat.umcproject.repository.MemberRepository;
import aromanticcat.umcproject.repository.MyLetterPaperRepository;
import aromanticcat.umcproject.repository.MyStampRepository;
import aromanticcat.umcproject.repository.StampRepository;
import aromanticcat.umcproject.web.dto.MyDesignRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RequiredArgsConstructor
@Service
public class MyDesignService {
@Autowired
private MyLetterPaperRepository myLetterPaperRepository;
@Autowired
private MemberRepository memberRepository;
@Autowired
private MyStampRepository myStampRepository;
@Autowired
private S3Service s3Service;

public Long createMyLetterPaper(MyDesignRequest myDesignRequest, MultipartFile file) throws IOException {
String url = s3Service.uploadFile1(file);
Member member = memberRepository.findById(myDesignRequest.getMember_id()).orElseThrow(() -> new IllegalArgumentException("Member not found. id=" + myDesignRequest.getMember_id()));
MyLetterPaper myLetterPaper = MyLetterPaper.builder()
.name(myDesignRequest.getName())
.imageUrl(url)
.member(member)
.build();
MyLetterPaper saveMyLetterPaper = myLetterPaperRepository.save(myLetterPaper);
return saveMyLetterPaper.getMPaper_id();
}

public Long createMyStamp(MyDesignRequest myDesignRequest, MultipartFile file) throws IOException {
String url = s3Service.uploadFile2(file);
Member member = memberRepository.findById(myDesignRequest.getMember_id()).orElseThrow(() -> new IllegalArgumentException("Member not found. id=" + myDesignRequest.getMember_id()));
MyStamp myStamp = MyStamp.builder()
.name(myDesignRequest.getName())
.imageUrl(url)
.member(member)
.build();
MyStamp saveMyStamp = myStampRepository.save(myStamp);
return saveMyStamp.getMStamp_id();
}
}
13 changes: 0 additions & 13 deletions src/main/java/aromanticcat/umcproject/service/StampService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,7 @@ public class StampService {
// 의존성 주입
private final StampRepository stampRepository;

//@Autowired
//private UploadS3Service uploadS3Service;

public StampService(StampRepository stampRepository) {
this.stampRepository = stampRepository;
}

// @Transactional
// public Long keepDiary(MultipartFile image, Stamp stamp) throws IOException {
// if(!image.isEmpty()) {
// String storedFileName = uploadS3Service.upload(image,"images");
// stamp.setImageUrl(storedFileName);
// }
// Stamp newStamp = stampRepository.save(stamp);
// return newStamp.getId();
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityNotFoundException;
import java.util.List;

@Transactional(readOnly = true)
@RequiredArgsConstructor
Expand Down Expand Up @@ -50,4 +51,5 @@ public LetterResponse getLetterById(Long letterId) {
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/letters")
Expand Down
Loading
Loading