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 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.0 or greater 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, create a resource, search for 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, create a storage account.

  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, storage account, containers, new container pane.

  9. The Azure portal will list your blob container after is has 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 or greater than 2.4.6.

    • Specify the Group and Artifact names for your application.

    • Add the Spring Web dependency.

      Basic Spring Initializr 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 starter

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>azure-spring-boot-starter-storage</artifactId>
       <version>3.13.0</version>
    </dependency>
    
  3. Save and close the pom.xml file.

Configure your Spring Boot app to use your Azure Storage account

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

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

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

    -or-

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

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

    # Storage account name length should be between 3 and 24 and use numbers and lower-case letters only
    azure.storage.account-name=<storage-account-name>
    
    # Fill storage account access key copied from portal
    azure.storage.account-key=<storage-account-access-key>
    
    # Fill storage blob endpoint URL copied from portal
    azure.storage.blob-endpoint=<storage-endpoint-URL>
    

    Where:

    Name Description Required
    azure.storage.accountName The name of the Azure Storage account. Yes
    azure.storage.accountKey The access key of the Azure Storage account. Yes
    azure.storage.blob-endpoint The blob endpoint URL of the Azure Storage account. Optional when a storage blob resource is used.
    azure.storage.file-endpoint The file endpoint URL of the Azure Storage account. Optional when a storage file resource is used.
  3. Save and close the application.properties file.

Add sample code to implement basic Azure storage functionality

In this section, you 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 the blob controller Java file 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 -d 'new message' -H 'Content-Type: text/plain' localhost:8080/blob/writeBlobFile
    

    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 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: