I would be very grateful if anybody has experience with the function DownloadRangeToStream.
Here they say that the parameter "length" is the length of the data, but in my experience it is the upper position of the segment to download, e.g. "length" - "offset" = real length of the data.
I would also really appreciate if anybody could give me some code for downloading a blob in chunks, since the function mentioned before doesn't seem to work.
Thank you for any help
You can download blobs and directories from Blob storage by using the AzCopy v10 command-line utility. To see examples for other types of tasks such as uploading files, synchronizing with Blob storage, or copying blobs between accounts, see the links presented in the Next Steps section of this article.
In order to download an Azure BLOB Storage item by its URL, you need to instantiate a CloudBlockBlob yourself using the item's URL: var blob = new CloudBlockBlob(new Uri(pdfFileUrl), cloudStorageAccount. Credentials); This blob can then be downloaded with the code you originally posted.
Copy containers, directories, and blobs Copy all containers, directories, and blobs to another storage account by using the azcopy copy command. This example encloses path arguments with single quotes (''). Use single quotes in all command shells except for the Windows Command Shell (cmd.exe).
Try this code. It downloads a large blob by splitting it in 1 MB chunks.
static void DownloadRangeExample()
{
var cloudStorageAccount = CloudStorageAccount.DevelopmentStorageAccount;
var containerName = "container";
var blobName = "myfile.zip";
int segmentSize = 1 * 1024 * 1024;//1 MB chunk
var blobContainer = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference(containerName);
var blob = blobContainer.GetBlockBlobReference(blobName);
blob.FetchAttributes();
var blobLengthRemaining = blob.Properties.Length;
long startPosition = 0;
string saveFileName = @"D:\myfile.zip";
do
{
long blockSize = Math.Min(segmentSize, blobLengthRemaining);
byte[] blobContents = new byte[blockSize];
using (MemoryStream ms = new MemoryStream())
{
blob.DownloadRangeToStream(ms, startPosition, blockSize);
ms.Position = 0;
ms.Read(blobContents, 0, blobContents.Length);
using (FileStream fs = new FileStream(saveFileName, FileMode.OpenOrCreate))
{
fs.Position = startPosition;
fs.Write(blobContents, 0, blobContents.Length);
}
}
startPosition += blockSize;
blobLengthRemaining -= blockSize;
}
while (blobLengthRemaining > 0);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With