question

SaurabhPatil-3693 avatar image
0 Votes"
SaurabhPatil-3693 asked SaurabhPatil-3693 commented

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

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-accountsazure-blob-storage
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

deherman-MSFT avatar image
1 Vote"
deherman-MSFT answered SaurabhPatil-3693 commented

@SaurabhPatil-3693
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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@deherman-MSFT ,
The code snippet worked for me. Thanks for help.

1 Vote 1 ·