Java 如何在springboot中访问GCS存储桶?
GCP在spring boot中从GCS存储桶读取文件的配置是什么?您可以使用spring Cloud GCP存储组件,通过gradle/maven很容易添加到您的项目中 您可以找到一个初学者示例:) 运行示例Java 如何在springboot中访问GCS存储桶?,java,spring-boot,google-cloud-platform,google-cloud-storage,Java,Spring Boot,Google Cloud Platform,Google Cloud Storage,GCP在spring boot中从GCS存储桶读取文件的配置是什么?您可以使用spring Cloud GCP存储组件,通过gradle/maven很容易添加到您的项目中 您可以找到一个初学者示例:) 运行示例 确保您通过以下方式配置了云SDK 在谷歌云存储中创建一个新bucket。 您可以使用CloudSDK附带的gsutil命令 将本地my file.txt文件传输到存储桶 编辑src/main/resources/application.properties,并将gcs资源测试bucket
gsutil
命令my file.txt
文件传输到存储桶src/main/resources/application.properties
,并将gcs资源测试bucket
属性设置为您在步骤2中创建的bucket的名称GcsApplication
Spring启动应用程序curl
向同一端点发送POST请求:
您应该会看到文件内容已更新的确认信息。您可以自动连接存储类以使用bucket(CRUD操作) 需要在application.properties中定义以下属性
- spring.cloud.gcp.project-id
- spring.cloud.gcp.credentials.location
- 存储桶名称的属性
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-dependencies</artifactId>
<version>1.2.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-storage</artifactId>
</dependency>
org.springframework.cloud
你能补充更多细节吗?您可以在属性文件中将GCS路径设置为gs://然后您的代码可以连接到GCS。@pradeep示例:我想在我的属性文件中添加路径。您能提供有关您的用例的更多详细信息吗?如果你遇到了一些错误等,你可以发布一个代码片段并在你的问题中添加更多信息,让我们知道你到目前为止尝试了什么。提供更多信息将帮助社区给你更好的答案。你说的财产档案是什么意思?
$ gsutil copy my-file.txt gs://$BUCKET
$ mvn spring-boot:run
$ curl -d 'new message' -H 'Content-Type: text/plain' localhost:8080
@Autowired
private Storage storage;
public void store(InputStream OBJECT_TO_BE_STORED){
Blob blob = storage.get(BUCKET_NAME).create(OBJECT_NAME, OBJECT_TO_BE_STORED);
}
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-dependencies</artifactId>
<version>1.2.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gcp-starter-storage</artifactId>
</dependency>