Spring Framework provides a ResourceLoader abstraction to easily read and write files from various sources, such as the filesystem, the classpath, or the web. You simply need to specify the URI to the resource using the well known protocol prefix. For example, to access a file on the local filesystem, you would specify a URI like file:/data/config.yaml.

In this exercise, you will write a Spring Boot application that will access files stored in Google Cloud Storage (GCS) using the Spring Resource abstraction and the gs: protocol prefix.

You will use Cloud Shell and the Cloud SDK gcloud command.

What you'll learn

What you'll need

How will you use use this tutorial?

Read it through only Read it and complete the exercises

How would you rate your experience with building HTML/CSS web apps?

Novice Intermediate Proficient

How would you rate your experience with using Google Cloud Platform services?

Novice Intermediate Proficient

Self-paced environment setup

If you don't already have a Google Account (Gmail or Google Apps), you must create one. Sign-in to Google Cloud Platform console (console.cloud.google.com) and create a new project:

Remember the project ID, a unique name across all Google Cloud projects (the name above has already been taken and will not work for you, sorry!). It will be referred to later in this codelab as PROJECT_ID.

Next, you'll need to enable billing in the Cloud Console in order to use Google Cloud resources.

Running through this codelab shouldn't cost you more than a few dollars, but it could be more if you decide to use more resources or if you leave them running (see "cleanup" section at the end of this document).

New users of Google Cloud Platform are eligible for a $300 free trial.

Google Cloud Shell

While Google Cloud can be operated remotely from your laptop, in this codelab we will be using Google Cloud Shell, a command line environment running in the Cloud.

Activate Google Cloud Shell

From the GCP Console click the Cloud Shell icon on the top right toolbar:

Then click "Start Cloud Shell":

It should only take a few moments to provision and connect to the environment:

This virtual machine is loaded with all the development tools you'll need. It offers a persistent 5GB home directory, and runs on the Google Cloud, greatly enhancing network performance and authentication. Much, if not all, of your work in this lab can be done with simply a browser or your Google Chromebook.

Once connected to the cloud shell, you should see that you are already authenticated and that the project is already set to your PROJECT_ID.

Run the following command in the cloud shell to confirm that you are authenticated:

gcloud auth list

Command output

Credentialed accounts:
 - <myaccount>@<mydomain>.com (active)
gcloud config list project

Command output

[core]
project = <PROJECT_ID>

If it is not, you can set it with this command:

gcloud config set project <PROJECT_ID>

Command output

Updated property [core/project].

After the Cloud Shell launches, you can start creating files and transferring them to GCS.

Let's first create a file called my-file.txt:

$ echo "Hello World from GCS" > my-file.txt

Now let's create a new unique bucket in GCS and transfer the file there using gsutil.

$ BUCKET=spring-bucket-$USER
$ gsutil makebucket gs://$BUCKET
$ gsutil copy my-file.txt gs://$BUCKET

Navigate to the Storage Browser in the GCP Console and verify that the bucket and the file are there.

Let's start writing the app by using the command line to generate a new Spring Boot application with Spring Initializr:

$ curl https://start.spring.io/starter.tgz \
-d dependencies=web,cloud-gcp-storage -d baseDir=spring-gcs | tar -xzvf -

Note that the Initializr will automatically add the spring-boot-starter-web and spring-cloud-gcp-starter-storage to our dependencies in the pom.xml of the template app.

Change to the directory of the template app:

$ cd spring-gcs

Build and run the app using Maven:

$ ./mvnw spring-boot:run

The app will start listening on port 8080. Open up a new Cloud Shell tab and run curl to access the app.

$ curl localhost:8080

You should get back a 404 response because the app doesn't do anything useful yet. Return back to the previous Cloud Shell tab where the app is running and kill it with Ctrl+C.

Now let's modify our Spring Boot app to access the my-file.txt file we previously stored in GCS. Our goal is to simply return the contents of the file via HTTP.

Add a REST controller GcsController to the app.

$ vi src/main/java/com/example/demo/GcsController.java

Paste the following code, and don't forget to fix the resource URI with the bucket that you created previously. You can check the bucket by running echo $BUCKET command.

src/main/java/com/example/demo/GcsController.java

package com.example.demo;

import java.io.IOException;
import java.nio.charset.Charset;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GcsController {

  @Value("gs://REPLACE_WITH_YOUR_BUCKET/my-file.txt")
  private Resource gcsFile;

  @RequestMapping(value = "/", method = RequestMethod.GET)
  public String readGcsFile() throws IOException {
    return StreamUtils.copyToString(
        gcsFile.getInputStream(),
        Charset.defaultCharset()) + "\n";
  }
}

Build and run the app using Maven:

$ ./mvnw spring-boot:run

The app will start listening on port 8080. Open up a new Cloud Shell tab and run curl to access the app.

$ curl localhost:8080

You should now see the contents of the file returned from the app. Go back to the previous Cloud Shell tab where the app is running and kill it with Ctrl+C.

We've read the contents of the file in GCS and exposed it through a Spring REST Controller. Now, let's change the contents of the file by posting the new file content to the same HTTP endpoint.

We need to add another method to GcsController that will respond to HTTP POST and write the data to our file in GCS. This time we will cast the Spring Resource to WritableResource.

Update the GcsController with additional imports that we'll need:

src/main/java/com/example/demo/GcsController.java

import java.io.OutputStream;
import org.springframework.core.io.WritableResource;
import org.springframework.web.bind.annotation.RequestBody;

And now, let's and the new endpoint method to the controller.

src/main/java/com/example/demo/GcsController.java

@RestController
public class GcsController {
  @RequestMapping(value = "/", method = RequestMethod.POST)
  String writeGcs(@RequestBody String data) throws IOException {
    try (OutputStream os = ((WritableResource) gcsFile).getOutputStream()) {
      os.write(data.getBytes());
    }
    return "file was updated\n";
  }
  ...
}

Build and run the app using Maven:

$ ./mvnw spring-boot:run

The application will start listening on port 8080. Open up a new Cloud Shell tab and run curl to post a message to the application.

$ 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 doing a GET.

$ curl localhost:8080

You should see the updated contents of the file returned from the app. Return back to the previous Cloud Shell tab where the app is running and kill it with Ctrl+C.

You've learned how to use the Spring Resource Abstraction to easily access files in Google Cloud Storage. You wrote a Spring Boot web app that can read and write to a file in GCS. You also learned about the Spring Boot Starter for Google Cloud Storage that enables this functionality.

Learn More

License

This work is licensed under a Creative Commons Attribution 2.0 Generic License.