Skip to content

Commit

Permalink
#18 - Add Google Cloud Storage config
Browse files Browse the repository at this point in the history
  • Loading branch information
eun61n00 committed Aug 14, 2023
1 parent d42e99d commit d2fea4d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.routemaster.api.total.infra.storage;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Configuration
@Component
@Getter
public class GoogleCloudStorageConfig {

public static String credentialsPath;
public static String projectId;
public static String bucketName;

@Value("${cloud.gcp.storage.credentials.location}")
public void setCredentialsPath(String credentialsPath) {
this.credentialsPath = credentialsPath;
}

@Value("${cloud.gcp.storage.project-id}")
public void setProjectId(String projectId) {
this.projectId = projectId;
}

@Value("${cloud.gcp.storage.bucket-name}")
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}

@Bean
public Storage storage() throws IOException {
ClassPathResource resource = new ClassPathResource(credentialsPath);
GoogleCredentials credentials = GoogleCredentials.fromStream(resource.getInputStream());
return StorageOptions.newBuilder()
.setProjectId(projectId)
.setCredentials(credentials)
.build()
.getService();
}

}
1 change: 1 addition & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ spring:
- prod/datasource.yml
- prod/tour.yml
- prod/weather.yml
- prod/gcs.yml
jackson:
# default-property-inclusion: non_null

Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/prod/gcs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cloud:
gcp:
storage:
credentials:
location: ${GCP_STORAGE_CREDENTIALS_LOCATIONS}
project-id: ${GCP_PROJECT_ID}
bucket-name: ${GCP_STORAGE_BUCKET_NAME}

0 comments on commit d2fea4d

Please sign in to comment.