From 08ced510449f3da928d0f927201de47f9eaaa732 Mon Sep 17 00:00:00 2001 From: okod Date: Sun, 9 Jun 2024 03:50:43 +0900 Subject: [PATCH] fix: Modify S3 Filename to randomUUID --- src/main/java/vom/spring/domain/album/AlbumService.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/vom/spring/domain/album/AlbumService.java b/src/main/java/vom/spring/domain/album/AlbumService.java index a3f753e..8444b56 100644 --- a/src/main/java/vom/spring/domain/album/AlbumService.java +++ b/src/main/java/vom/spring/domain/album/AlbumService.java @@ -16,6 +16,7 @@ import java.io.IOException; import java.time.LocalDateTime; import java.util.List; +import java.util.UUID; import java.util.stream.Collectors; @Service @@ -41,19 +42,21 @@ public void setS3Client(AmazonS3Client amazonS3Client) { @Transactional public void uploadAlbum(Long memberId, MultipartFile multipartFile) throws IOException { Homepy homepy = homepyRepository.findByMember_id(memberId); + String originalFilename = multipartFile.getOriginalFilename(); + String uniqueFilename = UUID.randomUUID().toString(); ObjectMetadata metadata = new ObjectMetadata(); metadata.setContentLength(multipartFile.getSize()); metadata.setContentType(multipartFile.getContentType()); - amazonS3Client.putObject(bucket, originalFilename, multipartFile.getInputStream(), metadata); + amazonS3Client.putObject(bucket, uniqueFilename, multipartFile.getInputStream(), metadata); albumRepository.save( Album.builder() .homepy(homepy) .name(originalFilename) - .img_url(amazonS3Client.getUrl(bucket, originalFilename).toString()) + .img_url(amazonS3Client.getUrl(bucket, uniqueFilename).toString()) .createdAt(LocalDateTime.now()) .build() );