DEV Community

Brijendra Rai
Brijendra Rai

Posted on

Write file to azure blob with plain java and maven

create maven project and add following dependency-

<dependencies>
            <dependency>
                <groupId>com.azure</groupId>
                <artifactId>azure-sdk-bom</artifactId>
                <version>1.2.10</version>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>com.azure</groupId>
                <artifactId>azure-core</artifactId>
                <version>1.37.0</version>
            </dependency>
            <dependency>
                <groupId>com.azure</groupId>
                <artifactId>azure-storage-blob</artifactId>
                <version>12.20.3</version>
            </dependency>

            <dependency>
                <groupId>com.azure</groupId>
                <artifactId>azure-identity</artifactId>
                <version>1.8.1</version>
                <scope>compile</scope>
            </dependency>
        </dependencies>
Enter fullscreen mode Exit fullscreen mode

Java Code to write file to azure blob:
Change connection string based on your account

package org.example;
import  com.azure.storage.blob.*;
import  com.azure.storage.blob.BlobServiceClient;
public class Main{
  public  static  void  mainwrite( String[] args )
    {
        String  connectStr = "DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=***;EndpointSuffix=core.windows.net";
        BlobServiceClient  blobServiceClient = new  BlobServiceClientBuilder().connectionString(connectStr).buildClient();
        String  containerName = "ads-automation";
        BlobContainerClient  containerClient = blobServiceClient.getBlobContainerClient(containerName);
        String  localPath = "abc.txt";
        BlobClient  blobClient = containerClient.getBlobClient("myalert2.docx");

        System.out.println("\nUploading to Blob storage as blob:\n\t" + blobClient.getBlobUrl());
        blobClient.uploadFromFile(localPath);
        System.out.println("Time Elapsed:");
    }
}```

Enter fullscreen mode Exit fullscreen mode

Top comments (0)