|
| 1 | +package com.threestar.trainus.domain.file.service; |
| 2 | + |
| 3 | +import java.net.URL; |
| 4 | +import java.util.Date; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.UUID; |
| 7 | + |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.stereotype.Service; |
| 10 | +import org.springframework.transaction.annotation.Transactional; |
| 11 | + |
| 12 | +import com.amazonaws.HttpMethod; |
| 13 | +import com.amazonaws.services.s3.AmazonS3; |
| 14 | +import com.amazonaws.services.s3.Headers; |
| 15 | +import com.amazonaws.services.s3.model.CannedAccessControlList; |
| 16 | +import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; |
| 17 | +import com.threestar.trainus.domain.file.dto.GetS3UrlDto; |
| 18 | + |
| 19 | +import lombok.RequiredArgsConstructor; |
| 20 | + |
| 21 | +@Service |
| 22 | +@RequiredArgsConstructor |
| 23 | +public class S3Service { |
| 24 | + @Value("${cloud.aws.s3.bucket}") |
| 25 | + private String bucket; |
| 26 | + |
| 27 | + private final AmazonS3 amazonS3; |
| 28 | + |
| 29 | + public GetS3UrlDto getPostS3Url(String prefix, String fileName) { |
| 30 | + if (!prefix.isEmpty()) { |
| 31 | + fileName = createPath(prefix, fileName); |
| 32 | + } |
| 33 | + |
| 34 | + GeneratePresignedUrlRequest generatePresignedUrlRequest = getGeneratePresignedUrlRequest(bucket, fileName); |
| 35 | + URL url = amazonS3.generatePresignedUrl(generatePresignedUrlRequest); |
| 36 | + |
| 37 | + return GetS3UrlDto.builder() |
| 38 | + .preSignedUrl(url.toString()) |
| 39 | + .key(fileName) |
| 40 | + .build(); |
| 41 | + } |
| 42 | + |
| 43 | + // 파일 업로드용 Presigned URL 생성 |
| 44 | + private GeneratePresignedUrlRequest getGeneratePresignedUrlRequest(String bucket, String fileName) { |
| 45 | + GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucket, fileName) |
| 46 | + .withMethod(HttpMethod.PUT) |
| 47 | + .withExpiration(getPresignedUrlExpiration()); |
| 48 | + |
| 49 | + generatePresignedUrlRequest.addRequestParameter( |
| 50 | + Headers.S3_CANNED_ACL, |
| 51 | + CannedAccessControlList.PublicRead.toString() |
| 52 | + ); |
| 53 | + |
| 54 | + return generatePresignedUrlRequest; |
| 55 | + } |
| 56 | + |
| 57 | + //Presigned URL 유효기간 설정 |
| 58 | + private Date getPresignedUrlExpiration() { |
| 59 | + Date expiration = new Date(); |
| 60 | + long expTimeMillis = expiration.getTime(); |
| 61 | + expTimeMillis += 1000 * 60 * 10; |
| 62 | + expiration.setTime(expTimeMillis); |
| 63 | + |
| 64 | + return expiration; |
| 65 | + } |
| 66 | + |
| 67 | + //UUID를 사용하여 파일 고유 ID 생성 |
| 68 | + private String createFileId() { |
| 69 | + return UUID.randomUUID().toString(); |
| 70 | + } |
| 71 | + |
| 72 | + //파일의 전체 경로 생성 |
| 73 | + private String createPath(String prefix, String fileName) { |
| 74 | + String fileId = createFileId(); |
| 75 | + return String.format("%s/%s", prefix, fileId + "-" + fileName); |
| 76 | + } |
| 77 | + |
| 78 | + // get 용 URL 생성 |
| 79 | + private GeneratePresignedUrlRequest getGetGeneratePresignedUrlRequest(String key, Date expiration) { |
| 80 | + return new GeneratePresignedUrlRequest(bucket, key) |
| 81 | + .withMethod(HttpMethod.GET) |
| 82 | + .withExpiration(expiration); |
| 83 | + } |
| 84 | + |
| 85 | + @Transactional(readOnly = true) |
| 86 | + public GetS3UrlDto getGetS3Url(Long memberId, String key) { |
| 87 | + // url 유효기간 설정하기(1시간) |
| 88 | + Date expiration = getExpiration(); |
| 89 | + |
| 90 | + // presigned url 생성하기 |
| 91 | + GeneratePresignedUrlRequest generatePresignedUrlRequest = |
| 92 | + getGetGeneratePresignedUrlRequest(key, expiration); |
| 93 | + |
| 94 | + URL url = amazonS3.generatePresignedUrl(generatePresignedUrlRequest); |
| 95 | + |
| 96 | + // return |
| 97 | + return GetS3UrlDto.builder() |
| 98 | + .preSignedUrl(url.toExternalForm()) |
| 99 | + .key(key) |
| 100 | + .build(); |
| 101 | + } |
| 102 | + |
| 103 | + private static Date getExpiration() { |
| 104 | + Date expiration = new Date(); |
| 105 | + long expTimeMillis = expiration.getTime(); |
| 106 | + expTimeMillis += 1000 * 60 * 10; // 10분으로 설정하기 |
| 107 | + expiration.setTime(expTimeMillis); |
| 108 | + return expiration; |
| 109 | + } |
| 110 | +} |
0 commit comments