title | description | ms.date | author | ms.author | ms.reviewer | ms.topic | ms.custom | appliesto | ||
---|---|---|---|---|---|---|---|---|---|---|
Spring Cloud Azure resource handling | This article describes Spring Cloud Azure resource handling. | 04/06/2023 | KarlErickson | karler | seal | reference | devx-track-java, devx-track-extended-java |
|
The Spring project provides a Spring Resources abstraction to access a number of low-level resources. The project provides interfaces like Resource
, ResourceLoader
and ResourcePatternResolver
. Spring Cloud Azure implements these interfaces for Azure Storage services, which allows you to interact with Azure storage Blob and File Share using the Spring programming model. Spring Cloud Azure provides spring-cloud-azure-starter-storage-blob
and spring-cloud-azure-starter-storage-file-share
to auto-configure Azure Storage Blob and Azure Storage File Share.
The following table lists Azure Storage related libraries:
Starter | Service | Description |
---|---|---|
spring-cloud-azure-starter-storage-blob | Azure Storage Blob | Allows unstructured data to be stored and accessed at a massive scale in block blobs. |
spring-cloud-azure-starter-storage-file-share | Azure Storage File Share | Offers fully managed cloud file shares that you can access from anywhere via the industry standard Server Message Block (SMB) protocol. |
<dependencies> <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-starter-storage-blob</artifactId> </dependency> <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-starter-storage-file-share</artifactId> </dependency> </dependencies>
The spring-cloud-azure-starter-storage-blob
dependency is only required when you're using Azure Storage Blob.
The spring-cloud-azure-starter-storage-file-share
dependency is only required when you're using Azure Storage File Share.
Tip
We also provide spring-cloud-azure-starter-storage
to support all the features of Storage. If you choose to use it, spring.cloud.azure.storage.enable
is the property to configure and the default value is true
. You can then use spring.cloud.azure.storage.<storage-service>.enable
to disable unneeded services.
Note
If you use a security principal to authenticate and authorize with Microsoft Entra ID for accessing an Azure resource, be sure the security principal has been granted sufficient permission to access the Azure resource. For more information, see Authorize access with Microsoft Entra ID.
The following table lists the configurable properties of spring-cloud-azure-starter-storage-blob
:
[!div class="mx-tdBreakAll"]
Property Default Description spring.cloud.azure.storage.blob.enabled true A value that indicates whether an Azure Blob Storage service is enabled. spring.cloud.azure.storage.blob.endpoint The URI to connect to Azure Blob Storage. spring.cloud.azure.storage.blob.account-key The private key to connect to Azure Blob Storage. spring.cloud.azure.storage.blob.account-name The Azure Storage Blob account name.
The following table lists the configurable properties of spring-cloud-azure-starter-storage-file-share
:
[!div class="mx-tdBreakAll"]
Property Default Description spring.cloud.azure.storage.fileshare.enabled true A value that indicates whether Azure File Storage service is enabled. spring.cloud.azure.storage.fileshare.endpoint The URI to connect to Azure File Storage. spring.cloud.azure.storage.fileshare.account-key The private key to connect to Azure File Storage. spring.cloud.azure.storage.fileshare.account-name The Azure Storage File Share account name.
Add the following properties to your application.yml file:
spring: cloud: azure: storage: blob: account-name: ${STORAGE_ACCOUNT_NAME}account-key: ${STORAGE_ACCOUNT_KEY}endpoint: ${STORAGE_BLOB_ENDPOINT}fileshare: account-name: ${STORAGE_ACCOUNT_NAME}account-key: ${STORAGE_ACCOUNT_KEY}endpoint: ${STORAGE_FILESHARE_ENDPOINT}
You can use the annotation of @Value("azure-blob://[your-container-name]/[your-blob-name]")
to autowire a blob resource, as shown in the following example:
@Value("azure-blob://[your-container-name]/[your-blob-name]") privateResourcestorageBlobResource;
You can use the annotation of @Value("azure-file://[your-fileshare-name]/[your-file-name]")
to autowire a file resource, as shown in the following example:
@Value("azure-file://[your-fileshare-name]/[your-file-name]") privateResourcestorageFileResource;
@AutowiredprivateResourceLoaderresourceLoader; ... // Get a BlobResource.ResourcestorageBlobResource = resourceLoader.getResource("azure-blob://[your-container-name]/[your-blob-name]"); // Get a FileResource.ResourcestorageFileResource = resourceLoader.getResource("azure-file://[your-fileshare-name]/[your-file-name]");
You can use an implementation class of ResourcePatternResolver
to search resources. Use AzureStorageBlobProtocolResolver
to search blob
resources and AzureStorageFileProtocolResolver
to search file
resources.
For pattern search, the
searchPattern
should start withazure-blob://
orazure-file://
. For example,azure-blob://**/**
means to list all blobs in all containers, andazure-blob://demo-container/**
means to list all blobs in thedemo-container
container, including any sub-folder.For location search, the
searchLocation
should start withazure-blob://
orazure-file://
and the remaining file path should exist, otherwise an exception will be thrown.
@AutowiredprivateAzureStorageBlobProtocolResolverazureStorageBlobProtocolResolver; @AutowiredprivateAzureStorageFileProtocolResolverazureStorageFileProtocolResolver; // Get all text blobs.Resource[] blobTextResources = azureStorageBlobProtocolResolver.getResources("azure-blob://[container-pattern]/*.txt"); // Get all text files.Resource[] fileTextResources = azureStorageFileProtocolResolver.getResources("azure-file://[fileshare-pattern]/*.txt");
You can download a resource from Azure Storage Blob or File Share with the getInputStream()
method of Resource
.
@Value("azure-blob://[your-container-name]/[your-blob-name]") privateResourcestorageBlobResource; @Value("azure-file://[your-fileshare-name]/[your-file-name]") privateResourcestorageFileResource; //...// Download data as a stream from a blob resource.InputStreaminputblobStream = storageBlobResource.getInputStream(); // Download data as a stream from a file resource.InputStreaminputfileStream = storageFileResource.getInputStream();
You can upload to a resource to Azure Blob or file storage by casting the Spring Resource
to WritableResource
, as shown in the following example:
@Value("azure-blob://[your-container-name]/[your-blob-name]") privateResourcestorageBlobResource; @Value("azure-file://[your-fileshare-name]/[your-file-name]") privateResourcestorageFileResource; Stringdata = "sampledata"; // Upload string data to a blob.try (OutputStreamblobos = ((WritableResource) this.storageBlobResource).getOutputStream()) { blobos.write(data.getBytes()); } // Upload string data to a file.try (OutputStreamfileos = ((WritableResource) this.storageFileResource).getOutputStream()) { fileos.write(data.getBytes()); }
Files larger than 4 MiB will be uploaded to Azure Storage in parallel.
See the storage-blob-sample and storage-file-sample repositories on GitHub.