Rahul Khandelwal
Rahul Khandelwal

Reputation: 104

Download partial file from Azure storage

I have files which are big in size. Since the file size is too large, I have been trying to download partial file from Azure blob storage. I have been using DownloadRangeToStream method from class CloudBlockBlob and it works perfectly fine for me.

Now we are planning to use Azure.Storage.Blobs.Specialized.BlockBlobClient for upload and there is not similar method in BlockBlobClient class which download file in range or blob block (Not the whole file but a block of it)

I was wondering if there is any other way to do the same in more optimized way.!!

Upvotes: 2

Views: 2370

Answers (4)

Daniel W.
Daniel W.

Reputation: 1198

As the length that blockBlobClient.Download(httpRange); only returns 3118 bytes (may be some http limit) at one I got another solution. I download to a stram and set the position of the stream:

public Stream DownloadRange(Stream outputStream, Guid containerName, Guid blobName, long offset, long length)
{
    var blockBlobClient = new BlockBlobClient(
                                    _configurationKeys.StorageConnectionString
                                    ,containerName.ToString(), 
                                    ,blobName.ToString());

    var buffer = new byte[length];
    using (Stream s = blockBlobClient .OpenRead())
    {
        s.Position = offset;
        var pos = 0;
        while(pos < length) {
            var len += s.Read(buffer, 0, length);
            outputStream.Write(buffer, 0, len);
            pos += len;
        }
        return outputStream;
    }

Upvotes: 0

Rahul Khandelwal
Rahul Khandelwal

Reputation: 104

Here is the code change to download big files in chunks (For Azure SKD v12). All you have to do is call this method by passing your start point and length (or end point)

    public Stream DownloadRange(Stream outputStream, Guid containerName, Guid blobName, long offset, long length)
{
    var blockBlobClient = new BlockBlobClient(
                                    _configurationKeys.StorageConnectionString
                                    ,containerName.ToString(), 
                                    ,blobName.ToString());

    var httpRange = new Azure.HttpRange(offset, length);
    var output = blockBlobClient.Download(httpRange);
    output.Value.Content.CopyTo(outputStream);
    return outputStream;
}

Upvotes: 2

Gaurav Mantri
Gaurav Mantri

Reputation: 136136

The method you would want to use is Download(HttpRange, BlobRequestConditions, Boolean, CancellationToken) which is in BlobBaseClient class (BlockBlobClient derives from this class).

HttpRange parameter of this method is where you would define the offset and length.

Upvotes: 0

Gellio Gao
Gellio Gao

Reputation: 843

Check the detail of the class BlockBlobClient. To perform a partial update of the content of a block blob, use the StageBlock(String, Stream, Byte[], BlobRequestConditions, IProgress, CancellationToken) and CommitBlockList(IEnumerable, BlobHttpHeaders, IDictionary<String,String>, BlobRequestConditions, Nullable, CancellationToken) operations.

Hope this can help you.

Upvotes: 0

Related Questions