diff --git a/backend/src/main/java/kr/codesquad/issuetracker/application/S3Service.java b/backend/src/main/java/kr/codesquad/issuetracker/application/S3Service.java index 8606d56f8..231b74b5f 100644 --- a/backend/src/main/java/kr/codesquad/issuetracker/application/S3Service.java +++ b/backend/src/main/java/kr/codesquad/issuetracker/application/S3Service.java @@ -1,52 +1,27 @@ package kr.codesquad.issuetracker.application; -import java.net.URLDecoder; -import java.nio.charset.StandardCharsets; - import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; -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 kr.codesquad.issuetracker.application.s3.S3Uploader; import kr.codesquad.issuetracker.domain.ImageFile; -import kr.codesquad.issuetracker.infrastructure.config.AwsProperties; +import lombok.RequiredArgsConstructor; +@RequiredArgsConstructor @Service public class S3Service { - private static final String UPLOADED_IMAGES_DIR = "public/uploaded-images/"; - private static final String PROFILE_IMAGES_DIR = "public/profile-images/"; - - private final AmazonS3Client amazonS3Client; - private final String bucket; - - public S3Service(AmazonS3Client amazonS3Client, AwsProperties awsProperties) { - this.amazonS3Client = amazonS3Client; - this.bucket = awsProperties.getS3().getBucket(); - } - - public String uploadImage(MultipartFile file) { - ImageFile imageFile = ImageFile.from(file); - - // 버킷에 저장할 파일 이름 생성 - String fileName = UPLOADED_IMAGES_DIR + imageFile.getRandomName(); + private static final String UPLOADED_IMAGES_DIR = "public/uploaded-images/"; - uploadImageToS3(imageFile, fileName); - return getObjectUri(fileName); - } + private final S3Uploader s3Uploader; - private void uploadImageToS3(ImageFile imageFile, String fileName) { - ObjectMetadata metadata = new ObjectMetadata(); - metadata.setContentType(imageFile.getContentType()); - metadata.setContentLength(imageFile.getFileSize()); - amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, imageFile.getImageInputStream(), metadata) - .withCannedAcl(CannedAccessControlList.PublicRead)); - } + @Transactional + public String uploadImage(MultipartFile file) { + ImageFile imageFile = ImageFile.from(file); - private String getObjectUri(String fileName) { - return URLDecoder.decode(amazonS3Client.getUrl(bucket, fileName).toString(), StandardCharsets.UTF_8); - } + // 버킷에 저장할 파일 이름 생성 + String fileName = UPLOADED_IMAGES_DIR + imageFile.getRandomName(); + return s3Uploader.uploadImageToS3(imageFile, fileName); + } } diff --git a/backend/src/main/java/kr/codesquad/issuetracker/application/s3/S3Uploader.java b/backend/src/main/java/kr/codesquad/issuetracker/application/s3/S3Uploader.java new file mode 100644 index 000000000..9e093ac69 --- /dev/null +++ b/backend/src/main/java/kr/codesquad/issuetracker/application/s3/S3Uploader.java @@ -0,0 +1,39 @@ +package kr.codesquad.issuetracker.application.s3; + +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; + +import org.springframework.stereotype.Component; + +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 kr.codesquad.issuetracker.domain.ImageFile; +import kr.codesquad.issuetracker.infrastructure.config.AwsProperties; + +@Component +public class S3Uploader { + + private final AmazonS3Client amazonS3Client; + private final String bucket; + + public S3Uploader(AmazonS3Client amazonS3Client, AwsProperties awsProperties) { + this.amazonS3Client = amazonS3Client; + this.bucket = awsProperties.getS3().getBucket(); + } + + public String uploadImageToS3(ImageFile imageFile, String fileName) { + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentType(imageFile.getContentType()); + metadata.setContentLength(imageFile.getFileSize()); + amazonS3Client.putObject(new PutObjectRequest(bucket, fileName, imageFile.getImageInputStream(), metadata) + .withCannedAcl(CannedAccessControlList.PublicRead)); + return getObjectUri(fileName); + } + + private String getObjectUri(String fileName) { + return URLDecoder.decode(amazonS3Client.getUrl(bucket, fileName).toString(), StandardCharsets.UTF_8); + } +}