Spring Framework provides a ResourceLoader
abstraction to easily read and write files from various sources, such as the file system, classpath, or 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 file system, you would specify a URI like file:/data/config.yaml
.
You'll write a Spring Boot app that will access files stored in Cloud Storage by using the Spring Resource abstraction and the gs:
protocol prefix.
You'll do that by using Cloud Shell and the Cloud SDK gcloud command-line tool.
What you'll learn
- How to use the Cloud Storage Spring Boot starter
- How to access files in Cloud Storage with Spring
- How to use Spring's
Resource
andWritableResource
abstractions
What you'll need
- A Google Cloud project
- A browser, such Google Chrome
- Familiarity with standard Linux text editors, such as Vim, Emacs, and GNU Nano
How will you use the codelab?
How would you rate your experience with building HTML and CSS web apps?
How would you rate your experience with using Google Cloud services?
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.
Cloud Shell
You'll use Cloud Shell, a command-line environment running in Google 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 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 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 Cloud Shell launches, you can start creating files and transferring them to Cloud Storage.
Create a file named my-file.txt
:
$ echo "Hello World from GCS" > my-file.txt
Then create a new unique bucket in Cloud Storage 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 Cloud Storage, and verify that the bucket and the file are there.
Start writing the app by using the command line to generate a new Spring Boot app 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 your 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 a new Cloud Shell tab and run curl
to access the app.
$ curl localhost:8080
You should get a 404 response because the app doesn't do anything useful yet. Return to the previous Cloud Shell tab where the app is running and kill it with Control+C
(Command+C
on Macintosh).
Modify your Spring Boot app to access my-file.txt
, the file that you previously stored in Cloud Storage. Your goal is to simply return the contents of the file via HTTP.
In the following instructions, you'll use Vim to edit the files, but you can also use Emacs, GNU Nano, or the built-in code editor in Cloud Shell:
$ cd ~/spring-gcs
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 with Maven:
$ ./mvnw spring-boot:run
The app starts listening on port 8080. Open a new Cloud Shell tab and run curl
to access the app.
$ curl localhost:8080
You should now see that the contents of the file returned from the app. Go to the previous Cloud Shell tab where the app is running and kill it with Control+C
(Command+C
on Macintosh).
You read the contents of the file in Cloud Storage and exposed it through a Spring REST controller. Now, change the contents of the file by posting the new file content to the same HTTP endpoint.
You need to add another method to GcsController
that will respond to HTTP POST and write the data to your file in Cloud Storage. This time, cast the Spring Resource
to WritableResource
.
Update the GcsController
with additional imports that you 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;
Add 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 with Maven:
$ ./mvnw spring-boot:run
The app starts listening on port 8080. Open up a new Cloud Shell tab and run curl
to post a message to the app.
$ 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, verify that by doing a GET
.
$ curl localhost:8080
You should see the updated contents of the file returned from the app. Return to the previous Cloud Shell tab where the app is running and kill it with Control+C
(Command+C
on Macintosh).
You learned to use the Spring Resource abstraction to easily access files in Cloud Storage. You wrote a Spring Boot web app that can read and write to a file in Cloud Storage. You also learned about the Spring Boot starter for Cloud Storage that enables that functionality.
Learn More
- Cloud Storage
- Spring Cloud Google Cloud Project
- Spring on Google Cloud GitHub repository
- Java on Google Cloud
License
This work is licensed under a Creative Commons Attribution 2.0 Generic License.