如何在springboot中访问GCS桶? [英] How to access GCS bucket in springboot?

查看:41
本文介绍了如何在springboot中访问GCS桶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 :)

  1. 确保按照这些说明配置了 Cloud SDK.立>
  2. 在 Google Cloud Storage 中创建一个新存储分区.您可以使用 Cloud SDK 附带的 gsutil 命令.
  1. Make sure that you have the Cloud SDK configured by following these instructions.
  2. 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

  1. 将本地 my-file.txt 文件传输到存储桶.
  1. Transfer the local my-file.txt file to the bucket.

$ gsutil copy my-file.txt gs://$BUCKET

  1. 编辑 src/main/resources/application.properties 并将 gcs-resource-test-bucket 属性设置为您在步骤中创建的存储桶的名称2.
  2. 启动 GcsApplication Spring Boot 应用程序.
  1. Edit the src/main/resources/application.properties and set the gcs-resource-test-bucket property to the name of your bucket that you created in Step 2.
  2. Start the GcsApplication Spring Boot app.

$ mvn spring-boot:run

  1. 在浏览器中导航到 http://localhost:8080/以打印您在第 3 步中上传的文件的内容.

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆