Azure Storage Java tutorial

Updated: 2024-04-01

Azure Storage allows to store files and blobs in Azure cloud, this is similar to the S3 offer of AWS.

In my test I will use Azure Blob storage to store the markdown files of this blog and dinamycally load them when required.

Connection with Java

pom.xml

If you are using Spring you can add this dependency to your pom.xml:

<dependency> 
  <groupId>com.azure.spring</groupId> 
  <artifactId>spring-cloud-azure-starter-storage-blob</artifactId> 
</dependency> 
 
<dependencyManagement> 
  <dependencies> 
    <dependency> 
      <groupId>com.azure.spring</groupId> 
        <artifactId>spring-cloud-azure-dependencies</artifactId> 
        <version>5.11.0</version> 
        <type>pom</type> 
        <scope>import</scope> 
      </dependency> 
  </dependencies> 
</dependencyManagement> 

In a Java application without Spring Boot you can add the dependency:

<dependency> 
  <groupId>com.azure</groupId> 
  <artifactId>azure-storage-blob</artifactId> 
  <version>12.25.3</version>  
</dependency> 

spring configuration

In your application.properties you need to add the information about your Azure storage.

For this example, I'm using the lazy solution of the connection string. Azure allows you to choose between multiple authorization modes, check the documentation for alternatives.

spring.cloud.azure.storage.blob.account-name=$YOUR_STORAGE_ACCOUNT 
spring.cloud.azure.storage.blob.endpoint=$YOUR_STORAGE_ENDPOINT 
spring.cloud.azure.storage.blob.connection-string=$YOUR_STORAGE_CONNECTION_STRING 

Azure Service

I created an AzureStorageService to handle the request for the files:

... 
import com.azure.core.util.BinaryData; 
import com.azure.storage.blob.BlobContainerClient; 
import com.azure.storage.blob.BlobServiceClient; 
import com.azure.storage.blob.models.BlobItem; 
... 
@Service 
@AllArgsConstructor 
@Slf4j 
public class AzureStorageService { 
   private BlobServiceClient blobServiceClient; 
    

The BlobServiceClient is a Client that is automatically configured with our properties and allows us the access to the storage.

List the objects (files) and read the content

I need to read all the content of the storage and filter the files of type markdown (.md).

These files need to be transformed in arrays of lines (strings).

public List<Article> readArticles() { 
  List<Article> articleList = new ArrayList<>(100); 
  BlobContainerClient containerClient = blobServiceClient 
          .getBlobContainerClient("posts"); 
 
  for (BlobItem blobItem : containerClient.listBlobs()) { 
    if (blobItem.getName().endsWith(".md")) { 
      try { 
        Article article; 
        BinaryData binaryData = containerClient.getBlobClient(blobItem.getName()) 
        .downloadContent(); 
 
        List<String> articleContent = convertBinaryDataToStringList(binaryData.toBytes()); 
        ... 
        } catch (Exception e) { 
          log.error(e.getMessage(), e); 
    } 
    } 
  } 
  return articleList; 
} 

In the client I specify the specific storage that I want to access (posts):

BlobContainerClient containerClient = blobServiceClient 
  .getBlobContainerClient("posts"); 

With .listBlobs() I can retrieve the list of objects (BlobItem) inside of my storage.

The BlobItem is only the representation of the file, we can access some of it's properties. In our case we need the name of the file to filter the markdown files: blobItem.getName().

To get the content of the file we need to download it using the client:

BinaryData binaryData = containerClient 
  .getBlobClient(blobItem.getName()) 
  .downloadContent(); 

After that we can transform the file content in an array of strings using a custom method:

List<String> articleContent = convertBinaryDataToStringList(binaryData.toBytes()); 

My Opinion

The integration with Spring Boot is very smooth and the integration was quick. The complexity of Azure Storage resides more in the multiple choices of features / costs of the Microsoft Cloud than the application implementation.


Fullstack Angular / Java application quick start guide.
WebApp built by Marco using SpringBoot 3.2.4 and Java 21. Hosted in Switzerland (GE8).