-
Notifications
You must be signed in to change notification settings - Fork 0
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
6085410
commit 67b53d1
Showing
3 changed files
with
93 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
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.server.capple.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(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/server/capple/global/utils/S3ImageComponent.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,59 @@ | ||
package com.server.capple.global.utils; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.DeleteObjectRequest; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class S3ImageComponent { | ||
private final AmazonS3 amazonS3; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
/** | ||
* 이미지 업로드 | ||
* @param multipartFile 업로드 할 이미지 파일 | ||
* @return 업로드된 파일의 접근 URL | ||
*/ | ||
public String uploadImage(MultipartFile multipartFile) throws IOException { | ||
String fileName = createFileName(multipartFile.getOriginalFilename()); | ||
|
||
ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentLength(multipartFile.getSize()); | ||
metadata.setContentType(multipartFile.getContentType()); | ||
|
||
amazonS3.putObject(bucket, fileName, multipartFile.getInputStream(), metadata); | ||
return amazonS3.getUrl(bucket, fileName).toString(); | ||
} | ||
|
||
/** | ||
* 이미지 파일명 생성 | ||
* @param originalFileName 파일의 이름 | ||
* @return 작명된 파일 이름 | ||
*/ | ||
public String createFileName(String originalFileName) { | ||
return getOriginalFileName(originalFileName) + UUID.randomUUID().toString().replaceAll("-","") + getFileExtension(originalFileName); | ||
} | ||
|
||
private String getFileExtension(String fileName) { | ||
return fileName.substring(fileName.lastIndexOf(".")); | ||
} | ||
private String getOriginalFileName(String fileName) { return fileName.substring(0, fileName.lastIndexOf(".")); } | ||
|
||
/** | ||
* 버킷에서 이미지 삭제 | ||
* @param fileUrl | ||
*/ | ||
public void deleteImage(String fileUrl) { | ||
amazonS3.deleteObject(new DeleteObjectRequest(bucket, fileUrl.split("/", 4)[3])); | ||
} | ||
} |