Skip to content

Commit

Permalink
[CHORE] s3 설정 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
hcg0127 committed Jul 4, 2024
1 parent 4a69445 commit c92e851
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 62 deletions.
47 changes: 23 additions & 24 deletions src/main/java/umc_haekathon_4/demo/aws/s3/AmazonS3Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
}


}
56 changes: 31 additions & 25 deletions src/main/java/umc_haekathon_4/demo/config/AmazonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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); }

}
23 changes: 23 additions & 0 deletions src/main/java/umc_haekathon_4/demo/domain/Uuid.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<Uuid, Long> {
}
26 changes: 13 additions & 13 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
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}

0 comments on commit c92e851

Please sign in to comment.