From f4fb9404a8e67fd76c11942c17b8d47067baf2cb Mon Sep 17 00:00:00 2001 From: kim_sang_ june <79149384+drbug2000@users.noreply.github.com> Date: Mon, 12 Aug 2024 16:01:16 +0900 Subject: [PATCH] [fix] image convert process fix download -> memory --- .../status/BaseExceptionResponseStatus.java | 2 +- .../space_spring/service/S3Uploader.java | 46 ++++++++++++++----- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java b/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java index 5d9a7da7..db21843e 100644 --- a/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java +++ b/src/main/java/space/space_spring/response/status/BaseExceptionResponseStatus.java @@ -88,7 +88,7 @@ public enum BaseExceptionResponseStatus implements ResponseStatus { */ IS_NOT_IMAGE_FILE(9000, HttpStatus.BAD_REQUEST, "지원되는 이미지 파일의 형식이 아닙니다."), - + MULTIPARTFILE_CONVERT_FAILE_IN_MEMORY(9001,HttpStatus.INTERNAL_SERVER_ERROR,"multipartFile memory 변환 과정에서 문제가 생겼습니다."), /* * 10000: voice room 오류 */ diff --git a/src/main/java/space/space_spring/service/S3Uploader.java b/src/main/java/space/space_spring/service/S3Uploader.java index 1eb4eb17..fe4d6f06 100644 --- a/src/main/java/space/space_spring/service/S3Uploader.java +++ b/src/main/java/space/space_spring/service/S3Uploader.java @@ -3,19 +3,24 @@ import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.CannedAccessControlList; +import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.PutObjectRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import space.space_spring.exception.CustomException; import space.space_spring.validator.AllowedImageFileExtensions; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.util.Optional; +import static space.space_spring.response.status.BaseExceptionResponseStatus.MULTIPARTFILE_CONVERT_FAILE_IN_MEMORY; + @Slf4j @RequiredArgsConstructor @Service @@ -27,20 +32,39 @@ public class S3Uploader { private String bucket; //MultipartFile을 전달 받아 File로 전환 후 S3 업로드 - public String upload(MultipartFile multipartFile, String dirName) throws IOException{// dirName의 디렉토리가 S3 Bucket 내부에 생성됨 +// public String uploadFile(MultipartFile multipartFile, String dirName) throws IOException{// dirName의 디렉토리가 S3 Bucket 내부에 생성됨 +// +// File uploadFile = convert(multipartFile).orElseThrow(()-> new IllegalArgumentException("MultipartFile -> File 전환 실패")); +// //System.out.p +// // print("error: multipart file input. cant control"); +// return upload(uploadFile,dirName); +// } - File uploadFile = convert(multipartFile).orElseThrow(()-> new IllegalArgumentException("MultipartFile -> File 전환 실패")); - //System.out.p - // print("error: multipart file input. cant control"); - return upload(uploadFile,dirName); - } - public String upload(File uploadFile, String dirName){ - String fileName = dirName+"/"+uploadFile.getName(); - String uploadImageUrl = putS3(uploadFile,fileName); + // File에 저장하지 않고 Memory에서 변환 시행 + public String upload(MultipartFile file, String dirName) throws IOException{ + String fileName = dirName + "/" + file.getOriginalFilename(); + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentType(file.getContentType()); + metadata.setContentLength(file.getSize()); - removeNewFile(uploadFile);// convert()함수로 인해서 로컬에 생성된 File 삭제 (MultipartFile -> File 전환 하며 로컬에 파일 생성됨) - return uploadImageUrl; + try (InputStream inputStream = file.getInputStream()) { + amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, inputStream, metadata)); + log.info("File uploaded successfully: {}", fileName); + return amazonS3Client.getUrl(bucket, fileName).toString(); + } catch (IOException e) { + log.error("Error uploading file: {}", fileName, e); + throw new CustomException(MULTIPARTFILE_CONVERT_FAILE_IN_MEMORY,"Failed to upload file"); + } } + + +// public String upload(File uploadFile, String dirName){ +// String fileName = dirName+"/"+uploadFile.getName(); +// String uploadImageUrl = putS3(uploadFile,fileName); +// +// removeNewFile(uploadFile);// convert()함수로 인해서 로컬에 생성된 File 삭제 (MultipartFile -> File 전환 하며 로컬에 파일 생성됨) +// return uploadImageUrl; +// } // 업로드하기 private String putS3(File uploadFile, String fileName){ amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, uploadFile)