-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: google cloud storage 파일 업로드 기능 구현
- Loading branch information
Showing
4 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ build/ | |
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
*.yml | ||
credentials/* | ||
|
||
### STS ### | ||
.apt_generated | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/com/j9/bestmoments/service/GoogleCloudStorageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
src/main/java/com/j9/bestmoments/service/StorageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |