-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
114 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
30 changes: 30 additions & 0 deletions
30
src/main/java/com/ogjg/daitgym/config/aws/s3/S3Config.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,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
80
src/main/java/com/ogjg/daitgym/s3/repository/S3Repository.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,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()); | ||
} | ||
} | ||
} |