-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from lotteon2/fix/presigned-url
Fix/presigned url
- Loading branch information
Showing
5 changed files
with
78 additions
and
14 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
src/main/java/kr/bb/product/domain/presigendurl/PresignedUrlData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/kr/bb/product/domain/presigendurl/PresignedUrlService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...main/java/kr/bb/product/domain/product/infrastructure/http/api/ProductRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters