Issue
I'm new to azure ,I want to upload a file in azure using java sdk and met with error.
Here is my approach,
BlobServiceClient client = new BlobServiceClientBuilder()
.connectionString(connectStr)
.buildClient();
BlobContainerClient blobContainerClient = blobServiceClient.createBlobContainer(containerName);
String localPath = "./data/";
String fileName = "quickstart" + java.util.UUID.randomUUID() + ".txt";
BlobClient blobClient = blobContainerClient.getBlobClient(fileName);
FileWriter writer = null;
try
{
writer = new FileWriter(localPath + fileName, true);
writer.write("Hello, World!");
writer.close();
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
}
blobClient.uploadFromFile(localPath + fileName);
exception,
Exception in thread "main" java.lang.illegalArgumenatation:Input byte array has wrong 4-byte ending unit at java.base/java.util.Base64$Decoder.decode0(Base64.java:837)
Kindly help with this?
Solution
I tried in my environment and successfully uploaded files in azure blob storage.
package com.blobs.quickstart;
import com.azure.storage.blob.*;
import com.azure.storage.blob.BlobServiceClient;
public class App
{
public static void main( String[] args )
{
String connectStr = "<Connection string>";
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient();
String containerName = "test";
BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(containerName);
String localPath = "C:\\Users\\v-vsettu\\Documents\\Venkat\\barcode.docx";
BlobClient blobClient = containerClient.getBlobClient("barcode.docx");
System.out.println("Blob uploaded");
blobClient.uploadFromFile(localPath);
}
}
Console:
Portal:
Initially, I got an same error :
Because when I pass wrong connection string or access key in the code I got an error. Make sure you are connection string is in correct.
You can get the connection string from portal:
Reference: Quickstart: Azure Blob Storage library - Java | Microsoft Learn
Answered By - Venkatesan
Answer Checked By - Clifford M. (JavaFixing Volunteer)