-
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.
- Loading branch information
1 parent
d833573
commit c7fc832
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/java/com/project/mapdagu/common/S3/FileService.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,68 @@ | ||
package com.project.mapdagu.common.S3; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.DeleteObjectRequest; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.project.mapdagu.error.exception.custom.BusinessException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.UUID; | ||
|
||
import static com.project.mapdagu.error.ErrorCode.INVALID_FILE_UPLOAD; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class FileService { | ||
|
||
private final AmazonS3Client amazonS3Client; | ||
|
||
@Value("${cloud.aws.s3.bucket.name}") | ||
private String bucketName; | ||
|
||
@Value("${cloud.aws.s3.bucket.url}") | ||
private String defaultUrl; | ||
|
||
public String uploadFile(MultipartFile multipartFile, String imageType) { | ||
if (multipartFile == null || multipartFile.isEmpty()) return null; | ||
|
||
String savedFileName = getSavedFileName(multipartFile, imageType); | ||
ObjectMetadata metadata = new ObjectMetadata(); | ||
|
||
try (InputStream inputStream = multipartFile.getInputStream()) { | ||
amazonS3Client.putObject(bucketName, savedFileName, inputStream, metadata); | ||
} catch (IOException e) { | ||
log.error("Failed to upload image", e); | ||
throw new BusinessException(INVALID_FILE_UPLOAD); | ||
} | ||
return getResourceUrl(savedFileName); | ||
} | ||
|
||
public void deleteFile(String fileUrl) { | ||
String fileName = getFileNameFromResourceUrl(fileUrl); | ||
amazonS3Client.deleteObject(new DeleteObjectRequest(bucketName, fileName)); | ||
} | ||
|
||
private String getSavedFileName(MultipartFile multipartFile, String imageType) { | ||
return String.format("%s/%s-%s", | ||
imageType, getRandomUUID(), multipartFile.getOriginalFilename()); | ||
} | ||
|
||
private String getRandomUUID() { | ||
return UUID.randomUUID().toString().replace("-", ""); | ||
} | ||
|
||
private String getResourceUrl(String savedFileName) { | ||
return amazonS3Client.getResourceUrl(bucketName, savedFileName); | ||
} | ||
|
||
private String getFileNameFromResourceUrl(String fileUrl) { | ||
return fileUrl.replace(defaultUrl + "/", ""); | ||
} | ||
} |
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,33 @@ | ||
package com.project.mapdagu.config; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@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 AmazonS3Client amazonS3Client() { | ||
BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); | ||
|
||
return (AmazonS3Client) AmazonS3ClientBuilder | ||
.standard() | ||
.withRegion(region) | ||
.withCredentials(new AWSStaticCredentialsProvider(credentials)) | ||
.build(); | ||
} | ||
} |
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