-
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.
* feat: spring cloud aws, aws-s3 의존성 추가 * feat: storage client 인터페이스 작성 * feat: aws cloud, s3 설정 추가 * feat: AwsS3Client upload, delete 메서드 구현 * feat: 테스트 aws s3 설정 추가 * chore: 빈 라인 정렬
- Loading branch information
Showing
5 changed files
with
110 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
10 changes: 10 additions & 0 deletions
10
server/src/main/java/com/fluffy/storage/application/StorageClient.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,10 @@ | ||
package com.fluffy.storage.application; | ||
|
||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
public interface StorageClient { | ||
|
||
String upload(MultipartFile file); | ||
|
||
void delete(String fileName); | ||
} |
67 changes: 67 additions & 0 deletions
67
server/src/main/java/com/fluffy/storage/infra/AwsS3Client.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,67 @@ | ||
package com.fluffy.storage.infra; | ||
|
||
import com.fluffy.global.exception.BadRequestException; | ||
import com.fluffy.global.exception.NotFoundException; | ||
import com.fluffy.storage.application.StorageClient; | ||
import io.awspring.cloud.s3.S3Exception; | ||
import io.awspring.cloud.s3.S3Resource; | ||
import io.awspring.cloud.s3.S3Template; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.text.SimpleDateFormat; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class AwsS3Client implements StorageClient { | ||
|
||
private final S3Template s3Template; | ||
|
||
@Value("${spring.cloud.aws.s3.bucket}") | ||
private String bucketName; | ||
|
||
@Override | ||
public String upload(MultipartFile file) { | ||
|
||
if (file.isEmpty()) { | ||
throw new BadRequestException("파일이 비어있습니다."); | ||
} | ||
|
||
String fileName = generateFileName(file.getOriginalFilename()); | ||
|
||
try (InputStream is = file.getInputStream()) { | ||
S3Resource upload = s3Template.upload(bucketName, fileName, is); | ||
|
||
return upload.getURL().toString(); | ||
} catch (IOException | S3Exception e) { | ||
throw new BadRequestException("파일 업로드에 실패했습니다.", e); | ||
} | ||
} | ||
|
||
@Override | ||
public void delete(String fileName) { | ||
try { | ||
s3Template.deleteObject(bucketName, fileName); | ||
} catch (S3Exception e) { | ||
throw new NotFoundException("파일을 찾을 수 없습니다.", e); | ||
} | ||
} | ||
|
||
private String generateFileName(String originalFileName) { | ||
if (originalFileName == null) { | ||
throw new NotFoundException("파일 이름을 찾을 수 없습니다."); | ||
} | ||
|
||
int extensionIndex = originalFileName.lastIndexOf("."); | ||
|
||
String extension = originalFileName.substring(extensionIndex); | ||
String fileName = originalFileName.substring(0, extensionIndex); | ||
|
||
String now = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SSS").format(System.currentTimeMillis()); | ||
|
||
return "%s-%s%s".formatted(fileName, now, extension); | ||
} | ||
} |
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