2
votes

I am trying to download Azure blob using azure data movement library.

I am facing the issue where the Azure source blob size is set to "Zero" when I try to download this source blob using the APIs downloadRange

The destination file is downloaded correctly and its size is correct. Am I missing anything?

I am using azure-storage java sdk version 8.6.5. Here is the sample code

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("myConnectionString");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("myContainer");
CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference("abc.txt");

And this is loop where I am reading in chunks. Destination file size is correct. I just cannot figure why the source blob size is set to zero ? This is not reproducible when entire contents are downloaded using API download

try (OutputStream out = new ByteBufferBackedOutputStream(buffer)) {
    cloudBlockBlob.downloadRange(position, (long) buffer.capacity(), out);
}...

Thanks in Advance!!

1

1 Answers

1
votes

If you just want to read a file in chunks by cloudBlockBlob.downloadRange, try code below:

import java.io.ByteArrayOutputStream;

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

public class lagencyStorgeSdk {

    public static void main(String[] args) throws Exception {
        CloudStorageAccount storageAccount = CloudStorageAccount.parse(
                "<storage account connection string>");
        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
        CloudBlobContainer container = blobClient.getContainerReference("<container name>");
        CloudBlockBlob cloudBlockBlob = container.getBlockBlobReference(".txt file name");
        cloudBlockBlob.downloadAttributes();
        long totalSize = cloudBlockBlob.getProperties().getLength();

        long readSize = 2;
        for (long i = 0; i < totalSize; i += readSize) {
            ByteArrayOutputStream downloadStream = new ByteArrayOutputStream();
            cloudBlockBlob.downloadRange(i, readSize, downloadStream);
            System.out.println("===>" + downloadStream);
        }
    }

}

content of my .txt file:

enter image description here

Result:

enter image description here

Let me know if you have any questions.