Spring Cloud GCP
The Spring framework! It’s not just popular, it’s the ultimate go-to framework for developers all around the world. Since its inception, Spring has been revolutionizing the Java landscape with its versatility and reliability. And now, enter Spring Cloud GCP, the result of an extraordinary collaboration between Spring and Google Cloud Platform.
This project seamlessly integrates the power of Spring with the cutting-edge features of the Google Cloud Platform. Spring Cloud GCP empowers developers to harness the immense potential of Google Cloud Platform, unleashing a whole new realm of possibilities for building robust and scalable applications.
Spring Cloud GCP comprises of multiple libraries that enable seamless integration between Springboot applications and various GCP services. With Spring Cloud GCP, developers gain direct access to a wide range of services offered by the Google Cloud Platform. Note that while Spring Cloud GCP offers support for a vast majority of services, not all services are currently included. The project continues to evolve, expanding the list of supported services and enhancing the overall functionality.
You can find comprehensive information about Spring Cloud GCP on its official website, accessible through this link. This website offers precise details regarding the features and services provided by Spring Cloud GCP, along with relevant documentation to assist you in getting started. While the page covers a wide array of services, our focus for this article will be on Google Cloud Storage, one of the services offered by Spring Cloud GCP.
Google Cloud Storage
Google Cloud Storage is a highly scalable, durable, and cost-effective object storage service that Google Cloud Platform (GCP) offers. It facilitates the storage and management of substantial volumes of unstructured data. This solution provides noteworthy performance and reliability and can effectively meet the needs of various applications, including data backups and content delivery for websites and applications. Google Cloud Storage’s fundamental building block is the object, which consists of the actual content, metadata, and a unique identifier. Users can utilize different storage classes, namely Standard, Nearline, Coldline, and Archive, to fulfill specific access speed and cost requirements.
Google Cloud Storage allows users to access and manipulate data through a user-friendly web-based console, RESTful APIs for programmatic integration, command-line tools, and software development kits (SDKs) for multiple programming languages. Google Cloud IAM manages fine-grained access controls and permissions, enabling granular control over bucket and object-level access permissions. To ensure high durability and availability, Google Cloud Storage distributes data across multiple geographic regions, reducing risks associated with hardware failures and other contingencies. Additionally, the service seamlessly scales to accommodate evolving storage needs, automating expansion tasks without requiring manual intervention.
Application
There are multiple ways to access and use Google Cloud Storage:
- Console: The Google Cloud console provides a visual interface for you to manage your data in a browser.
- Google Cloud CLI: The gcloud CLI allows you to interact with Cloud Storage through a terminal using
gcloud storage
commands. - Client libraries: The Cloud Storage client libraries allow you to manage your data using multiple languages.
- REST APIs: Manage your data using the JSON or XML API.
- Terraform: Terraform is an infrastructure-as-code (IaC) tool that you can use to provision the infrastructure for Cloud Storage.
- Cloud Storage FUSE: Cloud Storage FUSE allows you to mount Cloud Storage buckets to your local file system. This enables your applications to read from a bucket or write to a bucket by using standard file system semantics.
Enough theory let’s try out an implementation and see how can we access GCS using Spring Cloud GCP. For this we will setup our GCP account and use the credentials to access GCS using REST APIs from our Springboot app.
Set up your Google Cloud Project
- Create a Google Cloud Project if you don’t have one: https://console.cloud.google.com/projectcreate
- Enable the Google Cloud Storage API for your project: https://console.cloud.google.com/flows/enableapi?apiid=storage.googleapis.com

Set up Google Cloud SDK and Authentication
- Install the Google Cloud SDK: https://cloud.google.com/sdk/docs/install
- Create a Spring Boot Project You can create a Spring Boot project using a tool like Spring Initializr (https://start.spring.io/), or use your preferred IDE to create a new Spring Boot project.
- Configure authentication:
- After you have installed Google Cloud SDK open up the terminal in your IDE and run
-
gcloud auth login
- Now a browser window will open up asking for permission to access your GCP account. Allow it and go back to your Terminal and run:
-
gcloud config set project PROJECT_ID
(replacePROJECT_ID
with your Google Cloud Project ID).
- Now we can access all GCP resources from our local machine.
Springboot Application
Add the required dependencies:
For Maven:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.22.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
</dependency>
For Gradle:
implementation platform('com.google.cloud:libraries-bom:26.22.0')
implementation 'com.google.cloud:google-cloud-storage'
Create a Service to interact with GCS:
@Service public class GcsService { private String bucketName = your-gcs-bucket-name; private final Storage storage; public GcsService() { this.storage = StorageOptions.getDefaultInstance().getService(); } public void writeDataToGcs(String filename, String data) throws IOException { Bucket bucket = storage.get(bucketName); bucket.create(filename, data.getBytes(StandardCharsets.UTF_8)); } public String readDataFromGcs(String filename) throws IOException { Bucket bucket = storage.get(bucketName); return new String(bucket.get(filename).getContent(), StandardCharsets.UTF_8); } }
Create a REST Controller to expose GCS operations:
@RestController @RequestMapping("/gcs") public class GcsController { @Autowired private GcsService gcsService; @PostMapping("/write") public void writeData(@RequestParam String filename, @RequestParam String data) throws IOException { gcsService.writeDataToGcs(filename, data); } @GetMapping("/read") public String readData(@RequestParam String filename) throws IOException { return gcsService.readDataFromGcs(filename); } }
Run the application and test:
Please proceed with running your Spring Boot application. Once successfully executed, you will gain access to the REST endpoints /gcs/write
and /gcs/read
. These endpoints will enable you to facilitate the writing and reading of data to and from Google Cloud Storage.
NOTE: When you are done with all the tasks please do not forget to delete the Cloud Storage buckets/objects to avoid additional charges.
Summary
In conclusion, our Springboot application provides a seamless integration with Google Cloud Storage, enabling efficient data storage and retrieval. Stay tuned for more insightful articles on cutting-edge technologies at Nashtech Blogs, where we continue to explore and share the latest innovations in the tech industry.
References
All the contents in this blog are referred from Google Cloud Platform and Spring Cloud GCP documentation and guides.