Skip to content

Commit

Permalink
Merge pull request #166 from Katchup-dev/refactor/#165-external
Browse files Browse the repository at this point in the history
[REFACTOR] S3 구조 수정
  • Loading branch information
unanchoi authored Oct 19, 2023
2 parents 72fe199 + a750916 commit f8f790b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.katchup.katchupserver.api.file.domain.File;
import site.katchup.katchupserver.api.file.dto.request.FileCreateRequestDto;
import site.katchup.katchupserver.api.file.dto.response.FileGetDownloadPreSignedResponseDto;
import site.katchup.katchupserver.api.file.dto.response.FileGetUploadPreSignedResponseDto;
import site.katchup.katchupserver.api.file.repository.FileRepository;
import site.katchup.katchupserver.api.file.service.FileService;
import site.katchup.katchupserver.api.member.domain.Member;
import site.katchup.katchupserver.api.member.repository.MemberRepository;
import site.katchup.katchupserver.common.util.S3Util;
import site.katchup.katchupserver.external.s3.PreSignedUrlVO;
import site.katchup.katchupserver.external.s3.S3Service;

import java.util.HashMap;
import java.util.Optional;
import java.util.UUID;

Expand All @@ -24,25 +23,25 @@ public class FileServiceImpl implements FileService {

private static final String FILE_FOLDER_NAME = "files";

private final S3Util s3Util;
private final S3Service s3Service;
private final MemberRepository memberRepository;
private final FileRepository fileRepository;

@Override
@Transactional
public FileGetUploadPreSignedResponseDto getUploadPreSignedUrl(Long memberId, String fileName) {
String fileUploadPrefix = makeUploadPrefix(memberId);
HashMap<String, String> preSignedUrlInfo = s3Util.generatePreSignedUrl(fileUploadPrefix, fileName);
PreSignedUrlVO presignedUrlInfo = s3Service.generatePreSignedUrl(fileUploadPrefix, fileName);

return FileGetUploadPreSignedResponseDto.of(preSignedUrlInfo.get(s3Util.KEY_FILENAME), fileName
, preSignedUrlInfo.get(s3Util.KEY_PRESIGNED_URL), preSignedUrlInfo.get(s3Util.KEY_FILE_UPLOAD_DATE));
return FileGetUploadPreSignedResponseDto.of(presignedUrlInfo.getFileName(), fileName,
presignedUrlInfo.getPreSignedUrl(), presignedUrlInfo.getFileUploadDate());
}

@Override
@Transactional
public FileGetDownloadPreSignedResponseDto getDownloadPreSignedUrl(String fileUUID, String fileName) {
File findFile = fileRepository.findByIdOrThrow(UUID.fromString(fileUUID));
String downloadUrl = s3Util.getDownloadPreSignedUrl(findFile.getFileKey(), fileName);
String downloadUrl = s3Service.getDownloadPreSignedUrl(findFile.getFileKey(), fileName);
return FileGetDownloadPreSignedResponseDto.of(downloadUrl);
}

Expand All @@ -59,12 +58,12 @@ public void deleteFile(Long memberId, String fileOriginalName, String fileUpload
Optional<File> file = fileRepository.findById(UUID.fromString(fileUUID));
if (file.isPresent()) {
fileRepository.delete(file.get());
} s3Util.deleteFile(createKey(memberId, fileUploadDate, fileUUID, fileOriginalName));
} s3Service.deleteFile(createKey(memberId, fileUploadDate, fileUUID, fileOriginalName));
}

private String makeUploadPrefix(Long memberId) {
Member member = memberRepository.findByIdOrThrow(memberId);
String userUUID = member.getUserUUID();
return s3Util.makeUploadPrefix(userUUID, FILE_FOLDER_NAME);
return s3Service.makeUploadPrefix(userUUID, FILE_FOLDER_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.katchup.katchupserver.api.file.domain.File;
import site.katchup.katchupserver.api.member.domain.Member;
import site.katchup.katchupserver.api.member.repository.MemberRepository;
import site.katchup.katchupserver.api.screenshot.domain.Screenshot;
import site.katchup.katchupserver.api.screenshot.dto.request.ScreenshotCreateRequestDto;
import site.katchup.katchupserver.api.screenshot.dto.response.ScreenshotGetPreSignedResponseDto;
import site.katchup.katchupserver.api.screenshot.repository.ScreenshotRepository;
import site.katchup.katchupserver.api.screenshot.service.ScreenshotService;
import site.katchup.katchupserver.common.util.S3Util;
import site.katchup.katchupserver.external.s3.PreSignedUrlVO;
import site.katchup.katchupserver.external.s3.S3Service;

import java.util.HashMap;
import java.util.Optional;
import java.util.UUID;

Expand All @@ -24,24 +23,24 @@ public class ScreenshotServiceImpl implements ScreenshotService {

private static final String SCREENSHOT_FOLDER_NAME = "screenshots";

private final S3Util s3Util;
private final S3Service s3Service;
private final ScreenshotRepository screenshotRepository;
private final MemberRepository memberRepository;

@Override
@Transactional
public ScreenshotGetPreSignedResponseDto getPreSignedUrl(Long memberId, String screenshotName) {
String screenshotUploadPrefix = makeUploadPrefix(memberId);
HashMap<String, String> preSignedUrlInfo = s3Util.generatePreSignedUrl(screenshotUploadPrefix, screenshotName);
PreSignedUrlVO preSignedUrlInfo = s3Service.generatePreSignedUrl(screenshotUploadPrefix, screenshotName);

return ScreenshotGetPreSignedResponseDto.of(preSignedUrlInfo.get(s3Util.KEY_FILENAME)
, preSignedUrlInfo.get(s3Util.KEY_PRESIGNED_URL), screenshotName, preSignedUrlInfo.get(s3Util.KEY_FILE_UPLOAD_DATE));
return ScreenshotGetPreSignedResponseDto.of(preSignedUrlInfo.getFileName(), preSignedUrlInfo.getPreSignedUrl(),
screenshotName, preSignedUrlInfo.getFileUploadDate());
}

@Override
@Transactional
public String findUrl(Long memberId, ScreenshotCreateRequestDto requestDto) {
return s3Util.findUrlByName(createKey(memberId, requestDto.getScreenshotUploadDate(), requestDto.getScreenshotUUID().toString()
return s3Service.findUrlByName(createKey(memberId, requestDto.getScreenshotUploadDate(), requestDto.getScreenshotUUID().toString()
, requestDto.getScreenshotName()));
}

Expand All @@ -58,13 +57,13 @@ public void deleteFile(Long memberId, String screenshotName, String screenshotUp
Optional<Screenshot> screenshot = screenshotRepository.findById(UUID.fromString(screenshotUUID));
if (screenshot.isPresent()) {
screenshotRepository.delete(screenshot.get());
} s3Util.deleteFile(createKey(memberId, screenshotUploadDate, screenshotUUID, screenshotName));
} s3Service.deleteFile(createKey(memberId, screenshotUploadDate, screenshotUUID, screenshotName));
}

private String makeUploadPrefix(Long memberId) {
Member member = memberRepository.findByIdOrThrow(memberId);
String userUUID = member.getUserUUID();
return s3Util.makeUploadPrefix(userUUID, SCREENSHOT_FOLDER_NAME);
return s3Service.makeUploadPrefix(userUUID, SCREENSHOT_FOLDER_NAME);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.katchup.katchupserver.config;
package site.katchup.katchupserver.external.s3;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package site.katchup.katchupserver.external.s3;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor(staticName = "of")
public class PreSignedUrlVO {
private String fileName;
private String fileUploadDate;
private String preSignedUrl;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.katchup.katchupserver.common.util;
package site.katchup.katchupserver.external.s3;

import com.amazonaws.HttpMethod;
import com.amazonaws.services.s3.AmazonS3;
Expand All @@ -17,38 +17,29 @@
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.UUID;

@Component
@RequiredArgsConstructor
public class S3Util {

public static final String KEY_FILENAME = "fileName";
public static final String KEY_FILE_UPLOAD_DATE = "fileUploadDate";

public static final String KEY_PRESIGNED_URL = "preSignedUrl";
public class S3Service {

private final AmazonS3 amazonS3;
@Value("${cloud.aws.s3.bucket}")
private String bucket;
@Value("${cloud.aws.region.static}")
private String location;
private String region;

public HashMap<String, String> generatePreSignedUrl(String prefix, String fileName) {
HashMap<String, String> result = new HashMap<>();
public PreSignedUrlVO generatePreSignedUrl(String prefix, String fileName) {
String uuidFileName = getUUIDFile();
result.put(KEY_FILENAME, uuidFileName);
String filePath = prefix + "/" + getDateFolder() + "/" + uuidFileName + fileName;
GeneratePresignedUrlRequest generatePresignedUrlRequest = getGeneratePreSignedUrlRequest(bucket, filePath);
result.put(KEY_PRESIGNED_URL, amazonS3.generatePresignedUrl(generatePresignedUrlRequest).toString());
result.put(KEY_FILE_UPLOAD_DATE, getDateFolder());
return result;
String presignedUrl = amazonS3.generatePresignedUrl(generatePresignedUrlRequest).toString();
return PreSignedUrlVO.of(uuidFileName, getDateFolder(), presignedUrl);
}

public String findUrlByName(String path) {
return "https://" + bucket + ".s3." + location + ".amazonaws.com/" + path;
return "https://" + bucket + ".s3." + region + ".amazonaws.com/" + path;
}

public String getDownloadPreSignedUrl(String filePath, String fileName) {
Expand Down

0 comments on commit f8f790b

Please sign in to comment.