Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,22 @@ public class S3Service {
* @return 업로드된 파일의 S3 URL
*/
public String uploadFile(MultipartFile file, String folderPath, boolean isPublic) {
String fileName = UUID.randomUUID() + "_" + sanitizeFileName(file.getOriginalFilename());
// 파일명: UUID + 확장자만 추출
String extension = getExtension(file.getOriginalFilename());
String fileName = UUID.randomUUID() + (extension != null ? "." + extension : "");
String s3Key = folderPath + "/" + fileName;

try {
PutObjectRequest.Builder requestBuilder = PutObjectRequest.builder()
.bucket(bucketName)
.key(s3Key)
.contentType(file.getContentType());

s3Client.putObject(
PutObjectRequest.builder()
.bucket(bucketName)
.key(s3Key)
.contentType(file.getContentType())
.build(),
requestBuilder.build(),
RequestBody.fromInputStream(file.getInputStream(), file.getSize())
);

} catch (IOException e) {
throw new RuntimeException("파일 입력 스트림을 읽지 못했습니다.", e);
} catch (S3Exception e) {
Expand Down Expand Up @@ -97,4 +102,14 @@ public String generatePresignedUrl(String s3Key, Duration duration) {
return presignedRequest.url().toString();
}
}

/**
* 파일 확장자 추출
*/
private String getExtension(String fileName) {
if (fileName == null || !fileName.contains(".")) {
return null;
}
return fileName.substring(fileName.lastIndexOf('.') + 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class StoragePortfolioService {
@Transactional
public String uploadMentorPortfolio(PortfolioUploadRequest request, Long memberId) {
String folderPath = "portfolio/mentor/" + memberId;
String filePath = s3Service.uploadFile(request.file(), folderPath, false);
String filePath = s3Service.uploadFile(request.file(), folderPath, true);

fileMemberRepository.save(request.toFileMemberEntity(filePath, memberId));
return filePath;
Expand Down