title: Why is CloudBlockBlob.DownloadToStream always returning an empty stream?
body: <p>I have the following code:</p>
  
  <pre><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;  &lt;-- This shows 203 bytes
      blockBlob.UploadFromStream(stream);        
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>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;  &lt;-- This shows 0 bytes
          stream.Position = 0;          
      }
  
      return stream;
  }
  </code></pre>
  
  <p>I'm running this in the Azure emulator, which I have pointed to my Sql Server.</p>
  
  <p>From what I can tell, it appears that the UploadFromStream is sending the data correctly, however, if I try and run the DownloadStreamFromBlob, it returns a 0 length stream.  The blockBlob.Exists is returning true, so I assume it's there.  I just can't figure out why my stream is empty.</p>
  
  <p>Btw, I'm passing in test and test for containerName and blobName on both calls.</p>
  
  <p>Any ideas?</p>