Skip to content

Commit

Permalink
refactor : S3 Service 레이어가 S3 라이브러리를 의존하고 있어서 이를 분리 (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
pie2457 authored Aug 18, 2023
1 parent 6bb5c6f commit a01bcf8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit a01bcf8

Please sign in to comment.