How to download Azure BLOB?

I am not able to download azure blob file to my local disk. Please find below the java code I am using to download the file. When I run and do a HTTP trigger test, I do not see the file downloaded into my Local path. Also, I have given the authentication as public access.And , I am able to read the contents of the txt file using the blob.downloadText(). But I am not able to download it to a file.

My requirement is to download a pdf in the Blob Storage to my Local Disk.

 @FunctionName("BlobDownload-Java")
    public void run1(
            @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION) HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {
        context.getLogger().info("Java HTTP trigger processed a request.");


        try {
            // Retrieve storage account from connection-string.
            CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

            // Get a reference to a container.
            // The container name must be lower case
            CloudBlobContainer container = blobClient.getContainerReference("doccontainer");


          CloudBlockBlob blob1 = container.getBlockBlobReference("AssembledDoc.pdf");

          context.getLogger().info("File Name Check 1 ----->" +  blob1.getName()); 


          CloudBlockBlob blob2 = container.getBlockBlobReference("Test.txt");
          String s= blob2.downloadText();
          context.getLogger().info("Text Document content ----->" + s );
          File file = new File("C:\\Users\\wb541348\\Project\\Temp.txt");
          blob2.downloadToFile(file.getAbsolutePath());

Upvotes: 1

Views: 6829

Answers (2)

Steve
Steve

Reputation: 1

@Prasanna, Your can't download the blob into your localpath because the pattern of your path is wrong, it should be "c:/Users/wb541348/Project/Temp.txt", there is another point to pay attention, that is the filename is needed, which means that path shouldn't be only folder, hope it benefits. you can also click this sample of azure blob for java sdk for reference.

Upvotes: 0

George Chen
George Chen

Reputation: 14334

If you just want to download PDF file to local, here is my test code , you could have a try.

@Test
public void downloadBlob(){


    try {
        CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

        //Create the service client object for credentialed access to the Blob service.
        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();

        // Retrieve a reference to a container.
        CloudBlobContainer container = blobClient.getContainerReference("blobcontainer");


        CloudBlob blob1 =container.getBlockBlobReference("thinking-in-java.pdf");

        blob1.download(new FileOutputStream("C:\\Users\\georgec\\Documents\\" + blob1.getName()));


    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (StorageException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } 

}

If you still have questions, please let me know.

Upvotes: 1

Related Questions