1

I want to download blob as byte array, but above mention error occurs. my code is as follows

Dim fullFileBytes() As Byte = {}
Dim objAzureStorage As New AzureCloudStorage

Dim fullImageBlob As Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob = objAzureStorage.CloudContainer.GetBlockBlobReference(row(0))

fullImageBlob.DownloadToByteArray(fullFileBytes, 0)
Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
SajidBp
  • 63
  • 2
  • 14
  • Does this answer your question? [Azure Blob storage: DownloadToByteArray VS DownloadToStream](https://stackoverflow.com/questions/24312527/azure-blob-storage-downloadtobytearray-vs-downloadtostream) – Michael Freidgeim Nov 06 '21 at 01:34

1 Answers1

8

Since I have not worked with VB.Net, let me provide an answer in C#. Essentially what I have done is read the blob's contents in memory stream and then converted that to a byte array and return that array.

    private static byte[] ReadBlobInByteArray()
    {
        var cred = new StorageCredentials(accountName, accountKey);
        var account = new CloudStorageAccount(cred, true);
        var container = account.CreateCloudBlobClient().GetContainerReference("container-name");
        var blob = container.GetBlockBlobReference("blob-name");
        using (var ms = new MemoryStream())
        {
            blob.DownloadToStream(ms);
            return ms.ToArray();
        }
    }
Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • that I have done,but when i use like this cblobFull.BeginDownloadToByteArray() ,its give error that I have mention above. – SajidBp Apr 07 '17 at 10:25
  • 1
    The reason you're getting this error is because byte array's size can't be changed. If you wish to read directly in a byte array, first you have to fetch blob's properties which will give you blob's length. Then you would create a byte array of that size and then read the blob's contents in that byte array. This is not recommended because you're making 2 network calls: one to read blob's properties and two to actually download the blob. In the approach I have mentioned, you only make one network call. HTH. – Gaurav Mantri Apr 07 '17 at 10:49