Heroku 배포 후 이미지 업로드로 상당히 애를 먹었고 사용 방법은 AWS S3를 이용해서 하려고 했습니다.
하지만 GitHub를 통해서 배포를 하는데에 있어서 IAM AccessKey가 GitHub에 노출이 되면
AccessKey를 변경하라는 이메일이 오고 권한을 막아버리는 정책을 추가합니다.
그래서 Heroku 배포를 포기하고 AWS EC2를 이용해서 배포를 했습니다.
생각보다 어렵진 않았습니다.
AWS로 배포를 하면 기존에 사용하던 방법으로 파일을 로컬에 저장, 삭제를 하면 되기 때문에
오히려 더 쉬운 것 같습니다.
@Slf4j
@Service
@RequiredArgsConstructor
public class FileServiceImpl implements FileService {
@Value("${file.path}")
private String filePath;
// 값이 있는지 확인
@Override
public boolean checkUploadFileListIsBlank(List<MultipartFile> fileList) throws Exception {
for (MultipartFile file : fileList) {
if (!file.getOriginalFilename().isBlank()) {
return false;
}
}
return true;
}
// 파일 생성
@Override
public String createFileByFileAndPath(MultipartFile file, String customPath) throws IOException {
Path tempPath = null;
String tempPathName = customPath + getTempFileName(file);
tempPath = Paths.get(filePath + tempPathName);
makeDirectory(filePath + customPath);
Files.write(tempPath, file.getBytes());
return tempPathName;
}
// 파일 생성
@Override
public void createSolutionFileByFileAndPath(List<MultipartFile> fileList, List<String> tempFileNameList, String customPath) throws IOException {
for (int i = 0; i < fileList.size(); i++) {
try {
Path path = Paths.get(filePath + customPath, tempFileNameList.get(i));
makeDirectory(filePath + customPath);
Files.write(path, fileList.get(i).getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 파일 삭제
@Override
public void deleteFileByFileNameAndPath(String fileName, String customPath) throws IOException {
Path path = Paths.get(filePath + customPath + fileName);
File f = new File(path.toString());
if (f.exists()) {
Files.delete(path);
}
}
// 폴더 삭제
@Override
public void deleteTempFolderByPath(String customPath) throws IOException {
Path path = Paths.get(filePath + customPath);
File f = new File(path.toString());
if (f.exists()) {
// 하위 파일 전체 삭제
for (File deleteFile : f.listFiles()) {
deleteFile.delete();
}
// 상위 폴더 삭제
if (f.listFiles().length == 0 && f.isDirectory()) {
Files.deleteIfExists(path);
}
}
}
// UUID + _파일 이름 생성 후 반환
private String getTempFileName(MultipartFile file) {
return UUID.randomUUID().toString().replaceAll("-", "") + "_" + file.getOriginalFilename();
}
// 폴더 생성
private void makeDirectory(String customPath) throws IOException {
File file = new File(customPath);
if (!file.exists()) {
file.mkdirs();
}
}
}
댓글