Skip to content

Commit

Permalink
feat(#111): Download presigned-url 생성
Browse files Browse the repository at this point in the history
- 파일을 조회하기 위한 presigned-url을 생성하는 기능을 만들었어요.
  • Loading branch information
cabbage16 committed Aug 1, 2024
1 parent a0a735b commit 24f3df3
Showing 1 changed file with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,41 @@ public UploadResponse execute(MultipartFile file, String folder, String fileName
);
}

public UploadResponse getPresignedUrl(String folder, String fileName) {
public UploadResponse getUploadPresignedUrl(String folder, String fileName) {
String fullFileName = createFileName(folder, fileName);
GeneratePresignedUrlRequest request = getGeneratePresignedUrlRequest(bucket, fullFileName);
GeneratePresignedUrlRequest request = getGenerateUploadPresignedUrlRequest(bucket, fullFileName);

return new UploadResponse(
amazonS3Client.generatePresignedUrl(request).toString()
);
}

private GeneratePresignedUrlRequest getGeneratePresignedUrlRequest(String bucket, String fileName) {
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket, fileName)
.withMethod(HttpMethod.PUT)
.withExpiration(getPresignedUrlExpiration());
request.addRequestParameter(
Headers.S3_CANNED_ACL,
CannedAccessControlList.PublicRead.toString()
public UploadResponse getDownloadPresignedUrl(String folder, String fileName) {
String fullFileName = createFileName(folder, fileName);
GeneratePresignedUrlRequest request = getGenerateDownloadPresignedUrlRequest(bucket, fullFileName);

return new UploadResponse(
amazonS3Client.generatePresignedUrl(request).toString()
);
}

private GeneratePresignedUrlRequest getGenerateUploadPresignedUrlRequest(String bucket, String fileName) {
return new GeneratePresignedUrlRequest(bucket, fileName)
.withMethod(HttpMethod.PUT)
.withExpiration(getPresignedUrlExpiration(1));
}

private GeneratePresignedUrlRequest getGenerateDownloadPresignedUrlRequest(String bucket, String fileName) {

return request;
return new GeneratePresignedUrlRequest(bucket, fileName)
.withMethod(HttpMethod.GET)
.withExpiration(getPresignedUrlExpiration(60 * 10));
}

private Date getPresignedUrlExpiration() {
private Date getPresignedUrlExpiration(int duration) {
Date expiration = new Date();
long expTimeMillis = expiration.getTime();
expTimeMillis += 1000 * 60 * 60;
expTimeMillis += 1000L * 60 * duration;
expiration.setTime(expTimeMillis);

return expiration;
Expand Down

0 comments on commit 24f3df3

Please sign in to comment.