Skip to content

Commit

Permalink
fix: aws presigned url 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
nowgnas committed Jan 19, 2024
1 parent 585a10c commit 86787c5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package kr.bb.product.domain.presigendurl;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class PresignedUrlData {
private String presignedUrl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package kr.bb.product.domain.presigendurl;

import com.amazonaws.HttpMethod;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.Headers;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import java.util.Date;
import java.util.UUID;
import org.springframework.stereotype.Component;

@Component
public class PresignedUrlService {
public static PresignedUrlData getPresignedUrl(
String prefix, String fileName, AmazonS3 amazonS3, String bucket) {
String onlyOneFileName = onlyOneFileName(fileName);

GeneratePresignedUrlRequest generatePresignedUrlRequest =
getGeneratePreSignedUrlRequest(bucket, prefix + "/" + onlyOneFileName);
return PresignedUrlData.builder()
.presignedUrl(amazonS3.generatePresignedUrl(generatePresignedUrlRequest).toString())
.build();
}

private static GeneratePresignedUrlRequest getGeneratePreSignedUrlRequest(
String bucket, String fileName) {
GeneratePresignedUrlRequest generatePresignedUrlRequest =
new GeneratePresignedUrlRequest(bucket, fileName)
.withMethod(HttpMethod.PUT)
.withExpiration(getPreSignedUrlExpiration());
generatePresignedUrlRequest.addRequestParameter(
Headers.S3_CANNED_ACL, CannedAccessControlList.PublicRead.toString());
return generatePresignedUrlRequest;
}

private static Date getPreSignedUrlExpiration() {
Date expiration = new Date();
long expTimeMillis = expiration.getTime();
expTimeMillis += 1000 * 60 * 5;
expiration.setTime(expTimeMillis);
return expiration;
}

private static String onlyOneFileName(String fileName) {
return UUID.randomUUID().toString() + fileName;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package kr.bb.product.domain.product.application.port.in;

import bloomingblooms.domain.aws.PresignedUrlData;
import bloomingblooms.domain.aws.PresignedUrlService;
import bloomingblooms.domain.flower.StockChangeDto;
import bloomingblooms.domain.order.ProcessOrderDto;
import bloomingblooms.domain.product.IsProductPriceValid;
Expand All @@ -24,6 +22,8 @@
import kr.bb.product.domain.flower.application.port.out.FlowerQueryOutPort;
import kr.bb.product.domain.flower.entity.Flower;
import kr.bb.product.domain.flower.mapper.FlowerCommand;
import kr.bb.product.domain.presigendurl.PresignedUrlData;
import kr.bb.product.domain.presigendurl.PresignedUrlService;
import kr.bb.product.domain.product.application.port.out.ProductQueryOutPort;
import kr.bb.product.domain.product.application.usecase.ProductQueryUseCase;
import kr.bb.product.domain.product.entity.Product;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package kr.bb.product.domain.product.application.usecase;

import bloomingblooms.domain.aws.PresignedUrlData;
import bloomingblooms.domain.flower.StockChangeDto;
import bloomingblooms.domain.order.ProcessOrderDto;
import bloomingblooms.domain.product.IsProductPriceValid;
Expand All @@ -13,6 +12,7 @@
import bloomingblooms.domain.wishlist.likes.LikedProductInfoResponse;
import java.util.List;
import java.util.Map;
import kr.bb.product.domain.presigendurl.PresignedUrlData;
import kr.bb.product.domain.product.entity.ProductSaleStatus;
import kr.bb.product.domain.product.mapper.ProductCommand;
import kr.bb.product.domain.product.mapper.ProductCommand.ProductList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package kr.bb.product.domain.product.infrastructure.http.api;

import bloomingblooms.domain.aws.PresignedUrlData;
import bloomingblooms.response.CommonResponse;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
import kr.bb.product.domain.presigendurl.PresignedUrlData;
import kr.bb.product.domain.product.application.usecase.ProductCommandUseCase;
import kr.bb.product.domain.product.application.usecase.ProductQueryUseCase;
import kr.bb.product.domain.product.entity.ProductSaleStatus;
Expand Down

0 comments on commit 86787c5

Please sign in to comment.