如何在springboot中访问GCS桶? [英] How to access GCS bucket in springboot?
问题描述
Spring Boot 中 GCP 从 GCS 存储桶中读取文件的配置是什么?
What are the configurations for GCP to read files from the GCS bucket in spring boot?
推荐答案
您可以使用 Spring Cloud GCP Storage 组件,通过 gradle/maven 轻松添加到您的项目中.
You could use Spring Cloud GCP Storage component, easy to add to your project via gradle/maven.
您可以找到入门示例 这里 :)
You can find a starter example here :)
- 确保按照这些说明配置了 Cloud SDK.立>
- 在 Google Cloud Storage 中创建一个新存储分区.您可以使用 Cloud SDK 附带的
gsutil
命令.
- Make sure that you have the Cloud SDK configured by following these instructions.
- Create a new bucket in Google Cloud Storage.
You can use the
gsutil
command that comes with the Cloud SDK.
$ BUCKET=spring-bucket-$USER
$ gsutil makebucket gs://$BUCKET
- 将本地
my-file.txt
文件传输到存储桶.
- Transfer the local
my-file.txt
file to the bucket.
$ gsutil copy my-file.txt gs://$BUCKET
- 编辑
src/main/resources/application.properties
并将gcs-resource-test-bucket
属性设置为您在步骤中创建的存储桶的名称2. - 启动
GcsApplication
Spring Boot 应用程序.
- Edit the
src/main/resources/application.properties
and set thegcs-resource-test-bucket
property to the name of your bucket that you created in Step 2. - Start the
GcsApplication
Spring Boot app.
$ mvn spring-boot:run
在浏览器中导航到 http://localhost:8080/以打印您在第 3 步中上传的文件的内容.
Navigate to http://localhost:8080/ in your browser to print the contents of the file you uploaded in step 3.
要更新文件内容,请使用 curl
向同一端点发送 POST 请求:
To update the file contents, send a POST request to the same endpoint using curl
:
$ curl -d 'new message' -H 'Content-Type: text/plain' localhost:8080
您应该会看到文件内容已更新的确认信息.
You should see a confirmation that the contents of the file were updated.
这篇关于如何在springboot中访问GCS桶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!