You can use the annotation of @Value("azure-blob://[your-container-name]/[your-blob-name]")
to autowire a blob Resource.
@Value("azure-blob://[your-container-name]/[your-blob-name]")
private Resource storageResource;
You can use the annotation of @Value("azure-file://[your-fileshare-name]/[your-file-name]") to autowire a file Resource.
@Value("azure-file://[your-fileshare-name]/[your-file-name]")
private Resource storageResource;
@Autowired
private ResourceLoader resourceLoader;
...
// get a BlobResource
Resource storageBlobResource = resourceLoader.getResource("azure-blob://[your-container-name]/[your-blob-name]");
// get a FileResource
Resource storageFileResource = resourceLoader.getResource("azure-file://[your-fileshare-name]/[your-file-name]");
You can use implementation class AzureStorageBlobProtocolResolver
of ResourcePatternResolver
to search blob
resource, and AzureStorageFileProtocolResolver
of ResourcePatternResolver
to search file
resource
- Pattern search, the searchPattern should start with
azure-blob://
orazure-file://
. Such asazure-blob://**/**
, it means list all blobs in all containers;azure-blob://demo-container/**
, it means list all blobs in the demo-container container, including any sub-folder. - Location search, the searchLocation should start with
azure-blob://
orazure-file://
, the remaining file path should exist, otherwise an exception will be thrown.
@Autowire
private AzureStorageBlobProtocolResolver azureStorageBlobProtocolResolver;
@Autowire
private AzureStorageFileProtocolResolver azureStorageFileProtocolResolver;
// 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 Blob or file storage with the getInputStream()
method of Resource.
@Value("azure-blob://[your-container-name]/[your-blob-name]")
private Resource storageBlobResource;
@Value("azure-file://[your-fileshare-name]/[your-file-name]")
private Resource storageFileResource;
....
// download data as stream from blob resource
InputStream inputblobStream = storageBlobResource.getInputStream();
// download data as stream from file resource
InputStream inputfileStream = storageFileResource.getInputStream();
You can upload to a resource to Azure Blob or file storage by casting the Spring Resource
to WritableResource
.
@Value("azure-blob://[your-container-name]/[your-blob-name]")
private Resource storageBlobResource;
@Value("azure-file://[your-fileshare-name]/[your-file-name]")
private Resource storageFileResource;
String data = "sampledata";
// upload string data to blob
try (OutputStream blobos = ((WritableResource) this.storageBlobResource).getOutputStream()) {
blobos.write(data.getBytes());
}
// upload string data to file
try (OutputStream fileos = ((WritableResource) this.storageFileResource).getOutputStream()) {
fileos.write(data.getBytes());
}
This looks good.