How to download a file from Azure Claud Storage

In this article, we will look at how to download a file from Azure Cloud Storage for legacy API.


Sometimes you must deal with the development of legacy applications which require a thorough approach. You go through ancient documentation, and at the same time, you Google stuff on the internet and hope your questions will be answered for a concrete version of your dependencies. It was also my case.

Therefore I have decided to share a piece of code for legacy Azure Storage API. To be precise, this article focused on the Azure Storage version 5 Software Development Kit.


This is just simple example of working code. For any new development, please use rather new API for Azure Storage. Azure teams created very comprehensive documentation. You can get inspired for example by Azure Storage Blob API documentation [1], [2].

Here is a code for a Java method that downloads a file from Azure Cloud Storage. Certain code lines of arbitrary class code were omitted for code brevity.



protected Logger logger = Logger.getLogger(getClass());


private void downloadFromAzureCloudStorageToDisk() {
    try {"File download from Azure Storage to Disk started.");

        final String sourceContainer = "reports";
        final String sourceFolder = "daily_reports";
        final String targetDir = "C://reports/daily/csv/";
        final String targetFileName = "DAILY_REPORT_IN_CSV.csv";

        final String targetConnection =
            "AccountName=HERE_GOES_ACCOUNT_NAME;" +
            "AccountKey=VERY_LONG_ACCOUNT_KEY;" +
            "DefaultEndpointsProtocol=https;" +

        final CloudStorageAccount storageAccount = CloudStorageAccount.parse(targetConnection);

        final CloudFileClient cloudFileClient = storageAccount.createCloudFileClient();

        final CloudFileShare azureStorageContainer = cloudFileClient.getShareReference(sourceContainer);

        CloudFileDirectory directory = azureStorageContainer.getRootDirectoryReference();

        directory = directory.getDirectoryReference(sourceFolder);

        final CloudFile fileReference = directory.getFileReference(targetFileName);

        final File targetFilePath = new File(targetDir + '/' + targetFileName);

        try (FileOutputStream fileOutputStream = new FileOutputStream(targetFilePath)) {
        } catch (IOException ex) {
            logger.error("Error saving file to target directory: ", ex);
            throw e;
        }"File download from Azure Storage to Disk completed.");

    } catch (StorageException | URISyntaxException ex) {
        logger.error("FromAzureCloudStorageToDisk threw exception. Storage or URI syntax is wrong: ", ex);
    } catch (Exception ex) {
        logger.error("FromAzureCloudStorageToDisk threw exception: ", ex);

First, we will create an instance of CloudStorageAccount into which we will push all credentials and the login information. From CloudStorageAccount instance we will instantiate Azure Cloud Storage client (cloudFileClient).

And from this point, everything should go very smoothly. We have an Azure Cloud Storage client, and all we need to do is get an instance of the reference of the file we want to download. We are using DAILY_REPORT_IN_CSV.csv as an arbitrary stand for the filename we want to download. You will download the file by loading the correct Azure Cloud Storage container and then getting a reference on its root directory. Then, we will crawl the root directory in the direction of the desired file and get its reference.

Now we have a reference instance on the file in our Azure Cloud Storage. We can download it. To do so, we need to save it on the local disk. And for that, we use the last piece of method code. File reference can be download only into Java’s object with OutputStream interface behavior. However, this condition fulfills any instance of OutputStream implementation.

Do not forget to close your stream if you are working with older Java. For safety, use Java’s try-with-resource stream construction instead, which has been available since Java 7.

This entry was posted in Tutorials and tagged , , , , , , , , , , , , , . Bookmark the permalink.

1 Response to How to download a file from Azure Claud Storage

  1. Pingback: How to trigger action with Scheduler and Quartz in Camel |

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.