Understanding Blob Storage - Part Two - Streaming Data

It is very easy to upload or download data to containers in the Blob through the portal. If you select the container you can see an option to do the same. You can click on upload and browse for an item from your local machine to add the blob, and in the advanced option you can select the blob type as Blob, Page or Append and specify the block size.

Azure 

Once uploaded, the image will be listed in the container and on selecting you will be able to download the file through the portal and also you can get the URL to access the file for the public. If you scroll down you can see an option to add metadata also.

Azure 

Now, here in this article, we will see how to download the existing image that we uploaded, upload a new image through a C# application and also list all the files in a container. Either you can work with the previous exercise or you can start a new project. If you are working with previous you only need to add new code and if you are working on a new project, you are required to do the connection string settings and adding the namespaces step.

Step 1

In this step, we will download the file that we just added (Make sure you are having the image in the container) to downloads folder on our C drive. Append the following code to your previous exercise in the Main method.

  1. CloudBlockBlob blockBlob2 = container.GetBlockBlobReference("img1.jpeg");  
  2.   
  3. using (var fileStream = System.IO.File.OpenWrite(@"c:\Downloads\img1.jpeg"))  
  4. {  
  5.    blockBlob2.DownloadToStream(fileStream);  
  6. }  

Azure 

Step 2

Run your application and you can see the file downloaded in the folder specified. (Make sure you have created a Downloads folder prior to running your application, else will throw an exception).
 
Azure 

Step 3

In this step I will upload a new file to the container from the same folder. For that, I have already added a new file named img2.jpeg to the same folder and append the following code to your main method.

  1. CloudBlockBlob blockBlob = container.GetBlockBlobReference("img2.jpeg");  
  2.   
  3. using (var fileStream = System.IO.File.OpenRead(@"c:\Downloads\img2.jpeg"))  
  4. {  
  5.    blockBlob.UploadFromStream(fileStream);  
  6. }  
Azure

Step 4

Run your application and if you check the container in the portal you can see a new image added

Azure 

Step 5

Now in this step we will write some code to list all the blobs that are up there in that container. As the files are of image type we will list the URI for the images. Append the following code to the main method.

  1. var blobs = container.ListBlobs();  
  2.   
  3. foreach (var blob in blobs)  
  4. {  
  5.    Console.WriteLine(blob.Uri);  
  6. }  
  7.   
  8. Console.ReadKey();  
Azure

Step 6

Run the application and you can see the URIs of our two images in the container listed

Azure


Similar Articles