느린 것을 걱정하지 말고, 멈춰서는 것을 걱정하라

작업환경 

IDE : STS(Spring Tool Suite)

Build Tool : Maven

 

나의 작업환경은 위와같이 STS+Maven이다. 현재 나는 웹 에디터에서 이미지를 올릴떄, 로컬이 아닌 구글 클라우드의 스토리지에 올리는 작업을 진행하고자 한다. 그래서 여러가지 관련 Dependency 라이브러리를 찾아본 결과 최종적으로 Spring Boot에서는 Spring-Cloud 관련 starter를 제공하고 해당 starter가 Google Cloud Platform을 지원한다는 사실을 알게되었다.

 

그래서 다음과 같이 관련 Dependency를 추가하고 실행해 보았다.

[pom.xml]

... 생략 ...........
</dependencies>
  ...... 생략 ......
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter-storage</artifactId>
  </dependency>

</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>


... 생략 ... 

 

위의 Dependency Library를 추가하고 실행해본 결과 다음과 같은 에러가 뜬다. 에러로그를 읽어보니 이유인 즉슨, Credential 파일의 부재로 인해 bean을 만들지 못하는 것으로 보인다. 그리고 이러한 credential 파일을 읽으려는 것을 보니 application.yml에 설정을 해줘야 할 것이다.

 

[Error 로그]

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'storage' defined in class path resource [org/springframework/cloud/gcp/autoconfigure/storage/GcpStorageAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.google.cloud.storage.Storage]: Factory method 'storage' threw exception; nested exception is java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
	at com.copocalypse.web.WebdemoApplication.main(WebdemoApplication.java:11) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.google.cloud.storage.Storage]: Factory method 'storage' threw exception; nested exception is java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	... 19 common frames omitted
Caused by: java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
	at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:134) ~[google-auth-library-oauth2-http-0.18.0.jar:na]
	at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:113) ~[google-auth-library-oauth2-http-0.18.0.jar:na]
	at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:85) ~[google-auth-library-oauth2-http-0.18.0.jar:na]
	at com.google.api.gax.core.GoogleCredentialsProvider.getCredentials(GoogleCredentialsProvider.java:67) ~[gax-1.49.1.jar:1.49.1]
	at org.springframework.cloud.gcp.core.DefaultCredentialsProvider.getCredentials(DefaultCredentialsProvider.java:67) ~[spring-cloud-gcp-core-1.2.1.RELEASE.jar:1.2.1.RELEASE]
	at org.springframework.cloud.gcp.autoconfigure.storage.GcpStorageAutoConfiguration.storage(GcpStorageAutoConfiguration.java:66) ~[spring-cloud-gcp-autoconfigure-1.2.1.RELEASE.jar:1.2.1.RELEASE]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_221]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_221]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_221]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
	... 20 common frames omitted

 

Todo

1. Credentail Key 받기

2019/12/29 - [IT/Google Cloud Platform] - Google Cloud Platform 사용자 키 만들기

 

Google Cloud Platform 사용자 키 만들기

현재 스프링 부트 클라우드를 이용하여 Google Cloud Platform 의 Resource에 접근하려고 하고 있다 그런데 이러한 접근을 위해서는 사용자 계정키가 필요하다는 사실을 알게되어 사용자 키를 만드는 과정에 대해..

honeyinfo7.tistory.com

 

2. application.yml 설정

spring:
  cloud:
    gcp:
      credentials:
        location: [credential-key].json

 

다음과 같이 설정 후 프로젝트를 실행하면 정상적으로 실행되는 것을 확인할 수 있다. 참고로 나같은 경우 credential-key를 resources 폴더밑에 두고 classpath:[credential-key].json 형태로 location을 명시하였다.

 

Google Cloud Platform에 접근하기 위한 첫걸음을 떼었고 이젠 이를 이용하여 이미지 업로드를 수행할 예정인데 잘 되었으면 좋겠다.

profile

느린 것을 걱정하지 말고, 멈춰서는 것을 걱정하라

@주현태

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!