I am trying to use java SDK in order to connect to Azure Blob. On the method create if not exists I constantly get a general error.

StorageCredentials storageCredentials = new StorageCredentialsAccountAndKey(getShareAccount(), Base64.encode(getShareKey().getBytes()));
CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
CloudBlobClient c = storageAccount.createCloudBlobClient();
CloudBlobContainer container = c.getContainerReference(containerName);
container.createIfNotExists();

i get the following error Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.

when I try to

container.create();

I receive: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. RequestId:.... Time:2024-02-09T09:16:26.4410074Z

I tried to change my local machine time to GMT, but the error persists. My Azure blob is in eastus2, tried to change my local machine time to that timezone but it did not work.

Do you know what is the problem?

1

There are 1 best solutions below

0
Venkatesan On

I tried to change my local machine time to GMT, but the error persists. My Azure blob is in eastus2, tried to change my local machine time to that timezone but it did not work.

Do you know what is the problem?

To create a container in Azure blob storage you can use the below java code.

Code:

import com.azure.storage.blob.*;
import java.io.*;

public class App
{
    public static void main(String[] args) throws IOException
    {
        String connectStr="<Connection-string of your storage account>";
        BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
        .connectionString(connectStr)
        .buildClient();
        String containerName = "sample";
        BlobContainerClient blobContainerClient = blobServiceClient.createBlobContainer(containerName);
        System.out.println("The container is created!!!!");
    }
} 

The above code creates a new Azure Blob Storage container using the Azure Storage Blob SDK for Java. It first creates a BlobServiceClient object using the connection string of your storage account and then creates a new container with the specified name using the createBlobContainer method of the BlobServiceClient object. The code then prints a message to the console indicating that the container has been created.

Pom.xml

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-storage-blob</artifactId>
    <version>12.25.1</version>
</dependency>

Output:

 The container is created!!!!
 

enter image description here

Portal: enter image description here

Reference:

Quickstart: Azure Blob Storage library - Java | Microsoft Learn