Uploading Image Blobs–Stream vs Byte Array (Year of Azure Week 4)

Ok, I promised you with when I started some of this Year of Azure project that some of these would be short. Its been a busy week so I’m going to give you a quickly based on a question posted on MSDN Azure forums.

The question centered around getting the following code snippit to work (I’ve paraphrased this a bit).

  1. MemoryStream streams = new MemoryStream();
  2.  
  3. // create storage account
  4. var account = CloudStorageAccount.DevelopmentStorageAccount;
  5. // create blob client
  6. CloudBlobClient blobStorage = account.CreateCloudBlobClient();
  7.  
  8. CloudBlobContainer container = blobStorage.GetContainerReference("guestbookpics");
  9. container.CreateIfNotExist(); // adding this for safety
  10.  
  11. string uniqueBlobName = string.Format("image_{0}.jpg", Guid.NewGuid().ToString());
  12.  
  13. CloudBlockBlob blob = container.GetBlockBlobReference(uniqueBlobName);
  14. blob.Properties.ContentType = "image\\jpeg";
  15.  
  16. System.Drawing.Image imgs = System.Drawing.Image.FromFile("waLogo.jpg");
  17.  
  18. imgs.Save(streams, ImageFormat.Jpeg);
  19.  
  20. byte[] imageBytes = streams.GetBuffer();
  21.  
  22. blob.UploadFromStream(streams);
  23.  
  24. imgs.Dispose();
  25. streams.Close();

Now the crux of the problem was that the resulting image in storage was empty (zero bytes). And Steve Marx correctly pointed out, the key thing missing is the resetting the buffer to position zero. So the corrected code would look like this. Note the addition of line 22. If fixes things just fine.

  1. MemoryStream streams = new MemoryStream();
  2.  
  3. // create storage account
  4. var account = CloudStorageAccount.DevelopmentStorageAccount;
  5. // create blob client
  6. CloudBlobClient blobStorage = account.CreateCloudBlobClient();
  7.  
  8. CloudBlobContainer container = blobStorage.GetContainerReference("guestbookpics");
  9. container.CreateIfNotExist(); // adding this for safety
  10.  
  11. string uniqueBlobName = string.Format("image_{0}.jpg", Guid.NewGuid().ToString());
  12.  
  13. CloudBlockBlob blob = container.GetBlockBlobReference(uniqueBlobName);
  14. blob.Properties.ContentType = "image\\jpeg";
  15.  
  16. System.Drawing.Image imgs = System.Drawing.Image.FromFile("waLogo.jpg");
  17.  
  18. imgs.Save(streams, ImageFormat.Jpeg);
  19.  
  20. byte[] imageBytes = streams.GetBuffer();
  21.  
  22. streams.Seek(0, SeekOrigin.Begin);
  23. blob.UploadFromStream(streams);
  24.  
  25. imgs.Dispose();
  26. streams.Close();

But the root issue I still have here is that the original code sample is pulling a byte array but not doing anything with it. But a byte array is still a valid method of uploading the image. So I reworked the sample a bit to support this..

  1. MemoryStream streams = new MemoryStream();
  2.  
  3. // create storage account
  4. var account = CloudStorageAccount.DevelopmentStorageAccount;
  5. // create blob client
  6. CloudBlobClient blobStorage = account.CreateCloudBlobClient();
  7.  
  8. CloudBlobContainer container = blobStorage.GetContainerReference("guestbookpics");
  9. container.CreateIfNotExist(); // adding this for safety
  10.  
  11. string uniqueBlobName = string.Format("image_{0}.jpg", Guid.NewGuid().ToString());
  12.  
  13. CloudBlockBlob blob = container.GetBlockBlobReference(uniqueBlobName);
  14. blob.Properties.ContentType = "image\\jpeg";
  15.  
  16. System.Drawing.Image imgs = System.Drawing.Image.FromFile("waLogo.jpg");
  17.  
  18. imgs.Save(streams, ImageFormat.Jpeg);
  19.  
  20. byte[] imageBytes = streams.GetBuffer();
  21. blob.UploadByteArray(imageBytes);
  22.  
  23. imgs.Dispose();
  24. streams.Close();

We pulled out the reset of the stream and replaced UploadFromStream and replaced it with UploadByteArray.

Funny part is that while both samples work, the resulting blobs are different size. And since these aren’t the only way to upload files, there might be other sizes available. But I’m short on time so maybe we’ll explore that a bit further another day . The mysteries of Azure never cease.

Next time!

10 Responses to Uploading Image Blobs–Stream vs Byte Array (Year of Azure Week 4)

  1. Pingback: Windows Azure and Cloud Computing Posts for 7/29/2011+ - Windows Azure Blog

  2. I searched around for a while and this is by far the simplest and most concise example of uploading to blob storage that I found. You definitely saved me a bunch of time getting a simple storage case up and running.

    Thanks Brent!

  3. ericsb says:

    Some time has passed since this post, but I wanted to take a moment to thank you for it. I found this post to be extremely helpful/useful.

  4. Kyryll says:

    Excellent post, helps people to find quick examples of every day operations:)

    About the different sizes:
    I don’t think that it has anything to do with Azure. I think if you look at the sizes of the byte arrays you are trying to save in both cases, they will be different. I predict that stream.GetBuffer() will return a bigger array then stream.ToArray() because GetBuffer() returns unused trailing bytes of the buffer.

  5. georgius says:

    thank you very much the solution works perfectly

  6. Pingback: Read a PDF file and return as stream from a WCF service? | PHP Developer Resource

  7. Sachin says:

    Thanks, It helped me a lot…

    – Sachin Magdum

  8. Lakky says:

    Thanks it helped lot of time 🙂

  9. Rakesh Reddy says:

    Thanks,it’s very useful to me.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.