How to download a file from Azure Claud Storage

At some point, you need to deal with and develop legacy code for legacy software. As you Google stuff on the internet and waiting for being by StackOverflow, you go through ancient documentation. It was also my case.

Therefore I have decided to share with you a piece of code for legacy Azure Storage API. To be precise, this is the article for 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 e) {
            logger.error( "Error saving file to target directory: ", e );
            throw e;
        }"File download from Azure Storage to Disk completed.");

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

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. In our case, we are using DAILY_REPORT_IN_CSV.csv as an arbitrary stand for the filename we want to download. Downloading the file can be achieved by loading the correct Azure Cloud Storage container and then getting a reference on its root directory. We crawl the root directory up to the desired file, and we 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 instead Java’s try-with-resource stream construction, which is 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.

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