99import software .amazon .awssdk .services .s3 .model .PutObjectRequest ;
1010
1111import java .io .IOException ;
12+ import java .time .LocalDateTime ;
1213import java .util .UUID ;
1314
1415@ Service
1516@ RequiredArgsConstructor
1617public class S3FileStorageService implements FileStorageService {
1718
1819 private final S3Client s3Client ;
20+ private final ImageFileValidator imageFileValidator ;
21+ private final FileDeleteQueueRepository fileDeleteQueueRepository ;
1922
2023 @ Value ("${cloud.aws.s3.bucket}" )
2124 private String bucket ;
@@ -27,7 +30,16 @@ public class S3FileStorageService implements FileStorageService {
2730 @ Override
2831 public String upload (MultipartFile file , String basePath ) {
2932
30- String fileName = UUID .randomUUID () + "_" + file .getOriginalFilename ();
33+ imageFileValidator .validateImageFile (file );
34+
35+ String originalFilename = file .getOriginalFilename ();
36+
37+ // 확장자 추출
38+ String extension = originalFilename .substring (originalFilename .lastIndexOf ("." )).toLowerCase ();
39+
40+ // 파일명 제거 (UUID만 사용)
41+ String fileName = UUID .randomUUID () + extension ;
42+
3143 String key = basePath + "/" + fileName ;
3244
3345 try {
@@ -56,16 +68,11 @@ public void delete(String fileUrl) {
5668 return ;
5769 }
5870
59- String prefix = "https://" + bucket + ".s3." + region + ".amazonaws.com/" ;
60- if (!fileUrl .startsWith (prefix )) {
61- throw new IllegalArgumentException ("잘못된 S3 파일 URL입니다." );
62- }
63-
64- String key = fileUrl .substring (prefix .length ());
65-
66- s3Client .deleteObject (builder -> builder
67- .bucket (bucket )
68- .key (key )
71+ fileDeleteQueueRepository .save (
72+ FileDeleteQueue .builder ()
73+ .fileUrl (fileUrl )
74+ .deleteAt (LocalDateTime .now ().plusMinutes (30 ))
75+ .build ()
6976 );
7077 }
7178}
0 commit comments