Skip to content

Commit

Permalink
perf(#111): 조회하려는 file이 null일 경우 null 반환
Browse files Browse the repository at this point in the history
- 존재하지 않는 파일에 대해서도 presigned url이 생성되는 것을 방지하기 위해 파일이 존재하지 않으면 null을 반환하도록 변경했습니다.
  • Loading branch information
cabbage16 committed Aug 12, 2024
1 parent 6ea76d6 commit 595987d
Showing 1 changed file with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import com.amazonaws.SdkClientException;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.Headers;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.*;
import com.bamdoliro.maru.infrastructure.s3.dto.response.UploadResponse;
import com.bamdoliro.maru.infrastructure.s3.exception.FailedToSaveException;
import com.bamdoliro.maru.infrastructure.s3.validator.FileValidator;
Expand Down Expand Up @@ -66,7 +63,7 @@ public UploadResponse getDownloadPresignedUrl(String folder, String fileName) {
GeneratePresignedUrlRequest request = getGenerateDownloadPresignedUrlRequest(bucket, fullFileName);

return new UploadResponse(
amazonS3Client.generatePresignedUrl(request).toString()
request != null ? amazonS3Client.generatePresignedUrl(request).toString() : null
);
}

Expand All @@ -77,10 +74,18 @@ private GeneratePresignedUrlRequest getGenerateUploadPresignedUrlRequest(String
}

private GeneratePresignedUrlRequest getGenerateDownloadPresignedUrlRequest(String bucket, String fileName) {

return new GeneratePresignedUrlRequest(bucket, fileName)
.withMethod(HttpMethod.GET)
.withExpiration(getPresignedUrlExpiration(60 * 10));
try {
amazonS3Client.getObjectMetadata(bucket, fileName);

return new GeneratePresignedUrlRequest(bucket, fileName)
.withMethod(HttpMethod.GET)
.withExpiration(getPresignedUrlExpiration(60 * 10));
} catch (AmazonS3Exception e) {
if (e.getStatusCode() == 404) {
return null;
}
throw e;
}
}

private Date getPresignedUrlExpiration(int duration) {
Expand Down

0 comments on commit 595987d

Please sign in to comment.