1
1
package com .sponus .coreinfras3 ;
2
2
3
3
import java .io .File ;
4
+ import java .io .FileInputStream ;
4
5
import java .io .IOException ;
6
+ import java .net .URI ;
7
+ import java .net .URISyntaxException ;
8
+ import java .nio .ByteBuffer ;
9
+ import java .nio .channels .FileChannel ;
5
10
import java .nio .file .Files ;
11
+ import java .util .Calendar ;
12
+ import java .util .Date ;
6
13
import java .util .List ;
7
14
import java .util .UUID ;
8
15
16
+ import org .springframework .http .MediaType ;
9
17
import org .springframework .stereotype .Service ;
10
18
import org .springframework .web .multipart .MultipartFile ;
19
+ import org .springframework .web .reactive .function .client .WebClient ;
11
20
21
+ import com .amazonaws .HttpMethod ;
12
22
import com .amazonaws .services .s3 .AmazonS3 ;
13
- import com .amazonaws .services .s3 .model .PutObjectRequest ;
14
23
import com .sponus .coreinfras3 .convert .webp .WebpConvertService ;
15
24
import com .sponus .coreinfras3 .util .FileUtils ;
16
25
@@ -43,19 +52,6 @@ public String uploadFile(MultipartFile multipartFile) {
43
52
return uploadFile (file );
44
53
}
45
54
46
- public String uploadFile (File file ) {
47
- String fileName = getFileNamePrefix () + file .getName ();
48
-
49
- amazonS3 .putObject (new PutObjectRequest (s3Config .getBucket (), s3Config .getFolder () + fileName , file ));
50
- try {
51
- Files .delete (file .toPath ());
52
- } catch (IOException e ) {
53
- throw new IllegalArgumentException ("Failed to delete file" , e );
54
- }
55
-
56
- return amazonS3 .getUrl (s3Config .getBucket (), s3Config .getFolder () + fileName ).toString ();
57
- }
58
-
59
55
public String deleteFile (String image ) {
60
56
try {
61
57
amazonS3 .deleteObject (s3Config .getBucket (), s3Config .getFolder () + image );
@@ -74,4 +70,59 @@ public List<String> uploadFiles(List<MultipartFile> files) {
74
70
private String getFileNamePrefix () {
75
71
return UUID .randomUUID ().toString ().substring (0 , 5 ) + "-" ;
76
72
}
73
+
74
+ public String generatePreSignUrl (String fileName ) {
75
+ Calendar calendar = Calendar .getInstance ();
76
+ calendar .setTime (new Date ());
77
+ calendar .add (Calendar .MINUTE , 10 ); //validfy of 10 minutes
78
+ return amazonS3 .generatePresignedUrl (s3Config .getBucket (), fileName , calendar .getTime (), HttpMethod .PUT )
79
+ .toString ();
80
+ }
81
+
82
+ public String uploadFile (File file ) {
83
+ String fileName = s3Config .getFolder () + getFileNamePrefix () + file .getName ();
84
+ String preSignedUrl = generatePreSignUrl (fileName );
85
+
86
+ // log.info("preSignedUrl : {}", preSignedUrl);
87
+ try {
88
+ uploadFileToS3UsingPreSignedUrl (preSignedUrl , file );
89
+ } catch (IOException e ) {
90
+ throw new IllegalArgumentException ("Failed to upload file" , e );
91
+ } catch (URISyntaxException e ) {
92
+ throw new IllegalArgumentException ("Failed to parsing uri" );
93
+ }
94
+
95
+ // 로컬 파일 삭제
96
+ try {
97
+ Files .delete (file .toPath ());
98
+ } catch (IOException e ) {
99
+ throw new IllegalArgumentException ("Failed to delete file" , e );
100
+ }
101
+
102
+ return amazonS3 .getUrl (s3Config .getBucket (), fileName ).toString ();
103
+ }
104
+
105
+ private void uploadFileToS3UsingPreSignedUrl (String preSignedUrl , File file ) throws URISyntaxException ,
106
+ IOException {
107
+
108
+ URI uri = new URI (preSignedUrl );
109
+ WebClient webClient = WebClient .create ();
110
+
111
+ byte [] fileBytes = readFileToBytes (file );
112
+
113
+ webClient .put ()
114
+ .uri (uri )
115
+ .contentType (MediaType .APPLICATION_OCTET_STREAM )
116
+ .bodyValue (fileBytes )
117
+ .retrieve ().toBodilessEntity ().block ();
118
+ }
119
+
120
+ private byte [] readFileToBytes (File file ) throws IOException {
121
+ try (FileInputStream fileInputStream = new FileInputStream (file );
122
+ FileChannel fileChannel = fileInputStream .getChannel ()) {
123
+ ByteBuffer byteBuffer = ByteBuffer .allocate ((int )fileChannel .size ());
124
+ fileChannel .read (byteBuffer );
125
+ return byteBuffer .array ();
126
+ }
127
+ }
77
128
}
0 commit comments