Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
[Bugfix] S3 Presigned를 사용할 때 확장자가 포함되지 않은 버그 수정
Browse files Browse the repository at this point in the history
motivation:
- presigend 요청 후 파일을 저장할 때 확장자가 붙지 않은 상황이 발생했습니다. 확장자를 고려하지 않고 UUID와 LocalDateTime 만을 조합했기 때문에 발생한 문제였습니다.

modification:
- 확장자 추출 메서드를 추가해 수정을 했습니다.

Close #177
  • Loading branch information
Nine-JH committed Jan 24, 2024
1 parent 291da6f commit 88d527c
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions app/src/main/java/fc/be/app/global/s3/service/S3Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,28 @@ public PreSignedResponses getPreSignedUrl(List<String> imageNames) {
List<PreSignedElement> elements = new ArrayList<>();

for (String imageName : imageNames) {
final String encodedFileName = createEncodedFileName();
GeneratePresignedUrlRequest generatePresignedUrlRequest = createGeneratePresignedUrlRequest(encodedFileName);
String fileExtension = extractFileExtension(imageName);
final String savedFileName = createEncodedFileName() + fileExtension;
GeneratePresignedUrlRequest generatePresignedUrlRequest = createGeneratePresignedUrlRequest(savedFileName);
String preSignedUrl = amazonS3.generatePresignedUrl(generatePresignedUrlRequest).toString();
elements.add(new PreSignedElement(imageName, preSignedUrl));
}

return new PreSignedResponses(elements);
}

private String extractFileExtension(String imageName) {
int extensionStartIndex = imageName.lastIndexOf(".");
return imageName.substring(extensionStartIndex);
}

private static String createEncodedFileName() {
String uuid = UUID.randomUUID().toString();
return uuid + "_" + LocalDateTime.now();
}

private GeneratePresignedUrlRequest createGeneratePresignedUrlRequest(String encodedFileName) {
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, encodedFileName)
private GeneratePresignedUrlRequest createGeneratePresignedUrlRequest(String fileName) {
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, fileName)
.withMethod(HttpMethod.PUT)
.withExpiration(getPreSignedUrlExpiration());

Expand Down

0 comments on commit 88d527c

Please sign in to comment.