22
33import com .runtracker .domain .upload .exception .*;
44import com .runtracker .global .util .ImageConverter ;
5- import com .runtracker .global .config .FileUploadConfig ;
5+ import com .runtracker .global .config .S3Config ;
66import lombok .RequiredArgsConstructor ;
77import lombok .extern .slf4j .Slf4j ;
8- import org .springframework .core .io .Resource ;
9- import org .springframework .core .io .UrlResource ;
108import org .springframework .stereotype .Service ;
119import org .springframework .util .StringUtils ;
1210import org .springframework .web .multipart .MultipartFile ;
11+ import software .amazon .awssdk .core .sync .RequestBody ;
12+ import software .amazon .awssdk .services .s3 .S3Client ;
13+ import software .amazon .awssdk .services .s3 .model .PutObjectRequest ;
1314
1415import java .io .ByteArrayInputStream ;
1516import java .io .IOException ;
1617import java .io .InputStream ;
17- import java .net .MalformedURLException ;
18- import java .nio .file .Files ;
19- import java .nio .file .Path ;
20- import java .nio .file .Paths ;
21- import java .nio .file .StandardCopyOption ;
2218import java .util .Base64 ;
2319import java .util .UUID ;
2420
2723@ RequiredArgsConstructor
2824public class FileStorageService {
2925
30- private final FileUploadConfig fileUploadConfig ;
26+ private final S3Client s3Client ;
27+ private final S3Config s3Config ;
3128
3229 // 이미지 파일을 업로드하고 URL을 반환
3330 public String uploadImage (MultipartFile file ) {
@@ -43,7 +40,7 @@ public String uploadImage(MultipartFile file) {
4340 return storeFile (file );
4441 }
4542
46- // 파일을 WebP 포맷으로 변환
43+ // 파일을 WebP 포맷으로 변환하여 S3에 업로드
4744 private String storeFile (MultipartFile file ) {
4845 String originalFilename = StringUtils .cleanPath (file .getOriginalFilename ());
4946
@@ -54,21 +51,36 @@ private String storeFile(MultipartFile file) {
5451 try {
5552 String storedFilename = UUID .randomUUID () + ".webp" ;
5653
57- Path targetLocation = Paths .get (fileUploadConfig .getUploadDir ()).resolve (storedFilename );
58-
5954 InputStream webpInputStream = ImageConverter .convertToWebP (file );
55+ byte [] fileBytes = webpInputStream .readAllBytes ();
6056
61- Files . copy ( webpInputStream , targetLocation , StandardCopyOption . REPLACE_EXISTING );
57+ uploadToS3 ( storedFilename , fileBytes );
6258
63- return fileUploadConfig .getBaseUrl () + "/api/upload/image /" + storedFilename ;
59+ return s3Config .getBaseUrl () + "/" + storedFilename ;
6460
6561 } catch (IOException ex ) {
6662 log .error ("Failed to store file: {}" , originalFilename , ex );
6763 throw new FileStorageFailedException (originalFilename );
6864 }
6965 }
7066
71- // Base64 인코딩된 이미지를 파일로 저장하고 URL을 반환
67+ private void uploadToS3 (String filename , byte [] fileBytes ) {
68+ try {
69+ PutObjectRequest putObjectRequest = PutObjectRequest .builder ()
70+ .bucket (s3Config .getBucketName ())
71+ .key (filename )
72+ .contentType ("image/webp" )
73+ .build ();
74+
75+ s3Client .putObject (putObjectRequest , RequestBody .fromBytes (fileBytes ));
76+ log .info ("File uploaded to S3: {}" , filename );
77+ } catch (Exception ex ) {
78+ log .error ("Failed to upload file to S3: {}" , filename , ex );
79+ throw new FileStorageFailedException (filename );
80+ }
81+ }
82+
83+ // Base64 인코딩된 이미지를 S3에 업로드하고 URL을 반환
7284 public String uploadBase64Image (String base64Data ) {
7385 if (base64Data == null || base64Data .trim ().isEmpty ()) {
7486 return null ;
@@ -83,34 +95,22 @@ public String uploadBase64Image(String base64Data) {
8395 byte [] imageBytes = Base64 .getDecoder ().decode (base64Image );
8496
8597 String storedFilename = UUID .randomUUID () + ".webp" ;
86- Path targetLocation = Paths .get (fileUploadConfig .getUploadDir ()).resolve (storedFilename );
8798
8899 InputStream imageInputStream = new ByteArrayInputStream (imageBytes );
89100 InputStream webpInputStream = ImageConverter .convertToWebP (imageInputStream );
101+ byte [] webpBytes = webpInputStream .readAllBytes ();
90102
91- Files . copy ( webpInputStream , targetLocation , StandardCopyOption . REPLACE_EXISTING );
103+ uploadToS3 ( storedFilename , webpBytes );
92104
93- return fileUploadConfig .getBaseUrl () + "/api/upload/image /" + storedFilename ;
105+ return s3Config .getBaseUrl () + "/" + storedFilename ;
94106
95107 } catch (Exception ex ) {
96108 log .error ("Failed to store base64 image" , ex );
97109 throw new FileStorageFailedException ("base64-image" );
98110 }
99111 }
100112
101- public Resource loadFileAsResource (String filename ) {
102- try {
103- Path filePath = Paths .get (fileUploadConfig .getUploadDir ()).resolve (filename ).normalize ();
104- Resource resource = new UrlResource (filePath .toUri ());
105-
106- if (resource .exists ()) {
107- return resource ;
108- } else {
109- throw new FileNotFoundException (filename );
110- }
111- } catch (MalformedURLException ex ) {
112- log .error ("Failed to load file: {}" , filename , ex );
113- throw new FileNotFoundException (filename );
114- }
113+ public String getImageUrl (String filename ) {
114+ return s3Config .getBaseUrl () + "/" + filename ;
115115 }
116116}
0 commit comments