![](/img/trans.png)
[英]Unable to access Google Cloud Storage service account key file in Cloud
[英]Docker image unable to add my JSON credential key to my Google Cloud Natural Language service client
我在Eclipse中創建了一個Spring Boot Java應用程序,該應用程序連接到Google Cloud Natural Language服務以為我分析文本。 我為其創建了一個Docker映像,它無法運行,但對我來說它可以在Eclipse中運行。
我這樣讀入JSON密鑰:
InputStream inputStream = GoogleNlpApplication.class.getClassLoader().getSystemResourceAsStream("AvayaNlp-e422dbca92e5.json");
credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(inputStream));
我的Spotify Docker插件的pom文件:
<profiles>
<profile>
<id>docker-build</id>
<activation>
<property>
<name>build</name>
<value>docker</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.0</version>
<configuration>
<imageName>googlenlp2vi</imageName>
<baseImage>java</baseImage>
<entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
<!-- copy the service's jar file from target into the root directory
of the image -->
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
<executions>
<execution>
<id>default</id>
<!-- phase>install</phase -->
<phase>package</phase>
<goals>
<goal>build</goal>
<!-- goal>push</goal -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
<finalName>googlenlp2vi</finalName>
</build>
</profile>
</profiles>
然后在Docker工具箱中運行:1.mvn clean install 2.mvn clean package -Dbuild = docker
它在Eclipse上運行良好,Docker映像是否未使用Java文件的導入?
我結束了在資源中創建一個新目錄:
src
|
main
|
resources
|
test
|
AvayaNlp-e422dbca92e5.json
然后使用:
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources = resolver.getResources("classpath*:test/*.json");
InputStream inputStream = null;
for (Resource r : resources) {
inputStream = r.getInputStream();
}
credentialsProvider = FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(inputStream));
現在,泊塢窗圖像現在可以訪問jar中的json密鑰,但是如果資源中的文件夾中有多個JSON圖像,它將獲取所有JSON。
您需要在運行此映像時在JAR期望找到該文件的位置提供AvayaNlp-e422dbca92e5.json
可用。
我對AvayaNlp-e422dbca92e5.json
docker-maven-plugin
不夠熟悉,無法為您提供精確的指導,但是,假設AvayaNlp-e422dbca92e5.json
位於${PWD}/secrets
並且您希望將其安裝到容器映像中的/secrets/
中, docker run
命令將采用以下形式:
docker run ... --volume=${PWD}/secrets:/secrets ... [[your-image]]
您如何運行容器以生成錯誤?
Maven創建后是否運行您的容器映像?
HTH!
在Dockerfile中,將json文件添加到適當的位置。 使用ADD
dockerfile命令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.