Skip to content

Commit

Permalink
✨ FEAT. AmazonConfig 및 S3 Manager 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
junhaa committed Apr 8, 2024
1 parent f369364 commit ff1a3b4
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/fairytale/tbd/global/aws/s3/AmazonS3Manager.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
56 changes: 56 additions & 0 deletions src/main/java/fairytale/tbd/global/config/AmazonConfig.java
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit ff1a3b4

Please sign in to comment.