Skip to content

Commit

Permalink
feat: google cloud storage 파일 업로드 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
SJ70 committed Jul 3, 2024
1 parent b371347 commit deb4839
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ build/
!**/src/main/**/build/
!**/src/test/**/build/
*.yml
credentials/*

### STS ###
.apt_generated
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/j9/bestmoments/service/StorageService.java
Original file line number Diff line number Diff line change
@@ -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;

}

0 comments on commit deb4839

Please sign in to comment.