Upload Files to Azure Blob Using Spring Boot

This post is about getting back to the basics of the Running application I had been working on. We will also discuss how we are going to feed the AI model with some Blob upload to Azure.

There are lot of samples/information that is available to upload to a blob container. This is a condensed version of my guide. It explains how to provision and bring down a Blob container using Terraform. It also shows how to use Spring Boot to upload and list all the files in a blob container.

First let us look at our Terraform code that is deploying

# Define the storage account
resource "azurerm_storage_account" "storage_account" {
  name                     = join("", [local.prefix, var.main_group_name, "storaccnt"])
  resource_group_name      = local.rg-name
  location                 = var.region
  account_tier             = var.account_tier
  account_replication_type = var.account_replication_type
  account_kind             = var.account_kind
  tags = {
    environment = var.environment
  }
}
#end of storage account definition

variable "container_access_type" {
  type        = string
  description = "The access type for the storage container."
  nullable    = false
}

resource "azurerm_storage_container" "data" {
  name                  = join("", [local.prefix, var.main_group_name, "storcont"])
  storage_account_name  = azurerm_storage_account.storage_account.name
  container_access_type = var.container_access_type
  #Come back for the life cycle management
  #has_legal_hold = var.change_feed_retention_in_days
}

Once we get that container defined., here is the code base for the variable files

main_group_name = "sathisprj1"
region = "EAST US"
# storage account attributes
account_tier = "Standard"
account_replication_type = "GRS"
account_kind = "StorageV2"
# storage contai                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ner attributes
container_access_type = "blob"
#Container Instances attributes

Terraform commands

Initterraform init
Apply terraform with varsterraform apply -var-file=”testing.tfvars”

This code will produce a BLOB account that looks like this

Now that the container is ready, we want to upload our activities.json, which has all the running activities, to feed the AI model. The next Java code, Gist, will be useful for uploading this data into the Blob container. I found this to be an easy way to upload a payload.

package me.sathish.common.sathishrunscommon.fileuploader;

import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.models.BlobContainerItem;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.List;


@Component
public class BlobFileUploader implements CommandLineRunner {
    // Path to the file to be uploaded
    private static final String FILE_PATH = "/Volumes/MacProHD/HDD_Downloads/Activities-1.csv";
    // Name of the blob container
    String blobContainerName = "devsathisprj1storaccnt";
    // Connection string for the Azure Blob Storage account
    String connectStr = "getururl";
    // List of blob containers in the storage account
    private List<BlobContainerItem> cotainersList;
    // Client for interacting with the Azure Blob Storage service
    private BlobServiceClient blobServiceClient;
    // Constructor to initialize the BlobServiceClient and list of containers
    public BlobFileUploader() {
        blobServiceClient = new BlobServiceClientBuilder()
                .connectionString(connectStr)
                .buildClient();
        cotainersList = blobServiceClient.listBlobContainers().stream().toList();
    }

    /**
     * Checks if a file exists at the given path.
     *
     * @param filePath the path to the file
     * @return true if the file exists, false otherwise
     */
    public static boolean fileExists(String filePath) {
        return Files.exists(Paths.get(filePath));
    }

    /**
     * Lists all blobs in each container in the storage account.
     */
    void listBlobs() {
        for (BlobContainerItem blobContainerItem : cotainersList) {
            BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(blobContainerItem.getName());
            containerClient.listBlobs().forEach(blobItem -> {
                System.out.println("This is the containerClient1 name: " + blobItem.getName());
            });
            System.out.println("This is the blob name: " + blobContainerItem.getName());
        }
    }
    /**
     * Uploads the activity file to the first container in the list.
     *
     * @throws IOException if an I/O error occurs
     */
    void uploadActivity() throws IOException {
        if (fileExists(FILE_PATH)) {
            cotainersList.stream().findFirst().ifPresent(blobContainerItem -> {
                BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(blobContainerItem.getName());
                containerClient.getBlobClient(blobContainerName + LocalDateTime.now()).uploadFromFile(FILE_PATH);
            });
            System.out.println("File uploaded");
        } else {
            System.out.println("File does not exist");
        }
    }
    /**
     * Runs the application, listing blobs and uploading the activity file.
     *
     * @param args command-line arguments
     * @throws Exception if an error occurs
     */
    @Override
    public void run(String... args) throws Exception {
        listBlobs();
        uploadActivity();
    }
}

The connectstr in the above code snippet can be substituted with this entry

All the code base at URL