Java and Azure SDK

Recently a customer asked me how to upload a file to Azure Blob Storage in java code and have a progress bar while doing it. This means using the CloudBlockBlob methods PutBlob and PutBlobList if programming in C#. Looking into the Azure Java SDK I cound see that there was support for it, although the methods were renamed to uploadBlock and commitBlockList, but I could find no samples available while searching the internet that showed how to use them that I could give to my customer. So, time came to use Java programming skills I had not used in many years.

I didn’t even have java installed on my laptop so one second objective became to see how easy it was to use Azure if you are not in the Visual Studio world. To create a java dev enironment from nothing, I downloaded:

I didn’t want Oracles installation to intrude to much on my laptop, since after all, using java is not what I normally do. After installation was complete of the JSE, I safely removed alterations Oracle installer had done to PATH, etc, and created a command prompt shortcut on the desktop with the target of C:\Windows\System32\cmd.exe /E:ON /V:ON /K “C:\Users\cljung\src\java\setenv.cmd”.


Then using Sublime Text as an editor, it was time to write some code translating what other people had done in C# to Java. I used the following as my template

To use the JARs in the Azure Java SDK I needed to reference the azure storage classes with imports. Beware if you found some samples on the internet, because the namespace have changed. If you get compiler errors, snoop in the JAR-files of the Azure SDK you downloaded and see if there are any new changes. In my case, I got bitten by a change to all lowercase.


Since I was writing this sample for a customer, I didn’t like hardcoding the StorageAccountName or Key in the code, like most other samples do, and therefor I decided to go with having a config file for that.


The meat of the code is actually around reading chunks of the file at a time and sending that in blocks to Azure Storage and at the end send a commit of the list of blocks to Azure. The Blob Storage file isn’t visible in the storage container until you issue the commit command. Uploading of large video files on 3-4 GB can be done this way without locking the UI while doing it. To gain performance you can even to this in parallell threads and/or async, but that wasn’t the objective for this sample.

The method uploadBlock takes a BlockEntry object, an InputStream object and a long integer that specifies the chunk size as parameters. The BlockEntry is just a class holding the BlockId, which is an text based id, encoded in base64, that uniquely identifies the block. I use “BlockId” followed by a block number that increments from 1 and on, formated into seven digits with leading zeroes. The base64 thing is so it will survive the HTTP PUT payload in the commit part.


The method commitBlockList takes an object that inherits from Iterable as a parameter which means saving them in List<BlockEntry> while we create the blocks for each uploadBlock call.

The rest is pretty basic code keeping track of where we are in the file and how large of a chunk to send in the next block. You will get the hang of it.


Primary objective achieved – Uploaded a large file with a progress bar (ok, pretty simple, but you get the idea) using Java.

Secondary objective also achieved – use a minimalistic dev environment and avoid Eclipse, etc.

The full source code is available here. Use it at your own risk

Leave a Reply

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