Azure File Storage Integration with Spring Boot: Upload, List, Download & Delete Files - Step-by-Step Guide

🔷 Diagram Explanation

  1. 👤 User interacts with the Spring Boot application through HTTP requests.
  2. 📂 AzureFileController handles user requests and forwards them to ⚙️ AzureFileStorageService.
  3. ⚙️ AzureFileStorageService communicates with 📦 Azure File Storage to perform:
    • 📤 Upload files
    • 📄 List files
    • 📥 Download files
    • Delete files
  4. ☁️ Azure Cloud hosts the 📦 Azure File Storage, which securely stores the files.


Azure File Storage provides a cloud-based fully managed file share system that allows applications to store and manage files in a distributed environment. In this guide, we'll build a Spring Boot application that interacts with Azure File Storage to:
✅ Upload files
✅ List files
✅ Download files
✅ Delete files


2. Prerequisites

✔️ Azure Subscription – Sign up here if you don’t have one.
✔️ Azure Storage Account – Create one via the Azure Portal.
✔️ Spring Boot (3.x) application with Spring Web dependency.
✔️ Azure Storage SDK (azure-storage-file-share).


3. Setting Up Azure File Storage

Step 1: Create a Storage Account

  1. Log in to the Azure Portal.
  2. Search for Storage Accounts → Click Create.
  3. Fill in:
    • Subscription: Choose your subscription
    • Resource Group: Create or select a resource group
    • Storage Account Name: unique-name
    • Region: Select a nearby region
    • Performance: Standard
  4. Click Review + CreateCreate.

Step 2: Create a File Share

  1. Inside your Storage Account, go to File shares.
  2. Click + File share → Enter a name and size limit → Click Create.
  3. Copy the Connection String from Access Keys.

4. Spring Boot Project Setup

Step 1: Add Dependencies (pom.xml)

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-storage-file-share</artifactId>
    <version>12.25.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

5. Application Configuration

Step 1: Add Configuration in application.properties

azure.storage.account-name=<your-storage-account-name>
azure.storage.account-key=<your-storage-account-key>
azure.storage.share-name=<your-file-share-name>

6. Implement Azure File Storage Service

Step 1: Create AzureFileStorageService.java

import com.azure.storage.file.share.*;
import com.azure.storage.file.share.models.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.*;

@Service
public class AzureFileStorageService {

    private final ShareFileClient fileClient;
    private final ShareDirectoryClient rootDirectory;

    public AzureFileStorageService(
            @Value("${azure.storage.account-name}") String accountName,
            @Value("${azure.storage.account-key}") String accountKey,
            @Value("${azure.storage.share-name}") String shareName) {

        String connectionString = String.format("DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s;EndpointSuffix=core.windows.net",
                accountName, accountKey);

        ShareServiceClient serviceClient = new ShareServiceClientBuilder()
                .connectionString(connectionString)
                .buildClient();

        ShareClient shareClient = serviceClient.getShareClient(shareName);
        this.rootDirectory = shareClient.getRootDirectoryClient();
        this.fileClient = rootDirectory.getFileClient("testfile.txt");
    }

    public void uploadFile(MultipartFile file) throws IOException {
        ShareFileClient fileClient = rootDirectory.getFileClient(file.getOriginalFilename());
        fileClient.create(file.getSize());
        fileClient.upload(new ByteArrayInputStream(file.getBytes()), file.getSize());
    }

    public List<String> listFiles() {
        List<String> fileNames = new ArrayList<>();
        rootDirectory.listFilesAndDirectories().forEach(item -> fileNames.add(item.getName()));
        return fileNames;
    }

    public byte[] downloadFile(String fileName) {
        ShareFileClient fileClient = rootDirectory.getFileClient(fileName);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        fileClient.download(outputStream);
        return outputStream.toByteArray();
    }

    public void deleteFile(String fileName) {
        ShareFileClient fileClient = rootDirectory.getFileClient(fileName);
        fileClient.delete();
    }
}

7. Implement Controller

Step 1: Create AzureFileController.java

import com.example.azurefileservice.service.AzureFileStorageService;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Controller
@RequestMapping("/files")
public class AzureFileController {

    private final AzureFileStorageService fileStorageService;

    public AzureFileController(AzureFileStorageService fileStorageService) {
        this.fileStorageService = fileStorageService;
    }

    @GetMapping
    public String listFiles(Model model) {
        model.addAttribute("files", fileStorageService.listFiles());
        return "file-list";
    }

    @PostMapping("/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        fileStorageService.uploadFile(file);
        return "redirect:/files";
    }

    @GetMapping("/download/{fileName}")
    public ResponseEntity<byte[]> downloadFile(@PathVariable String fileName) {
        byte[] fileContent = fileStorageService.downloadFile(fileName);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + fileName)
                .body(fileContent);
    }

    @GetMapping("/delete/{fileName}")
    public String deleteFile(@PathVariable String fileName) {
        fileStorageService.deleteFile(fileName);
        return "redirect:/files";
    }
}

8. Implement HTML UI for File Management

Create file-list.html in src/main/resources/templates/

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Azure File Storage</title>
</head>
<body>
    <h2>Azure File Storage</h2>

    <h3>Upload File</h3>
    <form action="/files/upload" method="post" enctype="multipart/form-data">
        <input type="file" name="file">
        <button type="submit">Upload</button>
    </form>

    <h3>Stored Files</h3>
    <ul>
        <th:block th:each="file : ${files}">
            <li>
                <a th:href="@{/files/download/{fileName}(fileName=${file})}" th:text="${file}"></a>
                <a th:href="@{/files/delete/{fileName}(fileName=${file})}" style="color: red;">Delete</a>
            </li>
        </th:block>
    </ul>
</body>
</html>

9. Run & Test the Application

Step 1: Start Spring Boot Application

mvn spring-boot:run

Step 2: Open Browser & Test

Visit http://localhost:8080/files

  • Upload files
  • List available files
  • Download files
  • Delete files

10. Summary

✅ Configured Azure File Storage
✅ Implemented Spring Boot service for file management
✅ Built a REST API & Web UI for file upload, list, download, and delete

🚀 Now you have a complete Spring Boot application integrated with Azure File Storage!

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete