1
1
package cmf .commitField .global .aws .s3 ;
2
2
3
3
import lombok .RequiredArgsConstructor ;
4
+ import org .slf4j .Logger ;
5
+ import org .slf4j .LoggerFactory ;
4
6
import org .springframework .stereotype .Service ;
5
7
import org .springframework .web .multipart .MultipartFile ;
6
8
import software .amazon .awssdk .core .sync .RequestBody ;
11
13
import java .io .IOException ;
12
14
import java .util .UUID ;
13
15
16
+
14
17
@ Service
15
18
@ RequiredArgsConstructor
16
19
public class S3Service {
17
20
private final S3Client s3Client ;
18
21
private static final String BUCKET_NAME = "cmf-bucket-dev-seoyeon-1" ;
19
22
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 ());
20
25
21
26
// ํ์ผ ์
๋ก๋ ๊ธฐ๋ฅ
22
27
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 );
30
32
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 ("ํ์ผ ์
๋ก๋ ์ฑ๊ณต" );
33
46
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
+ }
35
61
}
36
62
63
+
64
+
37
65
// ํ์ผ ์ญ์ ๊ธฐ๋ฅ
38
66
public void deleteFile (String fileName ) {
39
67
DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest .builder ()
@@ -42,5 +70,6 @@ public void deleteFile(String fileName) {
42
70
.build ();
43
71
44
72
s3Client .deleteObject (deleteObjectRequest );
73
+ logger .info ("ํ์ผ ์ญ์ ์ฑ๊ณต: {}" , fileName );
45
74
}
46
75
}
0 commit comments