-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- S3Service를 직접 주입받는 대신 StorageService 인터페이스로 대체
- Loading branch information
1 parent
c76607f
commit 9b3c710
Showing
10 changed files
with
134 additions
and
142 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.drinkeg.drinkeg.S3; | ||
|
||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.DeleteObjectRequest; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import com.drinkeg.drinkeg.apipayLoad.code.status.ErrorStatus; | ||
import com.drinkeg.drinkeg.config.S3Config; | ||
import com.drinkeg.drinkeg.exception.GeneralException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class S3Manager implements StorageManager { | ||
private final AmazonS3 amazonS3; | ||
private final S3Config s3Config; | ||
|
||
@Override | ||
public String uploadFile(MultipartFile file, String keyPath) { | ||
ObjectMetadata metadata = new ObjectMetadata(); | ||
metadata.setContentLength(file.getSize()); | ||
|
||
try { | ||
amazonS3.putObject( | ||
new PutObjectRequest(s3Config.getBucket(), | ||
keyPath, | ||
file.getInputStream(), | ||
metadata) | ||
); | ||
} catch (IOException e) { | ||
// 로깅 필요 | ||
throw new GeneralException(ErrorStatus.FILE_UPLOAD_FAILED); | ||
} | ||
|
||
return amazonS3.getUrl(s3Config.getBucket(), keyPath) | ||
.toString(); | ||
} | ||
|
||
@Override | ||
public void deleteFile(String keyPath) { | ||
amazonS3.deleteObject(new DeleteObjectRequest(s3Config.getBucket(), keyPath)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,89 @@ | ||
package com.drinkeg.drinkeg.S3; | ||
|
||
|
||
import com.amazonaws.AmazonServiceException; | ||
import com.amazonaws.SdkClientException; | ||
import com.amazonaws.services.s3.AmazonS3; | ||
import com.amazonaws.services.s3.model.DeleteObjectRequest; | ||
import com.amazonaws.services.s3.model.ObjectMetadata; | ||
import com.amazonaws.services.s3.model.PutObjectRequest; | ||
import com.drinkeg.drinkeg.apipayLoad.code.status.ErrorStatus; | ||
import com.drinkeg.drinkeg.config.S3Config; | ||
import com.drinkeg.drinkeg.domain.Uuid; | ||
import com.drinkeg.drinkeg.exception.GeneralException; | ||
import com.drinkeg.drinkeg.repository.UuidRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.extern.slf4j.XSlf4j; | ||
import org.springframework.dao.DataIntegrityViolationException; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class S3Service { | ||
|
||
|
||
private final AmazonS3Manager s3Manager; | ||
@Slf4j | ||
public class S3Service implements StorageService { | ||
private final UuidRepository uuidRepository; | ||
private final S3Manager s3Manager; | ||
|
||
public String SaveImage(MultipartFile image){ //이미지 한개 저장할 때 | ||
|
||
|
||
String uuid = UUID.randomUUID().toString(); | ||
Uuid savedUuid = uuidRepository.save(Uuid.builder() | ||
.uuid(uuid).build()); | ||
|
||
String pictureUrl = s3Manager.uploadFile(s3Manager.generateWineNewsKeyName(savedUuid), image); | ||
|
||
|
||
return pictureUrl; | ||
private String generateKeyPath(Uuid uuid, String path) { | ||
return path + '/' + uuid.getUuid(); | ||
} | ||
|
||
public void deleteFile(String pictureUrl){ // 사진 삭제 | ||
|
||
String savedUuid = pictureUrl.substring(pictureUrl.lastIndexOf("/wineNews/") + "/wineNews/".length()); | ||
Uuid uuid = uuidRepository.findByUuid(savedUuid); | ||
public String uploadFile(MultipartFile file, String path) { | ||
try { | ||
Uuid uuid = uuidRepository.save(Uuid.builder() | ||
.uuid(UUID.randomUUID().toString()) | ||
.build()); | ||
|
||
s3Manager.deleteFile(s3Manager.generateWineNewsKeyName(uuid)); | ||
String url = s3Manager.uploadFile(file, generateKeyPath(uuid, path)); | ||
|
||
return url; | ||
} catch (DataIntegrityViolationException e) { // 유니크 제약조건 위반시 발생 | ||
throw new GeneralException(ErrorStatus.FILE_UPLOAD_FAILED); | ||
} | ||
} | ||
|
||
public void SaveImages(List<MultipartFile> pictureList){ // List로 들어온 multipartFile 이미지 저장 | ||
@Transactional | ||
public List<String> uploadFiles(List<MultipartFile> files, String path) { | ||
List<String> FileUrls = new ArrayList<>(); | ||
for (MultipartFile file : files) { | ||
try { | ||
Uuid uuid = uuidRepository.save(Uuid.builder() | ||
.uuid(UUID.randomUUID().toString()) | ||
.build()); | ||
|
||
String url = s3Manager.uploadFile(file, generateKeyPath(uuid, path)); | ||
|
||
FileUrls.add(url); | ||
} catch (DataIntegrityViolationException e) { // 유니크 제약조건 위반시 발생 | ||
throw new GeneralException(ErrorStatus.FILE_UPLOAD_FAILED); | ||
} | ||
} | ||
return FileUrls; | ||
} | ||
|
||
for(MultipartFile picture : pictureList){ // picture마다 유일한 URL 값 생성 | ||
String uuid = UUID.randomUUID().toString(); | ||
Uuid savedUuid = uuidRepository.save(Uuid.builder().uuid(uuid).build()); | ||
public void deleteFile(String url) { | ||
String keyPath = URI.create(url) | ||
.getPath() | ||
.replaceFirst("^/", ""); | ||
|
||
String pictureUrl = s3Manager.uploadFile(s3Manager.generateWineNewsKeyName(savedUuid), picture); | ||
String uuid = keyPath.substring(keyPath.lastIndexOf('/') + 1); | ||
|
||
try { | ||
uuidRepository.deleteByUuid(uuid); | ||
} catch (RuntimeException e) { | ||
throw new GeneralException(ErrorStatus.FILE_DELETE_FAILED); | ||
} | ||
|
||
s3Manager.deleteFile(keyPath); | ||
} | ||
} | ||
} |
31 changes: 0 additions & 31 deletions
31
src/main/java/com/drinkeg/drinkeg/S3/S3TestController.java
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.drinkeg.drinkeg.S3; | ||
|
||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
public interface StorageManager { | ||
public String uploadFile(MultipartFile file, String keyPath); | ||
public void deleteFile(String keyPath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.drinkeg.drinkeg.S3; | ||
|
||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.List; | ||
|
||
public interface StorageService { | ||
public String uploadFile(MultipartFile file, String path); | ||
public List<String> uploadFiles(List<MultipartFile> files, String path); | ||
public void deleteFile(String fileName); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters