Microsoft Azure Blob Storage with a Spring Boot App

Gokhan Yilmaz
iyzico.engineering
Published in
5 min readAug 29, 2020

--

Here is Azure Blob Storage Spring boot example. In this article, We’ll discuss the Blob Storage services and we will make an example by using Microsoft Azure Blob Storage service for better understanding. Let’s start.

Prerequisite :

1.Microsoft Azure Account: If you do not have an Azure account there are some options to create the Azure account free of cost.

  • If you are a student, you can access free Azure account by the Azure4students
  • If you are not students still you can get the free Azure account for one year just visit here

2.Java — Spring Boot App

Blob Storage:

Blob Storage stores from hundreds to billions of objects in hot, cool, or archive tiers, depending on how often data access is needed. You get exabytes of capacity and massive scalability and also it does all of them easily and cost-effectively.

Why we should use cloud storage like Azure, Amazon WS, Google Cloud, etc?

Everything can be done via Databases, and you would only need to worry about SQL queries. Databases always could store binary content via its BLOB type. While you can store your images in the database, you will see that your database size increases considerably. But, If you store your images in blobs, and reference them via URI in your database, you will be able to query against your database, find the image’s URI, and then read from blob storage appropriately. That’s why it would be better to use cloud storage.

What is Azure Blob Storage?

Microsoft Azure Blob(Binary Large Objects) Storage is a managed cloud storage service that is secure, scalable, redundant, and highly available. We can use it to store large amounts of unstructured data such as images, audio, videos, documents, etc. The data can be exposed to the public or stored privately. Also, we can access this data from anywhere via HTTP and HTTPS protocols.

Azure storage services are accessible using REST APIs either by using services running on Azure or directly via the internet from any application that can send and receive HTTP and HTTPS requests.

Why use Azure Blob Storage?

Azure blob storage is primarily used to store and later retrieve large amounts of binary objects known simply as files. But, Blob storage can be used in many ways.

  • With rest-based object storage, the data stored in blob storage can be accessed from anywhere in the world with Azure’s regional data centers. It allows access to the right people at the right time without the need to worry about a physical location
  • The ability to edit a specific data object will improve overall performance while reducing bandwidth consumption
  • Blob storage ensures the user has superior data integrity. The data accessed is always the latest version as it instantly updates changes made to an object.
  • Ensures maximum availability by geo-replication to enhance both local and global access which leads to maximum business continuity.
  • Having page blobs, block blobs, and append blobs gives more flexibility to choose a storage option that suits your specific requirements.

How to Create an Azure Storage Account and Blob Container?

If you don’t have an Azure subscription, you need to subscribe first. Then you should follow the steps in the Azure Developer page to visit click here.

After performing the steps, you must keep the connection string and container name safely. Click to Access keys tab on the Storage Account page.

How to use Azure Blob Storage with Spring Boot application?

Let’s assume, you would like to upload a profile picture to Azure Blob Storage. At the same time, you don’t want to store your pictures as a Base64 format in DB. So, we’ll hold them on Azure cloud storage. Then Azure will only return us a URL as a response and we will only store this URL in our DB.

So, now we have to create a blob storage service.

  1. First of all, you need to add the following dependency to the list of <dependencies> in pom.xml:

2. Open application.yml file and add the following properties.

Enter your connection string and container name. Do not publish the fields directly for security. It would be a good choice to define these variables as an environment variable.

3. Now, we can write our blob storage service interface.

4. After that, we can write our service implementation. Here, we made some arrangments on the file and send it to Cloud. And Azure Storage service will return us a URI.

To briefly explain the annotations above:

@RequiredArgConstructor generates a constructor with 1 parameter for each field that requires special handling. The annotation comes from the Lombok library. For more information, visit here.

@ConditionalOnProperties checks if the specified property has a specific value. It checks by default if the property is in the environment and not equals to false. For more information, visit here.

For example, If you want to use a mock service instead of Azure Blob Storage Service, you can set this value as false or true. This way, you can easily switch your services using this annotation. By the way, the following class is our Mock service. As you see, we again implement the BlobStorageService interface. Of course, we override the method. The method always returns us the same value. However, we continue to use Azure Blob Storage Service instead of mock service.

5. Now, Let’s create a controller class and a method for uploading endpoints. This will accept the incoming picture.

6. In my example, I used MongoDB but, it does not matter what database you use. In this service layer, We send the multipart file object to the BlobStorageService that we wrote before. And It will give us the URL. After that, we pass it to the repository layer.

7. Finally, let’s create our repository layer for MongoDB. We updated the user’s profile picture. We just keep the picture’s URL in our database.

8. After all of these steps, you will be stored the pictures in Azure Blob Storage.

Also, you can check the endpoint by adding Swagger to your project.

Swagger

Thank you for reading.

--

--