|
1 | 1 | package org.yourssu.festa.service; |
2 | 2 |
|
3 | | -import com.amazonaws.services.s3.AmazonS3Client; |
4 | | -import com.amazonaws.services.s3.model.CannedAccessControlList; |
5 | | -import com.amazonaws.services.s3.model.PutObjectRequest; |
| 3 | +import com.amazonaws.services.s3.AmazonS3; |
| 4 | +import com.amazonaws.services.s3.model.ObjectMetadata; |
6 | 5 | import lombok.RequiredArgsConstructor; |
7 | 6 | import lombok.extern.slf4j.Slf4j; |
| 7 | +import org.springframework.beans.factory.annotation.Value; |
8 | 8 | import org.springframework.stereotype.Component; |
9 | 9 | import org.springframework.web.multipart.MultipartFile; |
10 | | -import org.yourssu.festa.common.code.BoothErrorCode; |
11 | | -import org.yourssu.festa.config.AmazonConfig; |
12 | | -import org.yourssu.festa.exception.CustomException; |
13 | | - |
14 | | -import java.io.File; |
15 | | -import java.io.FileOutputStream; |
16 | 10 | import java.io.IOException; |
17 | 11 | import java.util.*; |
18 | 12 |
|
19 | 13 | @Component |
20 | 14 | @RequiredArgsConstructor |
21 | 15 | @Slf4j |
22 | 16 | public class AmazonS3Manager { |
23 | | - private final AmazonS3Client amazonS3Client; |
24 | | - private final AmazonConfig amazonConfig; |
| 17 | + private final AmazonS3 amazonS3; |
25 | 18 |
|
26 | | - public List<String> upload(List<MultipartFile> multipartFiles) throws IOException{ |
27 | | - List<String> uploadImageUrls = new ArrayList<>(); |
28 | | - String dirName = amazonConfig.getReviewPath(); |
29 | | - for (MultipartFile multipartFile : multipartFiles){ |
30 | | - File uploadFile = convert(multipartFile) |
31 | | - .orElseThrow(() -> new CustomException(BoothErrorCode.BOOTH_UPLOAD_IMAGE_FAILED)); |
32 | | - String uploadImageUrl = upload(uploadFile, dirName); |
33 | | - uploadImageUrls.add(uploadImageUrl); |
34 | | - } |
35 | | - return uploadImageUrls; // 업로드된 파일들의 S3 URL 주소 리스트 반환 |
36 | | - } |
| 19 | + @Value("${cloud.aws.s3.bucket}") |
| 20 | + private String bucket; |
37 | 21 |
|
38 | | - private String upload(File uploadFile, String dirName){ |
39 | | - String uuid = UUID.randomUUID().toString(); |
40 | | - String fileName = dirName + "/" + uuid + uploadFile.getName(); |
41 | | - String uploadImageUrl = putS3(uploadFile, fileName); |
42 | | - removeNewFile(uploadFile); // 로컬에 생성된 File 삭제 (MultipartFile -> File 전환 하며 로컬에 파일 생성됨) |
43 | | - return uploadImageUrl; |
44 | | - } |
| 22 | + public List<String> upload(List<MultipartFile> multipartFiles) throws IOException { |
| 23 | + List<String> fileUrls = new ArrayList<>(); |
45 | 24 |
|
46 | | - private String putS3(File uploadFile, String fileName) { |
47 | | - amazonS3Client.putObject( |
48 | | - new PutObjectRequest(amazonConfig.getBucket(), fileName, uploadFile) |
49 | | - .withCannedAcl(CannedAccessControlList.PublicRead) // PublicRead 권한으로 업로드 됨 |
50 | | - ); |
51 | | - return amazonS3Client.getUrl(amazonConfig.getBucket(), fileName).toString(); |
52 | | - } |
| 25 | + for (MultipartFile multipartFile : multipartFiles) { |
| 26 | + String originalFilename = multipartFile.getOriginalFilename(); |
53 | 27 |
|
54 | | - private void removeNewFile(File targetFile) { |
55 | | - if (targetFile.delete()) { |
56 | | - log.info("파일이 삭제되었습니다."); |
57 | | - } else { |
58 | | - log.info("파일이 삭제되지 못했습니다."); |
59 | | - } |
60 | | - } |
| 28 | + ObjectMetadata metadata = new ObjectMetadata(); |
| 29 | + metadata.setContentLength(multipartFile.getSize()); |
| 30 | + metadata.setContentType(multipartFile.getContentType()); |
61 | 31 |
|
62 | | - private Optional<File> convert(MultipartFile file) throws IOException { |
63 | | - File convertFile = new File(Objects.requireNonNull(file.getOriginalFilename())); |
64 | | - if (convertFile.createNewFile()) { |
65 | | - try (FileOutputStream fos = new FileOutputStream(convertFile)) { |
66 | | - fos.write(file.getBytes()); |
67 | | - } |
68 | | - return Optional.of(convertFile); |
| 32 | + amazonS3.putObject(bucket, originalFilename, multipartFile.getInputStream(), metadata); |
| 33 | + String fileUrl = amazonS3.getUrl(bucket, originalFilename).toString(); |
| 34 | + fileUrls.add(fileUrl); |
69 | 35 | } |
70 | | - return Optional.empty(); |
| 36 | + |
| 37 | + return fileUrls; |
71 | 38 | } |
72 | 39 | } |
0 commit comments