diff --git a/src/main/java/umc_haekathon_4/demo/aws/s3/AmazonS3Manager.java b/src/main/java/umc_haekathon_4/demo/aws/s3/AmazonS3Manager.java index 25c36dc..8326475 100644 --- a/src/main/java/umc_haekathon_4/demo/aws/s3/AmazonS3Manager.java +++ b/src/main/java/umc_haekathon_4/demo/aws/s3/AmazonS3Manager.java @@ -9,6 +9,8 @@ import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import umc_haekathon_4.demo.config.AmazonConfig; +import umc_haekathon_4.demo.domain.Uuid; +import umc_haekathon_4.demo.repository.UuidRepository; import java.io.IOException; @@ -17,30 +19,27 @@ @RequiredArgsConstructor public class AmazonS3Manager{ -// private final AmazonS3 amazonS3; -// -// private final AmazonConfig amazonConfig; -// -// @Value(("${cloud.aws.S3.bucket}")) -// private String bucket; -// -// private final UuidRepository uuidRepository; -// -// public String uploadFile(String keyName, MultipartFile file) { -// ObjectMetadata metadata = new ObjectMetadata(); -// metadata.setContentLength(file.getSize()); -// try { -// amazonS3.putObject(new PutObjectRequest(amazonConfig.getBucket(), keyName, file.getInputStream(), metadata)); -// } -// catch (IOException e) { -// log.error("error at AmazonS3Manager uploadFile : {}", (Object) e.getStackTrace()); -// } -// return amazonS3.getUrl(amazonConfig.getBucket(), keyName).toString(); -// } -// -// public String generateReviewKeyName(Uuid uuid) { -// return amazonConfig.getReviewPath() + '/' + uuid.getUuid(); -// } + private final AmazonS3 amazonS3; + + private final AmazonConfig amazonConfig; + + private final UuidRepository uuidRepository; + + public String uploadFile(String keyName, MultipartFile file) { + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentLength(file.getSize()); + try { + amazonS3.putObject(new PutObjectRequest(amazonConfig.getBucket(), keyName, file.getInputStream(), metadata)); + } + catch (IOException e) { + log.error("error at AmazonS3Manager uploadFile : {}", (Object) e.getStackTrace()); + } + return amazonS3.getUrl(amazonConfig.getBucket(), keyName).toString(); + } + + public String generateReviewKeyName(Uuid uuid) { + return amazonConfig.getImagePath() + '/' + uuid.getUuid(); + } } \ No newline at end of file diff --git a/src/main/java/umc_haekathon_4/demo/config/AmazonConfig.java b/src/main/java/umc_haekathon_4/demo/config/AmazonConfig.java index f1c0b93..14611c1 100644 --- a/src/main/java/umc_haekathon_4/demo/config/AmazonConfig.java +++ b/src/main/java/umc_haekathon_4/demo/config/AmazonConfig.java @@ -16,30 +16,36 @@ @Getter public class AmazonConfig { -// 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 = "ap-northeast-2"; -// -// @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(); -// } -// -// @Bean -// public AWSCredentialsProvider awsCredentialsProvider() { return new AWSStaticCredentialsProvider(awsCredentials); } + private AWSCredentials awsCredentials; + + @Value("${cloud.aws.credentials.access-key}") + private String accessKey; + + @Value("${cloud.aws.credentials.secret-key}") + private String secretKey; + + @Value("${cloud.aws.region.static}") + private String region = "ap-northeast-2"; + + @Value("${cloud.aws.s3.bucket}") + private String bucket; + + @Value("${cloud.aws.s3.path.image}") + private String imagePath; + + @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(); + } + + @Bean + public AWSCredentialsProvider awsCredentialsProvider() { return new AWSStaticCredentialsProvider(awsCredentials); } } diff --git a/src/main/java/umc_haekathon_4/demo/domain/Uuid.java b/src/main/java/umc_haekathon_4/demo/domain/Uuid.java new file mode 100644 index 0000000..b4eb443 --- /dev/null +++ b/src/main/java/umc_haekathon_4/demo/domain/Uuid.java @@ -0,0 +1,23 @@ +package umc_haekathon_4.demo.domain; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import umc_haekathon_4.demo.domain.common.BaseEntity; + +@Entity +@Builder +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class Uuid extends BaseEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(unique = true) + private String uuid; +} diff --git a/src/main/java/umc_haekathon_4/demo/repository/UuidRepository.java b/src/main/java/umc_haekathon_4/demo/repository/UuidRepository.java new file mode 100644 index 0000000..dcddb41 --- /dev/null +++ b/src/main/java/umc_haekathon_4/demo/repository/UuidRepository.java @@ -0,0 +1,7 @@ +package umc_haekathon_4.demo.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import umc_haekathon_4.demo.domain.Uuid; + +public interface UuidRepository extends JpaRepository { +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 6902269..65fa01b 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -33,16 +33,16 @@ springdoc: paths-to-match: - /** use-fqn: true -#cloud: -# aws: -# s3: -# bucket: umc-6th -# path: -# review: review -# region: -# static: ap-northeast-2 -# stack: -# auto: false -# credentials: -# access-key: ${AWS_ACCESS_KEY_ID} -# secret-key: ${AWS_SECRET_ACCESS_KEY} \ No newline at end of file +cloud: + aws: + s3: + bucket: umc-team4-bucket + path: + image: image + region: + static: ap-northeast-2 + stack: + auto: false + credentials: + access-key: ${AWS_ACCESS_KEY_ID} + secret-key: ${AWS_SECRET_ACCESS_KEY} \ No newline at end of file