in

Copying files to Windows Azure Blob Storage

I’m compelled to share with you my simple program for copying files into Windows Azure blob storage. Now you may already have this sussed – it isn’t that cutting edge – but when I searched for examples I found such a range of out dated Azure material, and no simple 101 solution that I had to pull this together. I did pull it from a webrole example I found somewhere (think is is the additional examples download) but I still had to add bits to make it work.

So here is the single button windows form file copy to Azure blob storage example:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.IO;
  9. using System.Windows.Forms;
  10. using Microsoft.WindowsAzure;
  11. using Microsoft.WindowsAzure.ServiceRuntime;
  12. using Microsoft.WindowsAzure.StorageClient;
  13.  
  14. namespace BlogStorageTest
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.  
  19.         private CloudBlobClient _BlobClient = null;
  20.         private CloudBlobContainer _BlobContainer = null;
  21.  
  22.         public Form1()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         private void button1_Click(object sender, EventArgs e)
  28.         {
  29.             // Setup the connection to Windows Azure Storage
  30.             var storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=<YOURACCOUNTNAME>;AccountKey=<YOURACCOUNTKEY>");
  31.             _BlobClient = storageAccount.CreateCloudBlobClient();
  32.  
  33.             // For large file copies you need to set up a custom timeout period
  34.             // and using parallel settings appears to spread the copy across multiple threads
  35.             // if you have big bandwidth you can increase the thread number below
  36.             // because Azure accepts blobs broken into blocks in any order of arrival.
  37.             _BlobClient.Timeout = new System.TimeSpan(1,0,0);
  38.             _BlobClient.ParallelOperationThreadCount = 2;
  39.  
  40.             // Get and create the container
  41.             _BlobContainer = _BlobClient.GetContainerReference("publicfiles");
  42.             _BlobContainer.CreateIfNotExist();
  43.  
  44.             // Setup the permissions on the container to be public
  45.             var permissions = new BlobContainerPermissions();
  46.             permissions.PublicAccess = BlobContainerPublicAccessType.Container;
  47.             _BlobContainer.SetPermissions(permissions);
  48.  
  49.             // Make a unique blob name
  50.             string path = "E:\\FIlms\\Edible\\Cerne_Egg_Project.wmv";
  51.             string extension = System.IO.Path.GetExtension(path);
  52.             string filename = System.IO.Path.GetFileName(path);
  53.  
  54.             //Open the stream and read it back.
  55.             using (FileStream fs = File.OpenRead(path))
  56.             {
  57.                 // Create the Blob and upload the file
  58.                 var blob = _BlobContainer.GetBlobReference(Guid.NewGuid().ToString() + "/" + filename);
  59.                 blob.UploadFromStream(fs);    
  60.                 
  61.                 // Set the metadata into the blob
  62.                 blob.Metadata["FileName"] = filename;
  63.                 blob.Metadata["Submitter"] = "Automated Encoder";
  64.                 blob.SetMetadata();
  65.  
  66.                 // Set the properties
  67.                 blob.Properties.ContentType = "video/x-ms-wmv";
  68.                 blob.SetProperties();
  69.             }
  70.  
  71.         }
  72.     }
  73. }

Key things to note:

References for the three Microsoft.WindowsAzure namespaces installed with the Windows Azure SDK and tools.

You need to provide your Account name and the primary account key. The account name is found here http://accountNAME.blob.core.windows.net/ for your account.

The _BlobClient.Timeout was critical because otherwise my upload would timeout.

There aren’t any directories in Azure blob storage. Well there is. You can achieve a Container\directory\blob setup by defining everything you need in the path below the container specification in the GetBlobReference call. I generate a new guid which I use as my directory name, it allows me to keep the submitted video files original name without risk of duplication – which helps me with my human limitations for finding and identifying stuff :-)

Thanks to Simon Davies for explaining this stuff to me.

Digg This

» Trackbacks & Pingbacks

  1. I’ve been working with UK XNA MVP Charles Humphrey on project ZuneRay. ZuneRay is a raycasting engine
    wotudo — March 25, 2010 11:11 AM
  2. Pingback from  Azure uploading big files into blob storage &#8211; travio

  3. ... - חשוב לציין שטיפולי בוטוקס לא יותר מסוכנים מלצרוך כדור אקמול מילוי קמטים בשומן עצמי מחירים. חברות הקוסמטיקה שמזמן זיהו את הביקוש למוצרי אנטי-אייגינג למיניהם עטות על הלקוחות בהיצע... Copying files to Windows Azure Blob Storage - wotudo ...

    מילוי קמטים קולגן — April 17, 2014 8:41 AM
  4. Copying files to Windows Azure Blob Storage - wotudo

    x sperm — October 22, 2014 4:18 PM
  5. Copying files to Windows Azure Blob Storage - wotudo

    pre workout drink — October 25, 2014 5:31 PM
  6. ... עבודה באוסטרליה לא מכירות - אחרי עבודה בחו&quot;ל גם אם גנבו לכם את הדברים להזמין דילים לחו&quot;ל ולהתחיל בחיים חדשים במזרח, לפעמים תזיעו קצת עבודה באוסטרליה לצעירים. אלא להגשים ולחוות באופן אישי שיחה חיו... Copying files to Windows Azure Blob Storage

    לעבוד במכרות באוסטרליה — October 30, 2014 4:30 PM
Trackback url for this post:
http://wotudo.net/blogs/wotudo/trackback.ashx?PostID=834

» Comments

  1. I always enjoy learning what other people think about Microsoft Azure and how they use it. Check out my very own tool CloudBerry Explorer that helps managing Azure Blog Storage . It is a freeware. http://azure.cloudberrylab.com/
    Andy — February 23, 2010 4:28 PM
  2. Thanks dude, your tip on .Timeout and ParallelOperationThreadCount  just saved me a lot of frustration

    thanks!

    P — March 16, 2010 3:00 PM
  3. hey your code works!but i was wondering how would you modiy the code so that it can read a file from a textbox and upload it to the cloud.i tried changing th string path to  string path = textBox1.Text but it doesnt do the trick,any help would be excelltn thanks guys

    * i have a browse button which opens up a dialog which i can select my file and read it's path into the textbox,how do i call it in the string path section?

    jenvin — May 10, 2010 3:47 PM
  4. Great code, But i also having the same problem as jenvin.
    Lynn — May 19, 2010 5:15 AM
  5. hi, this is good, so next step is how can i download files from the azure?? please help me write the code
    mardan0126 — July 6, 2010 12:28 PM
  6. hi, this code does not work , it does not recognize Microsoft.WindowsAzure;

    using Microsoft.WindowsAzure.ServiceRuntime;

    using Microsoft.WindowsAzure.StorageClient;

    i added reference , but still did not work, it said the namespace are have dependency with system.web.  what can i do to run this code correctly ?

    need ur help,  many thanks

    martin_m — July 10, 2010 5:29 PM
  7. Ya. Same problem. Cannot use using Microsoft.WindowsAzure.ServiceRuntime; using Microsoft.WindowsAzure.StorageClient;
    Vadya — March 23, 2011 6:06 AM
  8. Thank you so much, you're right there's a lot of junk out there. This saved me so much time. Hey martin_m you need to check your targeted framework. I had the same problem.
    DougStiers — March 25, 2011 6:58 PM

» Leave a Comment

(required) 
(optional)
(required) 

Submit