Spring Boot file upload with Google Cloud Storage



In this article, I will create a simple and fresh Spring Boot service with API for file upload using Google Cloud Storage. I am pointing them out one by one without boring descriptions but I have given the GitHub link at the end of the article. 

Setup the GCP account and configure it.

Click on this link and then you will be on the below page.




  • This is a very simple process. You can login using your Gmail, and click on “Get started for free”.
  • Then you need to select your country and organization or usage details. 
  • Now you can add your contact number and once you receive the verification code, you can enter and go next.
  • Then you may need to add the credit card details. Don’t worry :)
  • Now you can create a simple project.
  • From the left menu, go to Cloud Storage and then you can create a bucket.



  • You can go one by one filling required details and selecting them according to your requirements. 

Create a service account

  • The service account is required to use Cloud Storage.
  • Click on the left menu and IAM & Admin -> Service Accounts

  • Let’s create a service account. Click on the CREATE SERVICE ACCOUNT

  • Now you can fill in the required details.
  • Then click on the created service account email.
  • Then click on KEYS -> ADD KEY -> Create new key as below.
  • Then it will ask about the key type, choose JSON and the key will be downloaded. This is required for the Spring Boot service. 

Create a Spring Boot service

  • Here I am not going to explain line by line and each and every file in the project. You can find the working project the GitHub, the link below at the end of this article.
  • This is a basic Spring Boot application with a single API.
  • In this application, the selected file will be saved to the server and then uploaded into Google Cloud Storage. The file name and cloud URL will be saved in the database. (I used the H2 database for the development. You can use any database with your preference.)

The folder structure
Pom.xml


Controller class
  • This is straightforward and contains a POST API with a MultipartFile array.
Service class 
  • In the service class, I am just passing the file to the util class to store it in the cloud storage and then save relevant URLs in the database using the fileRepository. 

Util class
  • This file contains Google Cloud Storage-specific logic.

  • As you can see I have given a list of allowed extensions in the CheckFileExtension() method.
  • For the gcpConfigFile, you need to put the downloaded key file in the resources directory. You may change the file name accordingly in the application.yml

You can find the completed application on GitHub. But you need to add the downloaded Google Cloud Storage key file into the resources and add the file name into the application.yml file.

You can test this using Postman. Here I attached a screenshot for your reference.



Spring Boot file upload with Google Cloud Storage Spring Boot file upload with Google Cloud Storage Reviewed by Ravi Yasas on 7:06 PM Rating: 5

No comments:

Powered by Blogger.