11package cmf .commitField .global .aws .s3 ;
22
33import lombok .RequiredArgsConstructor ;
4+ import org .slf4j .Logger ;
5+ import org .slf4j .LoggerFactory ;
46import org .springframework .stereotype .Service ;
57import org .springframework .web .multipart .MultipartFile ;
68import software .amazon .awssdk .core .sync .RequestBody ;
1113import java .io .IOException ;
1214import java .util .UUID ;
1315
16+
1417@ Service
1518@ RequiredArgsConstructor
1619public class S3Service {
1720 private final S3Client s3Client ;
1821 private static final String BUCKET_NAME = "cmf-bucket-dev-seoyeon-1" ;
1922 private static final String REGION = "ap-northeast-2" ;
23+ private static final long MAX_FILE_SIZE = 5 * 1024 * 1024 ; // 5MB
24+ private final Logger logger = LoggerFactory .getLogger (S3Service .class .getName ());
2025
2126 // ํ์ผ ์
๋ก๋ ๊ธฐ๋ฅ
2227 public String uploadFile (MultipartFile file , String dirName ) throws IOException {
23- String fileName = dirName + "/" + UUID .randomUUID () + "_" + file .getOriginalFilename ();
24- PutObjectRequest putObjectRequest = PutObjectRequest .builder ()
25- .bucket (BUCKET_NAME )
26- .key (fileName )
27- .contentType (file .getContentType ())
28- // .acl(ObjectCannedACL.PUBLIC_READ) // Public Read ๊ถํ ์ถ๊ฐ
29- .build ();
28+ try {
29+
30+ // ํ์ผ ํฌ๊ธฐ ๊ฒ์ฆ
31+ validateFileSize (file );
3032
31- s3Client .putObject (putObjectRequest ,
32- RequestBody .fromInputStream (file .getInputStream (), file .getSize ()));
33+ // UUID๋ก ๊ณ ์ ํ ํ์ผ๋ช
์์ฑ
34+ String fileName = dirName + "/" + UUID .randomUUID () + "_" + file .getOriginalFilename ();
35+ // PutObjectRequest ๊ฐ์ฒด๋ฅผ ์์ฑ
36+ PutObjectRequest putObjectRequest = PutObjectRequest .builder ()
37+ .bucket (BUCKET_NAME )
38+ .key (fileName )
39+ .contentType (file .getContentType ())
40+ // .acl(ObjectCannedACL.PUBLIC_READ) // Public Read ๊ถํ ์ถ๊ฐ
41+ .build ();
42+ // ํ์ผ S3์ ์
๋ก๋
43+ s3Client .putObject (putObjectRequest ,
44+ RequestBody .fromInputStream (file .getInputStream (), file .getSize ()));
45+ logger .info ("ํ์ผ ์
๋ก๋ ์ฑ๊ณต" );
3346
34- return "https://" + BUCKET_NAME + ".s3." + REGION + ".amazonaws.com/" + fileName ;
47+ // S3 ํ์ผ URL ๋ฐํ
48+ return "https://" + BUCKET_NAME + ".s3." + REGION + ".amazonaws.com/" + fileName ;
49+
50+ } catch (IOException e ) {
51+ logger .error ("ํ์ผ ์
๋ก๋ ์คํจ: {}" , e .getMessage ());
52+ throw new IOException ("ํ์ผ ์
๋ก๋ ์ค ์ค๋ฅ ๋ฐ์" , e );
53+ }
54+ }
55+
56+ // ํ์ผ ํฌ๊ธฐ ๊ฒ์ฆ ๋ฉ์๋
57+ private void validateFileSize (MultipartFile file ) throws IOException {
58+ if (file .getSize () > MAX_FILE_SIZE ) {
59+ throw new IOException ("ํ์ผ ํฌ๊ธฐ๊ฐ 5MB๋ฅผ ์ด๊ณผํ์ฌ ์
๋ก๋ํ ์ ์์ต๋๋ค." );
60+ }
3561 }
3662
63+
64+
3765 // ํ์ผ ์ญ์ ๊ธฐ๋ฅ
3866 public void deleteFile (String fileName ) {
3967 DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest .builder ()
@@ -42,5 +70,6 @@ public void deleteFile(String fileName) {
4270 .build ();
4371
4472 s3Client .deleteObject (deleteObjectRequest );
73+ logger .info ("ํ์ผ ์ญ์ ์ฑ๊ณต: {}" , fileName );
4574 }
4675}
0 commit comments