Mayur
Mayur

Reputation: 676

how to get properties of a particular file from azure blob using Java?

I am using Java to test items present in the blob. I already have a list of files I am expecting to be present in the blob. Below is my maven dependency.

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-storage</artifactId>
    <version>1.2.0</version>
</dependency>

I am not getting proper reference/code sample to get property of a particular file from the blob. The only way it has is to list all files and identify the file you need. That is very costly operation.

I am referring to below docs.

https://learn.microsoft.com/en-us/azure/storage/storage-java-how-to-use-blob-storage#download-a-blob

Please help me with a code sample, showing how to download a particular file.

Upvotes: 1

Views: 3314

Answers (2)

Sonthosh B
Sonthosh B

Reputation: 11

This code make you retrieve a particular file from azure blob container.

use the below azure storage version maven dependency

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-storage</artifactId>
    <version>8.6.0</version>
</dependency>

Create a CloudBlobClient bean in the spring-boot configuration file..

package com.azure.config;

import java.net.URISyntaxException;
import java.security.InvalidKeyException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.blob.CloudBlobClient;

@Configuration
public class AzureBlobConfiguration {


    /**
     * Cloud blob client.
     *
     * @return the cloud blob client
     * @throws URISyntaxException the URI syntax exception
     * @throws InvalidKeyException the invalid key exception
     */
    @Bean
    public CloudBlobClient cloudBlobClient()throws URISyntaxException, InvalidKeyException {
        CloudStorageAccount storageAccount = CloudStorageAccount.parse("Blob-Connection-String-Value");
        return storageAccount.createCloudBlobClient();
    }

}

        

Use the below method downloadFile to download a particular file from the azure blob container and return file as byte array.

package com.azure.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;

import org.springframework.beans.factory.annotation.Autowired;

import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.CloudBlobClient;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.blob.CloudBlockBlob;

public class AzureBlob {

    @Autowired
    private CloudBlobClient cloudBlobClient;
    
    private byte[] downloadFile(String containerName, String filename)throws URISyntaxException, StorageException, IOException {
    
        CloudBlobContainer container = cloudBlobClient.getContainerReference(containerName);
        
        CloudBlockBlob cloudBlockBlob = container.getBlockBlobReference(filename);
    
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            cloudBlockBlob.download(outputStream);
            return outputStream.toByteArray();
        }   
    }
}

Upvotes: 1

Mayur
Mayur

Reputation: 676

Ok, I got a method as given below to make it work. There is no straightforward implementation for this. You first have to navigate to the directory and retrieve the file with an exact name.

/**
 * This method will help you retrieve properties of a file or a list of files from particular folder in Azure Blob storage
 * @param containerName - Pass the name of the container
 * @param path - Location of your file
 * @param fileName - You can pass complete file name to retrieve one file or you can pass prefix of the file.
 * @return It returns List<BlobProperties> if you pass complete file name, it will return one file or it will find file with supplied prefix.
 * @throws Exception
 */

public List<BlobProperties> retriveBlobFilesProperties(String containerName, String path, String fileName) throws Exception{
    List<BlobProperties> blobFilesProperties = new ArrayList<BlobProperties>();
    CloudStorageAccount cloudStorageAccount = CloudStorageAccount.parse(this.storageConnectionString);
    CloudBlobClient cloudBlobClient = cloudStorageAccount.createCloudBlobClient();
    CloudBlobContainer cloudBlobContainer = cloudBlobClient.getContainerReference(containerName);
    CloudBlobDirectory directory = cloudBlobContainer.getDirectoryReference(path);
    Iterable<ListBlobItem> blobItems = directory.listBlobs(fileName);
    for(ListBlobItem item : blobItems){
        CloudBlob blob = (CloudBlob)item;
        blobFilesProperties.add(blob.getProperties());
    }
    return blobFilesProperties;
}

src: https://softwaretestingboard.com/qna/2197/how-to-download-particular-file-from-azure-blob-using-java

Upvotes: 1

Related Questions