Java Quickstart: Create and manage blobs with Azure Blob Storage

This quickstart guide shows you how to use the Azure Blob Storage client library for Java to programmatically create a container, upload a blob, download a blob, and list the blobs in a container.

Prerequisites:
  • An Azure account with an active subscription.
  • A storage account.
  • Java Development Kit (JDK) installed.
  • Maven installed.

1. Set up your Java project

Create a new Maven project or use an existing one. Add the Azure Blob Storage client library for Java to your project's `pom.xml` file:

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-storage-blob</artifactId>
    <version>12.25.0</version><!-- Check for the latest version -->
</dependency>
<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-identity</artifactId>
    <version>1.11.0</version><!-- For authentication -->
</dependency>

2. Get your connection string

You can find your storage account connection string in the Azure portal under your storage account's "Access keys" section.

Important: Treat your connection string like a sensitive credential. Avoid hardcoding it directly into your application code. Consider using environment variables or Azure Key Vault.

3. Write the code

Create a new Java class (e.g., `BlobQuickstart.java`) and add the following code. Replace placeholders with your actual connection string and container name.

// Paste the full Java code here. This is a placeholder.
// Example:

import com.azure.storage.blob.BlobClient;
import com.azure.storage.blob.BlobClientBuilder;
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.models.BlobItem;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class BlobQuickstart {

    public static void main(String[] args) throws IOException {
        // Retrieve the connection string for your storage account
        String connectionString = System.getenv("AZURE_STORAGE_CONNECTION_STRING");
        // Or replace with your actual connection string (not recommended for production)
        // String connectionString = "YOUR_CONNECTION_STRING"; 

        String containerName = "javademo-container";
        String blobName = "sample-blob.txt";
        String blobContent = "Hello from Azure Blob Storage!";
        String downloadFilePath = "downloaded-sample-blob.txt";

        // Create a BlobServiceClient object using the connection string
        BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
            .connectionString(connectionString)
            .buildClient();

        // Get a BlobContainerClient representing the container
        BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);

        System.out.println("Creating container: " + containerName);
        // Create the container if it doesn't exist
        containerClient.createIfNotExists();

        // Get a BlobClient for a specific blob
        BlobClient blobClient = containerClient.getBlobClient(blobName);

        System.out.println("Uploading blob: " + blobName);
        // Upload content to the blob
        blobClient.upload(new ByteArrayInputStream(blobContent.getBytes()), blobContent.length());
        System.out.println("Blob uploaded successfully.");

        System.out.println("Downloading blob: " + blobName);
        // Download the blob to a file
        try (OutputStream outputStream = new FileOutputStream(downloadFilePath)) {
            blobClient.downloadStream(outputStream);
        }
        System.out.println("Blob downloaded to: " + downloadFilePath);

        System.out.println("Listing blobs in container: " + containerName);
        // List all blobs in the container
        for (BlobItem blobItem : containerClient.listBlobs()) {
            System.out.println(" - " + blobItem.getName());
        }

        System.out.println("Finished.");
    }
}

4. Run the application

Before running, ensure you have set the `AZURE_STORAGE_CONNECTION_STRING` environment variable. Then, compile and run your Java application using Maven:

mvn clean install
mvn exec:java -Dexec.mainClass="BlobQuickstart"

This will create a container, upload a sample blob, download it back, and list the blobs in the container.

Next Steps