How can I generate the url programatically (JAVA) for downloading a file from my azure blob storage?

Saurabh Patil 26 Reputation points
2021-08-31T13:08:30.017+00:00

Hello,

I am trying to generate a URL link to download the file uploaded to my container. I was able to generate the URL but when I used that URL it gave the following error.

<Error>
<Code>AuthenticationFailed</Code>
<Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. </Message>
<AuthenticationErrorDetail>Signature did not match.</AuthenticationErrorDetail>
</Error>

I am using the following code to generate the SAS token.

BlobContainerSasPermission blobContainerSasPermission = new BlobContainerSasPermission()
                .setReadPermission(true)
        BlobServiceSasSignatureValues builder = new BlobServiceSasSignatureValues(OffsetDateTime.now().plusDays(1), blobContainerSasPermission)
                .setProtocol(SasProtocol.HTTPS_ONLY);
        BlobClient client = new BlobClientBuilder()
                .connectionString("connection string")
                .blobName("")
                .buildClient();
        String blobContainerName = "test";
        return String.format("https://%s.blob.core.windows.net/%s?%s",client.getAccountName(), blobContainerName, client.generateSas(builder));

I also tried to use the following code to generate the SAS token:-
CloudStorageAccount account = CloudStorageAccount.parse(blobConnectionString);

     // Create a blob service client
     CloudBlobClient blobClient = account.createCloudBlobClient();

     CloudBlobContainer container = blobClient.getContainerReference(containerName);

     Date expirationTime = Date.from(LocalDateTime.now().plusDays(7).atZone(ZoneOffset.UTC).toInstant());
    SharedAccessBlobPolicy sharedAccessPolicy=new SharedAccessBlobPolicy();
    sharedAccessPolicy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ, 
        SharedAccessBlobPermissions.WRITE,SharedAccessBlobPermissions.ADD));
    sharedAccessPolicy.setSharedAccessStartTime(new Date());
    sharedAccessPolicy.setSharedAccessExpiryTime(expirationTime);

    String sasToken = container.generateSharedAccessSignature(sharedAccessPolicy, null);

But I got the same error everytime.
I went through all the solutions which I came across, but nothing seems to work. It would be great to receive some suggestions.

Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,529 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,192 questions
0 comments No comments
{count} votes

Accepted answer
  1. deherman-MSFT 38,021 Reputation points Microsoft Employee Moderator
    2021-09-01T18:08:12.37+00:00

    @Saurabh Patil
    Please try the code below and see if it works for you:

        String connString = "<storage account connection string >";  
        String containerName = "<container name>";  
        String blobName = "<file name>";  
    
        BlobServiceClient client = new BlobServiceClientBuilder().connectionString(connString).buildClient();  
        BlobClient blobClient = client.getBlobContainerClient(containerName).getBlobClient(blobName);  
    
        BlobSasPermission blobSasPermission = new BlobSasPermission().setReadPermission(true);                                             
                                                                                               
        OffsetDateTime expiryTime = OffsetDateTime.now().plusDays(1);   
        BlobServiceSasSignatureValues values = new BlobServiceSasSignatureValues(expiryTime, blobSasPermission)  
                        .setStartTime(OffsetDateTime.now());  
    
        System.out.println(blobClient.generateSas(values));  
    

    There is also a relevant thread on GitHub which you might find helpful. If you are still facing issues I recommend also posting your question on Stack Overflow so our developer community can also assist.

    Hope this helps. Please let me know if you need further assistance.

    -------------------------------

    Please don’t forget to "Accept the answer" and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.