问题描述
我在这里找到了两个类似的帖子,但一个没有得到答复,另一个是关于 android 的。我有一个 Spring Boot 项目,我想在我的应用程序中访问 GCP 存储文件。
在本地一切正常,我可以访问我的存储桶并在我的存储中读取和存储文件。但是当我将其上传到 gcp kubernetes 时,出现以下异常:
"java.nio.file.FileSystemNotFoundException: Provider "gs" not 安装在 java.nio.file.Paths.get(Paths.java:147) ~[na:1.8.0_212] 在 xx.xx.StorageService.saveFile(StorageService.java:64) ~[classes!/:0.3.20-SNAPSHOT]
我的代码行如下所示:
public void saveFile(multipartfile multipartfile,String path) {
String completePath = filesBasePath + path;
Path filePath = Paths.get(URI.create(completePath)); // <- exception appears here
Files.createDirectories(filePath);
multipartfile.transferTo(filePath);
}
}
completePath 可能会导致类似“gs://my-storage/path/to/file/image.jpg”的结果
我有以下依赖项:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-storage</artifactId>
<version>1.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-nio</artifactId>
<version>0.122.5</version>
</dependency>
有没有人知道在哪里看? 除了基础设施之外,唯一真正的区别是我没有明确地不在 kubernetes 上使用身份验证,因为根据文档不需要它
从 Google Cloud Platform 使用 Google Cloud 库时 Compute Engine、Kubernetes Engine 或 App Engine 等环境, 无需额外的身份验证步骤。
解决方法
gs:// 语法不通用;某些 Google 工具和服务(例如 gsutil)支持它,但许多网址库都不知道它。
您可以更改为对 GCS API 之一使用 HTTP 语法,例如,
https://my-storage.storage.googlapis.com/path/to/file/image.jpg
请注意,上述 HTTP URL 将需要身份验证,因此您可能希望使用 google-cloud-java 之类的库来支持发出经过身份验证的 GCS 请求。
,这里的常规 Spring boot 包装似乎没有以所需的方式包装依赖项。通常你会看到如下内容:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.5</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
但是,要访问“gs”提供程序,它需要位于“lib/”文件夹中。您可以通过复制依赖项然后创建 JAR 来手动打包它(这基于 springboot-helloworld 示例:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>
com.example.appengine.springboot.SpringbootApplication
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
原来是posted on GitHub。