How to use the Spring Boot Starter for Azure Storage

This article walks you through creating a custom application using the Spring Initializr, then adding the Azure Storage Blob starter to your application, and then using your application to upload a blob to your Azure storage account.

Prerequisites

The following prerequisites are required in order to follow the steps in this article:

Important

Spring Boot version 2.5 or 2.6 is required to complete the steps in this article.

Create an Azure Storage Account and blob container for your application

The following procedure creates an Azure storage account and container in the portal.

  1. Browse to the Azure portal at https://portal.azure.com/ and sign in.

  2. Select Create a resource, then Get started, and then select Storage Account.

    Azure portal screenshot showing the 'Create a resource' page with the search box highlighted with search text 'Storage accounts'.

  3. On the Create storage account page, enter the following information:

    • Select Subscription.
    • Select Resource group, or create a new resource group.
    • Enter a unique Storage account name, which will become part of the URI for your storage account. For example: if you entered wingtiptoysstorage for the Name, the URI would be wingtiptoysstorage.core.windows.net.
    • Specify the Location for your storage account.
  4. When you have specified the options listed above, select Review + create.

    Azure portal screenshot of 'Create a storage account' page.

  5. Review the specification, then select Create to create your storage account.

  6. When the deployment is complete, select Go to resource.

  7. Select Containers.

  8. Select Container.

    • Name the container.
    • Select Blob from the drop-down list.

    Azure portal screenshot of a Storage account Containers page with the 'New container' pane showing.

  9. The Azure portal will list your blob container after it's been created.

You can also use Azure CLI to create an Azure storage account and container using the following steps. Remember to replace the placeholder values (in angle brackets) with your own values.

  1. Open a command prompt.

  2. Sign in to your Azure account:

    az login
    
  3. If you don't have a resource group, create one using the following command:

    az group create \
       --name <resource-group> \
       --location <location>
    
  4. Create a storage account by using the following command:

     az storage account create \
       --name <storage-account-name> \
       --resource-group <resource-group> \
       --location <location>
    
  5. To create a container, use the following command:

     az storage container create \
       --account-name <storage-account-name> \
       --name <container-name> \
       --auth-mode login
    

Create a simple Spring Boot application with the Spring Initializr

The following procedure creates the Spring boot application.

  1. Browse to https://start.spring.io/.

  2. Specify the following options:

    • Generate a Maven project.
    • Specify Java 11.
    • Specify a Spring Boot version that is equal to 2.5.10.
    • Specify the Group and Artifact names for your application.
    • Add the Spring Web dependency.

    Screenshot of Spring Initializr with basic options.

    Note

    The Spring Initializr uses the Group and Artifact names to create the package name; for example: com.wingtiptoys.storage.

  3. When you have specified the options listed above, select GENERATE.

  4. When prompted, download the project to a path on your local computer.

  5. After you have extracted the files on your local system, your simple Spring Boot application will be ready to edit.

Configure your Spring Boot app to use the Azure Storage Blob starter

Add dependency in pom.xml

The following procedure configures the Spring boot application to use Azure storage.

  1. Locate the pom.xml file in the root directory of your app; for example:

    C:\SpringBoot\storage\pom.xml

    -or-

    /users/example/home/storage/pom.xml

  2. Open the pom.xml file in a text editor, and add the Spring Cloud Azure Storage starter to the list of <dependencies>:

     <dependency>
       <groupId>com.azure.spring</groupId>
       <artifactId>spring-cloud-azure-starter-storage-blob</artifactId>
       <version>4.0.0</version>
     </dependency>
    
  3. Save and close the pom.xml file.

Configure property in application.yml

The following procedure configures the Spring boot application to use your Azure storage account.

  1. Locate the application.yml in the resources directory of your app; for example:

    C:\SpringBoot\storage\src\main\resources\application.yml

    -or-

    /users/example/home/storage/src/main/resources/application.yml

  2. Open the application.yml file in a text editor, add the following lines, and then replace the sample values with the appropriate properties for your storage account:

spring:
  cloud:
    azure:
      storage:
        blob:
          account-name: [storage-account-name]
          account-key: [storage-account-access-key]
          endpoint: [storage-blob-service-endpoint]

Where:

Name Description Required
spring.cloud.azure.storage.blob.account-name The name of the Azure Storage account. Yes
spring.cloud.azure.storage.blob.account-key The access key of the Azure Storage account. Yes
spring.cloud.azure.storage.blob.endpoint The blob endpoint URL of the Azure Storage account. Yes
  1. Save and close the application.yml file.

Add sample code to implement basic Azure storage functionality

In this section, you'll create the necessary Java classes for storing a blob in your Azure storage account.

Add a blob controller class

  1. Create a new Java file named BlobController.java in the package directory of your app; for example:

    C:\SpringBoot\storage\src\main\java\com\wingtiptoys\storage\BlobController.java

    -or-

    /users/example/home/storage/src/main/java/com/wingtiptoys/storage/BlobController.java

  2. Open BlobController.java in a text editor, and add the following lines to the file. Replace the <your-resource-group>, <your-artifact-name>, <your-container-name>, and <your-blob-name> placeholders with your values.

    package com.<your-resource-group>.<your-artifact-name>;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.core.io.Resource;
    import org.springframework.core.io.WritableResource;
    import org.springframework.util.StreamUtils;
    import org.springframework.web.bind.annotation.*;
    
    import java.io.IOException;
    import java.io.OutputStream;
    import java.nio.charset.Charset;
    
    @RestController
    @RequestMapping("blob")
    public class BlobController {
    
        @Value("azure-blob://<your-container-name>/<your-blob-name>")
        private Resource blobFile;
    
        @GetMapping("/readBlobFile")
        public String readBlobFile() throws IOException {
            return StreamUtils.copyToString(
                    this.blobFile.getInputStream(),
                    Charset.defaultCharset());
        }
    
        @PostMapping("/writeBlobFile")
        public String writeBlobFile(@RequestBody String data) throws IOException {
            try (OutputStream os = ((WritableResource) this.blobFile).getOutputStream()) {
                os.write(data.getBytes());
            }
            return "file was updated";
        }
    }
    
  3. Save and close the blob controller Java file.

  4. Open a command prompt and change directory to the folder where your pom.xml file is located; for example:

    cd C:\SpringBoot\storage
    

    -or-

    cd /users/example/home/storage
    
  5. Build your Spring Boot application with Maven and run it; for example:

    mvn clean package
    mvn spring-boot:run
    
  6. Once your application is running, you can use curl to test your application; for example:

    a. Send a POST request to update a file's contents:

    curl http://localhost:8080/blob/writeBlobFile -d "new message" -H "Content-Type: text/plain"
    

    You should see a response that file was updated.

    b. Send a GET request to verify the file's contents:

    curl -X GET http://localhost:8080/blob/readBlobFile
    

    You should see the "new message" text that you posted.

Summary

In this tutorial, you created a new Java application using the Spring Initializr, added the Azure Storage Blob starter to your application, and then configured your application to upload a blob to your Azure storage account.

Clean up resources

When no longer needed, use the Azure portal to delete the resources created in this article to avoid unexpected charges.

Next steps

To learn more about Spring and Azure, continue to the Spring on Azure documentation center.

Additional Resources

For more information about the additional Spring Boot Starters that are available for Microsoft Azure, see Spring Boot Starters for Azure.

For detailed information about additional Azure storage APIs that you can call from your Spring Boot applications, see the following articles: