title: Uploading to azure asynchronously directly from a stream
body: <p>I want to upload to my azure local development account directly using a stream. I have created a class for azure blob storage.</p>
  
  <pre><code>public class AzureBlob
  {
      delegate void UploadFinished(IAsyncResult result);
      public void uploadFile()
      {
          //Initial configuration
          CloudStorage account = CloudStorageAccount.DevelopmentStorageAccount;
          CloudBlobClient client = account.CreateBlobClient();
          CloudBlobContainer container = client.GetContainerReference("myfiles");
          Stream stream = new MemoryStream();
  
          //Upload to azure
          CloudBlob blob = container.GetBlobReference("sample.txt");
          UploadFinished uploadFinished = delegate(IAsyncResult result)
          {
              Console.WriteLine("Upload finished {0} {1}", result.IsCompleted, stream.Position);
          };
          blob.BeginUploadFromStream(stream, new AsyncCallback(uploadFinished));
  
          //Write to stream
          for(int i=0;i&lt;100;i++)
          {
              for(int j=0;j&lt;50;j++)
              {
                  stream.WriteByte(65);
              }
          }
          stream.Close();
  
  
      }
  }
  </code></pre>
  
  <p>The first problem that i face is i get the file in the storage but it contains no data. Even if i use EndOfUploadStream method in the callback (which according to some stackoverflow answer is a solution).
  Secondly, i put the break point inside the callback and i found that the callback gets executed once, before i close the stream, and the program keeps updating the stream. At this point the stream position is around 913(mostly random). 
  Please help me in uploading the blob directly through the stream asynchronously.</p>