μ€λμ S3 λ²ν·μ λ±λ‘νκΈ° μν νμΌ μ λ‘λ λ‘μ§μ λν μ λ€λ¦ μ μ©μ νμκ³ νλ‘ νΈ μλ μ‘°κΈ, ν μ€νΈ μ½λ μμ±μ νμλ€
AWS S3 λ²ν·μ μ΄λ―Έμ§λ₯Ό λ±λ‘νλ λΉμ¦λμ€ λ‘μ§μ΄ μμλ€.
@Transactional
public void uploadImage(Product requestType, MultipartFile file) throws IOException {
String imageLocation = bucketUrl;
String imageName = file.getOriginalFilename();
String requestTypeSimpleName = "product" + "/";
String imagePath = imageLocation + requestTypeSimpleName + imageName;
String fileName = requestTypeSimpleName + file.getOriginalFilename();
Image productImage = Product.
createProductImage(fileName, imagePath);
imageRepository.save(productImage);
createS3Bucket(fileName, file);
}
μ΄λ¬ν λ‘μ§μ μ€μ§ Product (μν) κ°μ²΄μ λν΄μλ§ νμΌμ μ
λ‘λ ν μ μλ κΈ°λ₯μ΄μμ΅λλ€.
νμ§λ§ νλ‘μ νΈλ₯Ό μ§ννλ©΄μ, νμΌ μ λ‘λλ₯Ό 곡ν΅μ μΌλ‘ μ¬μ©ν΄μΌν λΆλΆμ΄ κ²μκΈμ μμ±ν λ, μνμ λ±λ‘ν λ μμ΅λλ€.
β‘ μ΄λ¬ν λ¬Έμ μ μ ν΄κ²°νκΈ°μν΄ Generic (μ λ€λ¦) νμ
μ μ¬μ©ν νμμ±μ λλΌκ² λμμ΅λλ€.
@Service
@RequiredArgsConstructor
public class ImageServiceImpl implements ImageService {
private final AmazonS3Client amazonS3Client;
private final ImageRepository imageRepository;
@Value("${cloud.aws.s3.bucket}")
private String bucket;
@Value("${cloud.aws.s3.url}")
private String bucketUrl;
@Transactional
public <T> Optional<Image> uploadImage(T requestType, MultipartFile file) throws IOException {
String imageLocation = bucketUrl;
String imageName = file.getOriginalFilename();
String requestTypeSimpleName = requestType.getClass().getSimpleName() + "/";
String imagePath = imageLocation + requestTypeSimpleName + imageName;
String fileName = requestTypeSimpleName + file.getOriginalFilename();
Optional<Image> image = Optional.empty();
if (requestType instanceof Product) {
Image productImage = Product.createProductImage(fileName, imagePath);
image = Optional.of(productImage);
}
if (requestType instanceof Post) {
Image postImage = Post.createProductImage(fileName, imagePath);
image = Optional.of(postImage);
}
if (image.isPresent()) {
imageRepository.save(image.get());
createS3Bucket(fileName, file);
}
return image;
}
private void createS3Bucket(String fileName, MultipartFile image) throws IOException {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(image.getContentType());
metadata.setContentLength(image.getSize());
amazonS3Client.putObject(bucket, fileName, image.getInputStream(), metadata);
}
}
μ½λλ₯Ό νλ νλ λ―μ΄ λ³΄κ² μ΅λλ€.
private final AmazonS3Client amazonS3Client;
private final ImageRepository imageRepository;
@Value("${cloud.aws.s3.bucket}")
private String bucket;
@Value("${cloud.aws.s3.url}")
private String bucketUrl;
AmazonS3Client : AWS S3μ μ κ·ΌνκΈ° μν΄ μ 곡λλ κ°μ²΄
Value : yml μ€μ νμΌμ μμ±ν AWS S3 λ²ν·μ λν μ 보
<T> Optional<Image> uploadImage(T requestType, MultipartFile file)
ν΄λΉ νμΌ μ λ‘λμμ ν΅μ¬μ μΈ λ‘μ§μ΄λΌκ³ ν μ μλ€.
T : λ€μν νμ
μ λ§€κ°λ³μλ‘ λ°μ μ μλ€λ μλ―Έμ
λλ€,
μ¦ Product κ°μ²΄μ, Post κ°μ²΄λ₯Ό λ λ€ λ°μμ μμ΄, μ½λμ μ¬μ¬μ©μ±μ λμΌ μ μμ΅λλ€.
controller μμ Post μμ²μΌλ‘ API ν
μ€νΈ νκ² λλ©΄ μ€μ λ‘ S3μ ν¨ν€μ§κ° λ§λ€μ΄μ§κ³ ν¨ν€μ§ μμ νμΌμ΄ μ μ₯λκ² λ©λλ€.