From a564c9cbedbdf9394b83c95aac6007ef76827c91 Mon Sep 17 00:00:00 2001 From: jinno Yun Date: Fri, 20 Sep 2024 00:59:02 +0900 Subject: [PATCH] =?UTF-8?q?#21=20feat:=20S3StorageService=20uploadToServic?= =?UTF-8?q?e=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=B6=94=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../s3Storage/service/S3StorageService.java | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/web3-credential-server/src/main/java/web3/s3Storage/service/S3StorageService.java b/web3-credential-server/src/main/java/web3/s3Storage/service/S3StorageService.java index b17fc20..63c7c11 100644 --- a/web3-credential-server/src/main/java/web3/s3Storage/service/S3StorageService.java +++ b/web3-credential-server/src/main/java/web3/s3Storage/service/S3StorageService.java @@ -42,12 +42,8 @@ public String uploadPdf(MultipartFile file, Wallet wallet,String pdfInfo,String //첫 등록일때 => 생성해줘야함 if (wallet.getPdfUrl() == null){ - log.info("file = {}", file); - fileName = (file.getSize() > 0) ? getFileName(file, wallet): getEmptyFilename(wallet); - log.info("fileName = {}", fileName); - // PDF 파일 확장자 검증 //validatePdfFile(fileName); result = (file.getSize() > 0) ? file.getBytes():createEmptyPdf(); @@ -57,16 +53,12 @@ public String uploadPdf(MultipartFile file, Wallet wallet,String pdfInfo,String //이미 있을시 -> pdf 병합 String destination = wallet.getPdfUrl(); fileName = extractKeyFromUrl(destination); - log.info("destination = {}", destination); - log.info("fileName = {}", fileName); byte[] first = getPdf(destination).readAllBytes();//원래 파일 byte[] second = (file.getSize() > 0) ? file.getBytes() : createEmptyPdf(); //뒤에 들어온 파일 nowPage = getPdfPageCount(first)+1; result = mergePdfs(first, second); - log.info("merge Success"); - metadata= getPdfMetadata(fileName); } @@ -78,14 +70,7 @@ public String uploadPdf(MultipartFile file, Wallet wallet,String pdfInfo,String try { // S3에 PDF 파일 업로드 - PutObjectRequest putRequest = PutObjectRequest.builder() - .bucket(s3Properties.getS3BucketName()) - .key(fileName) - .contentType(file.getContentType()) - .metadata(metadata) - .build(); - - s3Client.putObject(putRequest, RequestBody.fromBytes(result)); + uploadToS3(file, fileName, metadata, result); } catch (S3Exception e) { throw new IOException("Failed to upload pdf to S3: " + e.getMessage()); @@ -97,6 +82,17 @@ public String uploadPdf(MultipartFile file, Wallet wallet,String pdfInfo,String return getpdfUrl(fileName); } + private void uploadToS3(MultipartFile file, String fileName, HashMap metadata, byte[] result) { + PutObjectRequest putRequest = PutObjectRequest.builder() + .bucket(s3Properties.getS3BucketName()) + .key(fileName) + .contentType(file.getContentType()) + .metadata(metadata) + .build(); + + s3Client.putObject(putRequest, RequestBody.fromBytes(result)); + } + private static String getFileName(MultipartFile file, Wallet wallet) { return wallet.getAddress() + "_" + System.currentTimeMillis() + "_" + wallet.getAddress(); } @@ -161,14 +157,7 @@ public String replacePdfPage(Wallet wallet, int pageNumberToRemove, MultipartFil // 최종 PDF를 S3에 업로드 try { - PutObjectRequest putRequest = PutObjectRequest.builder() - .bucket(s3Properties.getS3BucketName()) - .key(fileName) - .contentType(newPdfFile.getContentType()) - .metadata(metadata) - .build(); - s3Client.putObject(putRequest, RequestBody.fromBytes(finalPdfBytes)); - + uploadToS3(newPdfFile, fileName, metadata, finalPdfBytes); } catch (S3Exception e) { throw new IOException("Failed to upload pdf to S3: " + e.getMessage()); } finally {