Refactor: 이미지 업로드 형식 변경

This commit is contained in:
김용수 2024-10-02 17:37:03 +09:00
parent a2f44f049b
commit 6a32b071ff
3 changed files with 14 additions and 10 deletions

View File

@ -52,8 +52,8 @@ public class ImageController {
} }
@PostMapping("/folders/{folder_id}/images/presigned") @PostMapping("/folders/{folder_id}/images/presigned")
@SwaggerApiSuccess(description = "압축폴더를 성공적으로 업로드합니다.") @SwaggerApiSuccess(description = "이미지에 대한 presigned 주소를 받아옵니다..")
@Operation(summary = "압축 폴더 업로드", description = "압축 폴더 내 폴더와 이미지 파일을 업로드합니다.") @Operation(summary = "이미지에 대한 Presigned ", description = "이미지에 대한 Presigned 주소를 받아옵니다.")
@SwaggerApiError({ErrorCode.BAD_REQUEST, ErrorCode.NOT_AUTHOR, ErrorCode.SERVER_ERROR}) @SwaggerApiError({ErrorCode.BAD_REQUEST, ErrorCode.NOT_AUTHOR, ErrorCode.SERVER_ERROR})
public List<ImagePresignedUrlResponse> uploadFolderByPresignedImage( public List<ImagePresignedUrlResponse> uploadFolderByPresignedImage(
@CurrentUser final Integer memberId, @CurrentUser final Integer memberId,

View File

@ -330,16 +330,16 @@ public class ImageService {
for (ImageMetaRequest meta : imageMetaList) { for (ImageMetaRequest meta : imageMetaList) {
// UUID 생성 이미지 Key 지정 // UUID 생성 이미지 Key 지정
String uuid = UUID.randomUUID().toString(); String key = UUID.randomUUID().toString().substring(0,13);
String fileName = meta.getFileName(); String fileName = meta.getFileName();
String extension = getExtension(fileName); String extension = getExtension(fileName);
String s3Key = uuid + "." + extension;
// Presigned URL 생성 // Presigned URL 생성
String presignedUrl = s3UploadService.generatePresignedUrl(s3Key); String presignedUrl = s3UploadService.generatePresignedUrl(key, extension);
log.debug("presignedUrl {}", presignedUrl);
// DB에 이미지 메타데이터 저장 // DB에 이미지 메타데이터 저장
Image image = Image.of(fileName, s3Key, extension, folder); Image image = Image.of(fileName, s3UploadService.addBucketPrefix(key), extension, folder);
imageRepository.save(image); imageRepository.save(image);
// Presigned URL과 함께 응답 데이터 생성 // Presigned URL과 함께 응답 데이터 생성

View File

@ -90,7 +90,7 @@ public class S3UploadService {
PutObjectRequest putRequest = new PutObjectRequest(bucket, s3FileName, inputStream, metadata); PutObjectRequest putRequest = new PutObjectRequest(bucket, s3FileName, inputStream, metadata);
amazonS3.putObject(putRequest); amazonS3.putObject(putRequest);
return url + "/" + s3Key; return addBucketPrefix(s3Key);
} }
/** /**
@ -137,7 +137,7 @@ public class S3UploadService {
} }
} }
private static String getS3FileName(final Integer projectId) { public static String getS3FileName(final Integer projectId) {
return projectId + "/" + UUID.randomUUID().toString().substring(0, 13); return projectId + "/" + UUID.randomUUID().toString().substring(0, 13);
} }
@ -158,17 +158,21 @@ public class S3UploadService {
/** /**
* Presigned URL 생성 * Presigned URL 생성
*/ */
public String generatePresignedUrl(String s3Key) { public String generatePresignedUrl(String key, String extension) {
Date expiration = new Date(); Date expiration = new Date();
long expTimeMillis = expiration.getTime(); long expTimeMillis = expiration.getTime();
expTimeMillis += 1000 * 60 * 10; // 10분간 유효 expTimeMillis += 1000 * 60 * 10; // 10분간 유효
expiration.setTime(expTimeMillis); expiration.setTime(expTimeMillis);
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket, s3Key) GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket, key + "." + extension)
.withMethod(HttpMethod.PUT) // PUT 방식 .withMethod(HttpMethod.PUT) // PUT 방식
.withExpiration(expiration); .withExpiration(expiration);
URL url = amazonS3.generatePresignedUrl(generatePresignedUrlRequest); URL url = amazonS3.generatePresignedUrl(generatePresignedUrlRequest);
return url.toString(); return url.toString();
} }
public String addBucketPrefix(String key){
return url + "/" + key;
}
} }