-
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.
Browse files
Browse the repository at this point in the history
…40) �GCS 설정
- Loading branch information
Showing
7 changed files
with
117 additions
and
2 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
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
32 changes: 32 additions & 0 deletions
32
src/main/java/com/nainga/nainga/domain/gcsguide/GcsConfig.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,32 @@ | ||
package com.nainga.nainga.domain.gcsguide; | ||
|
||
import com.google.auth.oauth2.GoogleCredentials; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.util.ResourceUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
@Configuration | ||
public class GcsConfig { | ||
|
||
@Bean | ||
public Storage storage() { | ||
String keyFileName = "backend-submodule/kcs-dev-gcs.json"; | ||
Storage storage; | ||
try { | ||
InputStream keyFile = ResourceUtils.getURL("classpath:" + keyFileName).openStream(); | ||
storage = StorageOptions.newBuilder() | ||
.setCredentials(GoogleCredentials.fromStream(keyFile)) | ||
.build() | ||
.getService(); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("GCS 에러"); | ||
} | ||
|
||
return storage; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/nainga/nainga/domain/gcsguide/GcsController.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,26 @@ | ||
package com.nainga.nainga.domain.gcsguide; | ||
|
||
import com.nainga.nainga.global.util.Result; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class GcsController { | ||
|
||
private final GcsService gcsService; | ||
|
||
@Tag(name = "GCS") | ||
@Operation(summary = "GCS 접속", description = "GCS 에 이미지 저장!") | ||
@PostMapping(value = "api/v1/gcs", | ||
consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) | ||
public Result<String> saveImageToGCS(@ModelAttribute GcsResponse response) { | ||
String url = gcsService.uploadImage(response); | ||
return new Result<>(Result.CODE_SUCCESS, Result.MESSAGE_OK, url); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/nainga/nainga/domain/gcsguide/GcsResponse.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,9 @@ | ||
package com.nainga.nainga.domain.gcsguide; | ||
|
||
import lombok.Data; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
@Data | ||
public class GcsResponse { | ||
private MultipartFile image; | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/nainga/nainga/domain/gcsguide/GcsService.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,43 @@ | ||
package com.nainga.nainga.domain.gcsguide; | ||
|
||
|
||
import com.google.cloud.storage.Blob; | ||
import com.google.cloud.storage.BlobInfo; | ||
import com.google.cloud.storage.Storage; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class GcsService { | ||
|
||
private final Storage storage; | ||
|
||
public String uploadImage(GcsResponse response) { | ||
|
||
// 이미지 업로드 | ||
String bucketName = "kcs-dev-bucket"; | ||
String uuid = UUID.randomUUID().toString(); // Google Cloud Storage에 저장될 파일 이름(중복 이름 안되게 저장하도록 주의) | ||
String ext = response.getImage().getContentType(); // 파일의 형식 ex) JPG | ||
|
||
// Cloud에 이미지 업로드 | ||
// 이미지 접근 url : https://storage.googleapis.com/버킷이름/UUID값 | ||
try { | ||
BlobInfo blobInfo = BlobInfo.newBuilder(bucketName, uuid) | ||
.setContentType(ext) | ||
.build(); | ||
|
||
// Cloud 에 업로드 | ||
Blob blob = storage.create(blobInfo, response.getImage().getBytes()); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("GCS 에러"); | ||
} | ||
|
||
return "https://storage.googleapis.com/" + bucketName + "/" + uuid; | ||
} | ||
} |
Submodule backend-submodule
updated
from 59e0f3 to 6de3c1