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:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Windows.Forms;
- using Microsoft.WindowsAzure;
- using Microsoft.WindowsAzure.ServiceRuntime;
- using Microsoft.WindowsAzure.StorageClient;
-
- namespace BlogStorageTest
- {
- public partial class Form1 : Form
- {
-
- private CloudBlobClient _BlobClient = null;
- private CloudBlobContainer _BlobContainer = null;
-
- public Form1()
- {
- InitializeComponent();
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- // Setup the connection to Windows Azure Storage
- var storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=<YOURACCOUNTNAME>;AccountKey=<YOURACCOUNTKEY>");
- _BlobClient = storageAccount.CreateCloudBlobClient();
-
- // For large file copies you need to set up a custom timeout period
- // and using parallel settings appears to spread the copy across multiple threads
- // if you have big bandwidth you can increase the thread number below
- // because Azure accepts blobs broken into blocks in any order of arrival.
- _BlobClient.Timeout = new System.TimeSpan(1,0,0);
- _BlobClient.ParallelOperationThreadCount = 2;
-
- // Get and create the container
- _BlobContainer = _BlobClient.GetContainerReference("publicfiles");
- _BlobContainer.CreateIfNotExist();
-
- // Setup the permissions on the container to be public
- var permissions = new BlobContainerPermissions();
- permissions.PublicAccess = BlobContainerPublicAccessType.Container;
- _BlobContainer.SetPermissions(permissions);
-
- // Make a unique blob name
- string path = "E:\\FIlms\\Edible\\Cerne_Egg_Project.wmv";
- string extension = System.IO.Path.GetExtension(path);
- string filename = System.IO.Path.GetFileName(path);
-
- //Open the stream and read it back.
- using (FileStream fs = File.OpenRead(path))
- {
- // Create the Blob and upload the file
- var blob = _BlobContainer.GetBlobReference(Guid.NewGuid().ToString() + "/" + filename);
- blob.UploadFromStream(fs);
-
- // Set the metadata into the blob
- blob.Metadata["FileName"] = filename;
- blob.Metadata["Submitter"] = "Automated Encoder";
- blob.SetMetadata();
-
- // Set the properties
- blob.Properties.ContentType = "video/x-ms-wmv";
- blob.SetProperties();
- }
-
- }
- }
- }
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.