-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
10 changed files
with
202 additions
and
14 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
11 changes: 11 additions & 0 deletions
11
src/main/java/cotato/growingpain/common/exception/ImageException.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,11 @@ | ||
package cotato.growingpain.common.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
import java.io.IOException; | ||
@AllArgsConstructor | ||
@Getter | ||
public class ImageException extends IOException { | ||
ErrorCode errorCode; | ||
} |
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
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,35 @@ | ||
package cotato.growingpain.s3; | ||
|
||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder; | ||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import lombok.NoArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Slf4j | ||
@Configuration | ||
@NoArgsConstructor | ||
public class S3Config { | ||
|
||
@Value("${cloud.aws.credentials.accessKey}") | ||
private String accessKey; | ||
|
||
@Value("${cloud.aws.credentials.secretKey}") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package cotato.growingpain.s3; | ||
|
||
import com.amazonaws.services.s3.AmazonS3Client; | ||
import com.amazonaws.services.s3.model.CannedAccessControlList; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import cotato.growingpain.common.exception.ErrorCode; | ||
import cotato.growingpain.common.exception.ImageException; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor // final 멤버변수가 있으면 생성자 항목에 포함시킴 | ||
@Component | ||
@Service | ||
public class S3Uploader { | ||
|
||
private final AmazonS3Client amazonS3Client; | ||
|
||
@Value("${cloud.aws.s3.bucket}") | ||
private String bucket; | ||
|
||
// MultipartFile을 전달받아 File로 전환한 후 S3에 업로드 | ||
public String uploadFileToS3(MultipartFile multipartFile, String dirName) throws IOException { | ||
File uploadFile = convert(multipartFile) | ||
.orElseThrow(() -> new ImageException(ErrorCode.IMAGE_PROCESSING_FAIL)); | ||
return upload(uploadFile, dirName); | ||
} | ||
|
||
private String upload(File uploadFile, String dirName) { | ||
String fileName = dirName + "/" + uploadFile.getName(); | ||
String uploadImageUrl = putS3(uploadFile, fileName); | ||
|
||
removeNewFile(uploadFile); // 로컬에 생성된 File 삭제 (MultipartFile -> File 전환 하며 로컬에 파일 생성됨) | ||
|
||
return uploadImageUrl; // 업로드된 파일의 S3 URL 주소 반환 | ||
} | ||
|
||
//S3로 업로드 | ||
private String putS3(File uploadFile, String fileName) { | ||
amazonS3Client.putObject( | ||
new PutObjectRequest(bucket, fileName, uploadFile) | ||
.withCannedAcl(CannedAccessControlList.PublicRead) // PublicRead 권한으로 업로드 됨 | ||
); | ||
return amazonS3Client.getUrl(bucket, fileName).toString(); | ||
} | ||
|
||
//로컬에 저장된 이미지 지우기 | ||
private void removeNewFile(File targetFile) { | ||
if(targetFile.delete()) { | ||
log.info("파일이 삭제되었습니다."); | ||
}else { | ||
log.info("파일이 삭제되지 못했습니다."); | ||
} | ||
} | ||
|
||
private Optional<File> convert(MultipartFile file) throws IOException { | ||
File convertFile = new File(System.getProperty("user.dir") + "/" + UUID.randomUUID() + file.getOriginalFilename()); | ||
try { | ||
if (convertFile.createNewFile()) { // 바로 위에서 지정한 경로에 File이 생성됨 (경로가 잘못되었다면 생성 불가능) | ||
FileOutputStream fos = new FileOutputStream(convertFile); // FileOutputStream 데이터를 파일에 바이트 스트림으로 저장하기 위함 | ||
fos.write(file.getBytes()); | ||
fos.close(); | ||
return Optional.of(convertFile); | ||
} | ||
} catch (IOException e) { | ||
throw new ImageException(ErrorCode.IMAGE_PROCESSING_FAIL); | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
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