Skip to content

Instantly share code, notes, and snippets.

@backwind1233
Last active January 3, 2022 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save backwind1233/0f5a5e11ed9856db141f4cdc1c285971 to your computer and use it in GitHub Desktop.
Save backwind1233/0f5a5e11ed9856db141f4cdc1c285971 to your computer and use it in GitHub Desktop.

Resource Handling

  1. Get a resource
    1. Get a resource with @Value
    2. Get a resource with ResourceLoader
    3. Get resources by searching pattern
  2. Handling with resource
    1. Download data from specific resource.
    2. Upload data to specific resource.

1. Get a resource

1.1 Get a resource with @Value

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;

1.2 Get a resource with ResourceLoader

@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]");

1.3 Get resources by searching pattern

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:// or azure-file://. Such as azure-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:// or azure-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"); 

2. Handling with resource

2.1 Download data from specific resource.

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();

2.2 Upload data to specific resource.

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());
}

@kasobol-msft
Copy link

This looks good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment