Skip to content

Commit

Permalink
Feat : DIG-99 Aws S3 이미지 추가 삭제 설정
Browse files Browse the repository at this point in the history
### S3 버켓에 이미 삭제, 업로드 구현
  • Loading branch information
Dongjin113 committed Nov 13, 2023
1 parent 552cb2b commit a814dc7
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ dependencies {
asciidoctorExt 'org.springframework.restdocs:spring-restdocs-asciidoctor'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'

// 필요한 스프링 클라우드 AWS 스타터 추가
implementation platform('software.amazon.awssdk:bom:2.20.56')
implementation 'software.amazon.awssdk:s3'

}

clean {
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/ogjg/daitgym/config/aws/s3/S3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.ogjg.daitgym.config.aws.s3;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;

@Configuration
public class S3Config {

@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;

@Bean
public S3Client s3Client() {
AwsBasicCredentials awsCredentials = AwsBasicCredentials.create(accessKey, secretKey);

return S3Client.builder()
.region(Region.of(region))
.credentialsProvider(StaticCredentialsProvider.create(awsCredentials))
.build();
}
}
80 changes: 80 additions & 0 deletions src/main/java/com/ogjg/daitgym/s3/repository/S3Repository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.ogjg.daitgym.s3.repository;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import software.amazon.awssdk.services.s3.model.GetUrlRequest;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;

import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

@Slf4j
@Repository
@RequiredArgsConstructor
public class S3Repository {

@Value("${cloud.aws.credentials.bucket-name}")
private String bucketName;
private final S3Client s3Client;


public String uploadImageToS3(MultipartFile file) {
String uploadUrl = null;
try {
String uploadFilename = UUID.randomUUID() + "_" + file.getOriginalFilename();

PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.key(uploadFilename)
.build();

s3Client.putObject(
putObjectRequest,
RequestBody.fromInputStream(
file.getInputStream(), file.getSize()
)
);

uploadUrl = getObjectUrl(uploadFilename);

} catch (Exception e) {
log.error(e.getMessage());
}
return uploadUrl;
}

private String getObjectUrl(String fileName) {
return s3Client.utilities()
.getUrl(GetUrlRequest
.builder()
.bucket(bucketName)
.key(fileName)
.build())
.toExternalForm();
}

public void deleteImageFromS3(String fileUrl) {
try {
URL url = new URL(fileUrl);
String key = url.getPath().substring(1);
String filename = URLDecoder.decode(key, StandardCharsets.UTF_8);

DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder()
.bucket(bucketName)
.key(filename)
.build();

s3Client.deleteObject(deleteObjectRequest);
} catch (Exception e) {
log.error(e.getMessage());
}
}
}

0 comments on commit a814dc7

Please sign in to comment.