How can I download the blob from azure Storage account programatically (JAVA)?

Saurabh Patil 26 Reputation points
2021-09-03T08:10:17.843+00:00

Hello,

I want to download the whole blob and it's content from my storage account. How can I do it? I have read about doing it using AZCOPY via command line, but I want to do it using JAVA code. Is it possible and how?

What I want :-
Suppose, I have an storage account named x_strg_account. It has one container named y_container. In it I have stored multiple files like following:

  1. test/text/new.txt
  2. test/pdf/new.pdf
  3. test/img/new.img

Basically, test is the root folder. I want to download the "test" along with it's sub-folders and content. Any download format will work.

Please let me know if it is possible to achieve this programmatically (JAVA) and how ?

Thank You

Azure Container Instances
Azure Container Instances
An Azure service that provides customers with a serverless container experience.
757 questions
Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,535 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,200 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. deherman-MSFT 38,021 Reputation points Microsoft Employee Moderator
    2021-09-03T18:12:21.437+00:00

    @Saurabh Patil

    You might find the examples in the README and the samples folder for the repo helpful.

    The below code to download files from the container was provided by our service team:

      public static void main( String[] args ) throws IOException {  
            String containerName = "<containerName>";  
            String sasToken =  "<sasToken>";  
      
            BlobServiceClient storageClient = new BlobServiceClientBuilder()  
                .endpoint("https://storageaccount.blob.core.windows.net")  
                .sasToken(sasToken).buildClient();  
      
            BlobContainerClient blobContainerClient =  
                storageClient.getBlobContainerClient(containerName);  
      
            PagedIterable<BlobItem> blobs = blobContainerClient.listBlobs();  
            BlobClient blobClient = null;  
            for (BlobItem blobItem : blobs) {  
                blobClient = blobContainerClient.getBlobClient(blobItem.getName());  
      
                if (blobClient != null) {  
                    String filePath="D:\\temp\\" + blobItem.getName();  
                    FileUtils.forceMkdirParent(new File(filePath));  
                    blobClient.downloadToFile(filePath);  
                }  
            }  
        }  
    

    Hope this helps! Let us know if you need further assistance or have any questions.

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

    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.

    0 comments No comments

  2. Saurabh Patil 26 Reputation points
    2021-09-06T04:21:08.527+00:00

    @deherman-MSFT ,
    I tried to use the code but I am getting error, at sastoken. I have generated sastoken using the following code:

     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));  
    

    I want to know if this is the correct way for generating the sastoken for listing blobs because while generating the sastoken using this code we have to pass whole filepath as blob name. Do I need to modify it ? And what should be the changes?Also, when I used the sastoken generated by the above code, I got this error :-

    Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
    <AuthenticationErrorDetail>The specified signed resource is not allowed for the this resource level</AuthenticationErrorDetail></Error>

    Also, I am unable to comment on your answer.

    0 comments No comments

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.