-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ FEAT. AmazonConfig 및 S3 Manager 기능 추가
- Loading branch information
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/fairytale/tbd/global/aws/s3/AmazonS3Manager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
56
src/main/java/fairytale/tbd/global/config/AmazonConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |