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:
- An Azure subscription; if you don't already have an Azure subscription, you can activate your MSDN subscriber benefits or sign up for a free Azure account.
- The Azure Command-Line Interface (CLI).
- A supported Java Development Kit (JDK). For more information about the JDKs available for use when developing on Azure, see Java support on Azure and Azure Stack.
- Apache Maven, version 3.0 or later.
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.
Browse to the Azure portal at https://portal.azure.com/ and sign in.
Select Create a resource, then Get started, and then select Storage Account.

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.
When you have specified the options listed above, select Review + create.

Review the specification, then select Create to create your storage account.
When the deployment is complete, select Go to resource.
Select Containers.
Select Container.
- Name the container.
- Select Blob from the drop-down list.

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.
Open a command prompt.
Sign in to your Azure account:
az loginIf you don't have a resource group, create one using the following command:
az group create \ --name <resource-group> \ --location <location>Create a storage account by using the following command:
az storage account create \ --name <storage-account-name> \ --resource-group <resource-group> \ --location <location>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.
Browse to https://start.spring.io/.
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.

Note
The Spring Initializr uses the Group and Artifact names to create the package name; for example: com.wingtiptoys.storage.
When you have specified the options listed above, select GENERATE.
When prompted, download the project to a path on your local computer.
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.
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.xmlOpen 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>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.
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.propertiesOpen 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. 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
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.javaOpen 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"; } }Save and close the blob controller Java file.
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/storageBuild your Spring Boot application with Maven and run it; for example:
mvn clean package mvn spring-boot:runOnce 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/writeBlobFileYou 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/readBlobFileYou 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: