27 May 2012

Azure Flavor for the Sharepoint Media Component

Some time ago I wrote about a Media Processing Component for Sharepoint that I was working on. It is a Media Assets list for Sharepoint that lets you choose where to store the blob files. It provides also intelligence for encoding videos, generating thumbnail and poster images, obtaining media metadata, etc. On that first post the component was explained in detail, with the original 3 storage flavors: Sharepoint list, Virtual Directoy or FTP. The storage manager is extensible, so a new flavor was added later that enabled to store the media files on the cloud, in particular to AmazonS3. Today I’m going to talk about a new cloud flavor based on Azure Blob Storage.

Windows Azure Blob storage is a service for storing large amounts of unstructured data that can be accessed from anywhere in the world via HTTP or HTTPS. This kind of storage is special for storing large amounts of unstructured text or binary data such as video, audio and images. You can find documentation here on how to create a Windows Azure Storage Account and access it programmatically.

For the Media Processing Component I just needed to create a class (AzureStorageManager) that implemented the IAssetStorageManager interface. The interface has the methods to save and delete files from the external storage. It also needs four configuration parameters to be passed to its constructor:

  • AccountName: Name of the account that will be used to authenticate to the Blob Storage
  • AccountKey: Key that will be used to authenticate to the Blob Storage
  • BlobEndpoint: URL of the Blob endpoint, like http://someaccount.blob.core.windows.net
  • Container: Name of a container to store your application’s media files.

For example, this is the code required to upload a file to an Azure Blob Storage:

Code Snippet AzureStorageManager class - Save method
  1. public string Save(System.IO.FileInfo file)
  2. {
  3. //Create service client for credentialed access to the Blob service.
  4. CloudBlobClient blobClient = new CloudBlobClient(blobEndpoint, new StorageCredentialsAccountAndKey(accountName, accountKey));
  5. //Get a reference to a container, which may or may not exist.
  6. CloudBlobContainer container = blobClient.GetContainerReference(containerAddress);
  7. //Create a new container, if it does not exist
  8. container.CreateIfNotExist();
  9. //Get a reference to a blob, which may or may not exist.
  10. CloudBlob blob = container.GetBlobReference(file.Name);
  11. //Upload content to the blob, which will create the blob if it does not already exist.
  12. blob.UploadFile(file.FullName);
  13. return blob.Uri.ToString();
  14. }

You can download the complete code for the Media Component with all the storage options from gitgub.