From deb483978295aa1ba82cc15ebe6c1a16ebff8988 Mon Sep 17 00:00:00 2001 From: SJ70 Date: Wed, 3 Jul 2024 18:25:05 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20google=20cloud=20storage=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EC=97=85=EB=A1=9C=EB=93=9C=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + build.gradle | 2 + .../service/GoogleCloudStorageService.java | 46 +++++++++++++++++++ .../bestmoments/service/StorageService.java | 10 ++++ 4 files changed, 59 insertions(+) create mode 100644 src/main/java/com/j9/bestmoments/service/GoogleCloudStorageService.java create mode 100644 src/main/java/com/j9/bestmoments/service/StorageService.java diff --git a/.gitignore b/.gitignore index 3b33c53..c4cfcfc 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ build/ !**/src/main/**/build/ !**/src/test/**/build/ *.yml +credentials/* ### STS ### .apt_generated diff --git a/build.gradle b/build.gradle index 532a0ff..235b23b 100644 --- a/build.gradle +++ b/build.gradle @@ -32,6 +32,8 @@ dependencies { implementation 'io.jsonwebtoken:jjwt-api:0.11.5' implementation 'io.jsonwebtoken:jjwt-impl:0.11.5' implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5' + implementation 'com.google.auth:google-auth-library-oauth2-http:1.4.0' + implementation 'com.google.cloud:google-cloud-storage:2.1.5' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.h2database:h2' annotationProcessor 'org.projectlombok:lombok' diff --git a/src/main/java/com/j9/bestmoments/service/GoogleCloudStorageService.java b/src/main/java/com/j9/bestmoments/service/GoogleCloudStorageService.java new file mode 100644 index 0000000..f1a1554 --- /dev/null +++ b/src/main/java/com/j9/bestmoments/service/GoogleCloudStorageService.java @@ -0,0 +1,46 @@ +package com.j9.bestmoments.service; + +import com.google.auth.oauth2.GoogleCredentials; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.UUID; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import java.io.IOException; +import com.google.cloud.storage.BlobId; +import com.google.cloud.storage.BlobInfo; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import org.springframework.web.multipart.MultipartFile; + +@Service +@Slf4j +public class GoogleCloudStorageService implements StorageService { + + private final String bucketName; + private final Storage storage; + + public GoogleCloudStorageService( + @Value("${storage.google-cloud.bucket.name}") String bucketName, + @Value("${storage.google-cloud.project.id}") String projectId + ) throws IOException { + this.bucketName = bucketName; + this.storage = StorageOptions.newBuilder() + .setProjectId(projectId) + .setCredentials(GoogleCredentials.fromStream(Files.newInputStream(Paths.get("credentials/bestmoments-file-uploader.json")))) + .build() + .getService(); + } + + // 다운로드 링크를 반환 + @Override + public String uploadFile(MultipartFile file) throws IOException { + String fileName = String.format("%s.%s", UUID.randomUUID(), file.getContentType().split("/")[1]); + BlobId blobId = BlobId.of(bucketName, fileName); + BlobInfo blobInfo = BlobInfo.newBuilder(blobId).build(); + storage.create(blobInfo, file.getInputStream()); + log.info("blobId = {}", blobId); + return String.format("https://storage.cloud.google.com/%s/%s", bucketName, fileName); + } +} diff --git a/src/main/java/com/j9/bestmoments/service/StorageService.java b/src/main/java/com/j9/bestmoments/service/StorageService.java new file mode 100644 index 0000000..f0c656f --- /dev/null +++ b/src/main/java/com/j9/bestmoments/service/StorageService.java @@ -0,0 +1,10 @@ +package com.j9.bestmoments.service; + +import java.io.IOException; +import org.springframework.web.multipart.MultipartFile; + +public interface StorageService { + + String uploadFile(MultipartFile file) throws IOException; + +}