From e84407a26fb994d75c1d363784281cc4c7cf68fb Mon Sep 17 00:00:00 2001 From: oxdjww Date: Thu, 4 Jul 2024 18:47:29 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20S3=20=EC=84=9C=EB=B9=84=EC=8A=A4=20?= =?UTF-8?q?=EB=A0=88=EC=9D=B4=EC=96=B4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hackathon/TimeLapse/s3/S3Controller.java | 33 ++++++++++ .../com/hackathon/TimeLapse/s3/S3Service.java | 63 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/main/java/com/hackathon/TimeLapse/s3/S3Controller.java create mode 100644 src/main/java/com/hackathon/TimeLapse/s3/S3Service.java diff --git a/src/main/java/com/hackathon/TimeLapse/s3/S3Controller.java b/src/main/java/com/hackathon/TimeLapse/s3/S3Controller.java new file mode 100644 index 0000000..499e700 --- /dev/null +++ b/src/main/java/com/hackathon/TimeLapse/s3/S3Controller.java @@ -0,0 +1,33 @@ +package com.hackathon.TimeLapse.s3; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import lombok.RequiredArgsConstructor; + +@RestController +@RequestMapping("/api/files") +@RequiredArgsConstructor +public class S3Controller { + + private final S3Service s3Service; + + @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) { + return s3Service.uploadFile(file); + } + + @GetMapping("/url") + public String getFileUrl(@RequestParam("fileName") String fileName) { + return s3Service.getFileUrl(fileName); + } + + @DeleteMapping("/delete") + public ResponseEntity deleteFile(@RequestParam("fileName") String fileName) { + s3Service.deleteFile(fileName); + return ResponseEntity.ok("File deleted successfully"); + } +} diff --git a/src/main/java/com/hackathon/TimeLapse/s3/S3Service.java b/src/main/java/com/hackathon/TimeLapse/s3/S3Service.java new file mode 100644 index 0000000..d85380f --- /dev/null +++ b/src/main/java/com/hackathon/TimeLapse/s3/S3Service.java @@ -0,0 +1,63 @@ +package com.hackathon.TimeLapse.s3; + +import java.io.IOException; +import java.net.URL; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.Date; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.multipart.MultipartFile; + +import com.amazonaws.HttpMethod; +import com.amazonaws.services.s3.AmazonS3Client; +import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; +import com.amazonaws.services.s3.model.ObjectMetadata; + +import lombok.RequiredArgsConstructor; + +@Service +@RequiredArgsConstructor +public class S3Service { + + private final AmazonS3Client amazonS3Client; + + @Value("${cloud.aws.s3.bucket}") + private String bucket; + + public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) { + try { + String fileName = file.getOriginalFilename(); + String fileUrl = "https://" + bucket + "/test" + fileName; + ObjectMetadata metadata = new ObjectMetadata(); + metadata.setContentType(file.getContentType()); + metadata.setContentLength(file.getSize()); + amazonS3Client.putObject(bucket, fileName, file.getInputStream(), metadata); + return ResponseEntity.ok(fileUrl); + } catch (IOException e) { + e.printStackTrace(); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); + } + } + + public void deleteFile(String fileName) { + amazonS3Client.deleteObject(bucket, fileName); + } + + public String getFileUrl(String fileName) { + LocalDateTime expiration = LocalDateTime.now().plusHours(1); + Date expirationDate = Date.from(expiration.atZone(ZoneOffset.systemDefault()).toInstant()); + + GeneratePresignedUrlRequest generatePresignedUrlRequest = + new GeneratePresignedUrlRequest(bucket, fileName) + .withMethod(HttpMethod.GET) + .withExpiration(expirationDate); + + URL url = amazonS3Client.generatePresignedUrl(generatePresignedUrlRequest); + return url.toString(); + } +} From 9c5e7cee2648a6290ddb8bfcd3679384dc9e6de0 Mon Sep 17 00:00:00 2001 From: oxdjww Date: Thu, 4 Jul 2024 18:48:27 +0900 Subject: [PATCH 2/2] =?UTF-8?q?chore:=20=EA=B9=83=20=EC=BA=90=EC=8B=9C=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application-local.yml | 25 ------------------------ 1 file changed, 25 deletions(-) delete mode 100644 src/main/resources/application-local.yml diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml deleted file mode 100644 index 693677c..0000000 --- a/src/main/resources/application-local.yml +++ /dev/null @@ -1,25 +0,0 @@ -spring: - datasource: - driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://localhost:3306/TimeLapseDB - username: root - password: root - thymeleaf: - cache: false - - jpa: - database-platform: org.hibernate.dialect.MySQLDialect - open-in-view: false - show-sql: true - hibernate: - ddl-auto: create-drop - -cloud: - aws: - s3: - bucket: NONE - stack.auto: false - region.static: NONE - credentials: - accessKey: NONE - secretKey: NONE