Why does CloudBlockBlob.DownloadToStream always return an empty stream?

I have the following code:

public static void UploadStreamToBlob(Stream stream, string containerName, string blobName) { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName); blobContainer.CreateIfNotExists(); blobContainer.SetPermissions( new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName); long streamlen = stream.Length; <-- This shows 203 bytes blockBlob.UploadFromStream(stream); } 

and

 public static Stream DownloadStreamFromBlob(string containerName, string blobName) { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer blobContainer = blobClient.GetContainerReference(containerName); Stream stream = new MemoryStream(); CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(blobName); if (blockBlob.Exists()) { blockBlob.DownloadToStream(stream); long streamlen = stream.Length; <-- This shows 0 bytes stream.Position = 0; } return stream; } 

I run this on an Azure emulator that I pointed to my Sql server.

From what I can say, it seems that UploadFromStream is sending data correctly, however, if I try to start DownloadStreamFromBlob it will return a stream of length 0. BlockBlob.Exists returns true, so I assume it is there. I just can't understand why my thread is empty.

Btw, I passed the test and tested for containerName and blobName for both calls.

Any ideas?

+6
source share
1 answer

Ah, I realized that ...

The following lines:

 long streamlen = stream.Length; blockBlob.UploadFromStream(stream); 

need to change to

 long streamlen = stream.Length; stream.Position = 0; blockBlob.UploadFromStream(stream); 
+10
source

Source: https://habr.com/ru/post/951261/


All Articles