diff --git a/build.gradle b/build.gradle index 0148f54..85787cb 100644 --- a/build.gradle +++ b/build.gradle @@ -32,6 +32,10 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-web' + + // AWS + implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE' + compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.mysql:mysql-connector-j' annotationProcessor 'org.projectlombok:lombok' diff --git a/src/main/java/fairytale/tbd/global/aws/s3/AmazonS3Manager.java b/src/main/java/fairytale/tbd/global/aws/s3/AmazonS3Manager.java new file mode 100644 index 0000000..dc3a7a0 --- /dev/null +++ b/src/main/java/fairytale/tbd/global/aws/s3/AmazonS3Manager.java @@ -0,0 +1,40 @@ +package fairytale.tbd.global.aws.s3; + +import java.io.IOException; + +import org.springframework.stereotype.Component; +import org.springframework.web.multipart.MultipartFile; + +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.model.ObjectMetadata; +import com.amazonaws.services.s3.model.PutObjectRequest; +import com.amazonaws.services.s3.model.PutObjectResult; + +import fairytale.tbd.global.config.AmazonConfig; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@Component +@RequiredArgsConstructor +public class AmazonS3Manager { + + private final AmazonS3 amazonS3; + private final AmazonConfig amazonConfig; + + public String uploadFile(String keyName, MultipartFile file) { + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentType(file.getContentType()); + metadata.setContentLength(file.getSize()); + try { + PutObjectResult putObjectResult = amazonS3.putObject( + new PutObjectRequest(amazonConfig.getBucket(), keyName, file.getInputStream(), metadata)); + log.info("result={}", putObjectResult.getContentMd5()); + } catch (IOException e) { + log.error("error at AmazonS3Manager uploadFile : {}", (Object)e.getStackTrace()); + } + + return amazonS3.getUrl(amazonConfig.getBucket(), keyName).toString(); + } + +} diff --git a/src/main/java/fairytale/tbd/global/config/AmazonConfig.java b/src/main/java/fairytale/tbd/global/config/AmazonConfig.java new file mode 100644 index 0000000..acb5591 --- /dev/null +++ b/src/main/java/fairytale/tbd/global/config/AmazonConfig.java @@ -0,0 +1,56 @@ +package fairytale.tbd.global.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.amazonaws.auth.AWSCredentials; +import com.amazonaws.auth.AWSCredentialsProvider; +import com.amazonaws.auth.AWSStaticCredentialsProvider; +import com.amazonaws.auth.BasicAWSCredentials; +import com.amazonaws.services.s3.AmazonS3; +import com.amazonaws.services.s3.AmazonS3ClientBuilder; + +import jakarta.annotation.PostConstruct; +import lombok.Getter; + +@Configuration +@Getter +public class AmazonConfig { + + private AWSCredentials awsCredentials; + + @Value("${cloud.aws.s3.credentials.access-key}") + private String accessKey; + + @Value("${cloud.aws.s3.credentials.secret-key}") + private String secretKey; + + @Value("${cloud.aws.s3.region.static}") + private String region; + + @Value("${cloud.aws.s3.bucket}") + private String bucket; + + + + @PostConstruct + public void init(){ + this.awsCredentials = new BasicAWSCredentials(accessKey, secretKey); + } + + @Bean + public AmazonS3 amazonS3(){ + BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); + return AmazonS3ClientBuilder.standard() + .withRegion(region) + .withCredentials(new AWSStaticCredentialsProvider(awsCredentials)) + .build(); + } + + @Bean + public AWSCredentialsProvider awsCredentialsProvider(){ + return new AWSStaticCredentialsProvider(awsCredentials); + } + +}