Azure Storage: usage and testing with Java

Using Blob, Table and Queue storage in the cloud using Java... but testing it locally.

Azure Storage

Azure Storage is the highly scalable, secure, high available managed offering to store data in the cloud. Since not all storage needs are the same, there are a few different flavors:

  • Azure Blobs: Object store for text and binary data;
  • Azure Tables: NoSQL store for structured data;
  • Azure Queues: Message store for messaging between application components.

There are a few more flavors, but these I consider the originals ;).

Don’t care, show me code!

If blogs are not your vibe and like to learn from examples, you can jump directly to the example repo! I’ve made a couple of tests to showcase how the SDKs can be used to connect to a local Azurite container.

repo.jpg

Connecting to it?

Microsoft provides an easy to use SDK for most Azure components.

Import the BOM for easy dependency management in your pom.xml:

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.azure</groupId>
                <artifactId>azure-sdk-bom</artifactId>
                <version>1.2.22</version>
                <!-- lookup latest version ;) this was version at time of writing-->
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

Next, import the libraries you plan to use. In our case, we’ll try out the Blobs, Tables and Queues.

    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-data-tables</artifactId>
    </dependency>
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-storage-blob</artifactId>
    </dependency>
    <dependency>
        <groupId>com.azure</groupId>
        <artifactId>azure-storage-queue</artifactId>
    </dependency>

The use of the libraries themselves are pretty straightforward. Below is an example for Blob storage.

    // Create the client.
    var blobs = new BlobServiceClientBuilder()
                .connectionString(<YOUR CONNECTION STRING>) // Retrieved from your Azure portal.
                .buildClient();
    
    // Create a container, basically a sort of folder where the blobs will be saved.
    BlobContainerClient client = blobs.createBlobContainer("cat-pictures"); 
    // or use blobs.getBlobContainerClient("cat-pictures"); if the container already exists
    
    BlobClient blob = client.getBlobClient("my-cat-picture"); // create a new blob with a name
            
    // Save a cat picture to the blob storage.
    Path pathToPicture = Path.of("my-cat-picture.png")
    BinaryData data = BinaryData.fromFile(pathToPicture);
    blob.upload(data);
    
    // Some time later, you might want to download the file again ;)
    BinaryData downloaded = blob.downloadContent();

The official documentation can be found on the Azure SDK webpage.

Testing it!

Using the cloud is kinda cool… but being able to test your code without needing to pay for cloud resource usages is even cooler! And this is where we run into a bit of luck because the following two things are true:

  • A docker container exists which can emulate the Blob, Table and Queues endpoints: Azurite.
  • There is an easy way to start and stop Docker containers in your test: Testcontainers.

So let’s combine these two things together and show you some tests! Unfortunately, if you go to the official TestContainers website, you’ll find that Azurite doesn’t have a Java module. But no worries there, we can easily extend the Testcontainers GenericContainer class to build some utility ourselves.

I could copy over all the code on this page… but… it’s much easier to just look at the examples in the repo!

repo.jpg

FAQ

Can I get a free Storage Account somewhere?

Maybe not a free storage account but you can use Blob Storage for free (12 months). See the free services page on the Azure website.

Is Azure a perfect replacement for the real deal?

As with any “emulator” or “fake”, there are always things which don’t match the real implementation. For Azurite, these differences are listed on the official documentation page.

How expensive are the Storage solutions in Azure?

I would recommend checking out the official documentation for pricing.