This code sample demonstrates how to read and write files with the Spring Resource abstraction for Google Cloud Storage using the Spring Cloud GCP Storage Starter.
This content is also available in the Reading and Writing Files with Spring Resource Abstraction codelab.
-
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
-
Transfer the local
my-file.txt
file to the bucket.$ gsutil copy my-file.txt gs://$BUCKET
-
Run
$ mvn clean install
from the root directory of the project. -
In the same directory as this sample (spring-cloud-gcp-samples/spring-cloud-gcp-storage-resource-sample), 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
-
Navigate to http://localhost:8080/ in your browser to print the contents of the file you uploaded in 3. You can also use the
Web Preview
button in Cloud Shell to preview the app on port 8080. -
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.
However, let’s verify by going to http://localhost:8080/ or the
Web Preview
URL.