First things first, lets get a file into Windows Azure Media Services so that we can leverage all that cloud power.
Today we look at the following steps using the Media Services Client .Net SDK:
- Getting a CloudMediaContext object.
- Creating an Asset
That’s it, two easy steps.
But before you learn to walk, there’s a bit of crawling to do. You need to have three bits of software installed to build against the Media Services .Net SDK:
WCF Data Services 5.0
Reference: C:\Program Files (x86)\Microsoft WCF Data Services\5.0\bin\.NETFramework\Microsoft.Data.Edm.dll C:\Program Files (x86)\Microsoft WCF Data Services\5.0\bin\.NETFramework\Microsoft.Data.OData.dll C:\Program Files (x86)\Microsoft WCF Data Services\5.0\bin\.NETFramework\Microsoft.Data.Services.dll C:\Program Files (x86)\Microsoft WCF Data Services\5.0\bin\.NETFramework\Microsoft.Data.Services.Client.dll C:\Program Files (x86)\Microsoft WCF Data Services\5.0\bin\.NETFramework\System.Spatial.dll Download from: http://www.microsoft.com/en-us/download/details.aspx?id=29306
Azure Storage Client
Reference: C:\Program Files\Windows Azure SDK\v1.6\bin\Microsoft.WindowsAzure.StorageClient.dll Download from WebPI: http://go.microsoft.com/fwlink/?linkid=255386 On the Products tab select All, then find and install the Windows Azure SDK 1.6 for Visual Studio 2010 (November 2011). Yes, we ARE moving toward 1.7 SP1.
Media Services SDK
Reference: C:\Program Files (x86)\Microsoft SDKs\Windows Azure Media Services\Services\v1.0\Microsoft.WindowsAzure.MediaServices.Client.dll Download from: http://www.microsoft.com/en-us/download/details.aspx?id=30153
Here is a little bit more about each step of building that first media asset:
1. What is a CloudMediaContext?
The .Net SDK does a number of things for you so you don’t need to be an expert at making REST calls. In addition to surfacing all the REST functionality, it also simplifies things by managing connections, redirects, uploads, downloads, provides storage encryption prior to uploads, securely transfers configuration information and keys, it gives you the ability to enumerate your media objects:
- Assets and their associated objects: Files, Locators, ContentKeys
- Jobs and their associated objects: Tasks, Configurations, Assets
- AccessPolicies and Media Processors
To make all this happen, the CloudMediaContext creates a state which holds your user credentials, connectivity information, and the state or data of some of the recently queried objects. It is important to understand that this state or data can become stale if other threads or the Media Services act upon the objects. In some cases, you can simply re-query the data, in others, you are best to discard the Context and request a new one.
To get a CloudMediaContext, you need your Media Services credentials. You can retrieve these using the Azure Management Portal. In this blog post, you created your account. Now lets go back and get the credentials you need to use it: login to the Azure Management Portal.
Click on the Media Services icon on the far left. Choose your Media Service account, in my case ndrouineast, and click Manage Keys at the bottom.
Take note of the Media Service name and the Primary Media Service Access Key, you will need those in the simple call:
CloudMediaContext context = new CloudMediaContext( "ndrouineast", "H6sdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdfsdff4=");
Now that you have the context, you can do all sorts of things.
2. Create a new Asset using context.AssetCollection
Lots of ways to create an asset:
- Create a new empty asset then add files to it.
- Use some of the convenience features of the SDK to both create and upload at once.
- Run a Job in which a Task creates an Asset as it’s output.
- Use the BulkIngest SDK.
We’ll just cover creating an asset and uploading a single file.
IAsset asset = context.Assets.Create(“drive:path\filename.ext”, AssetCreationOptions.None);
That’s it a one-liner again.
If that’s all you need right now, you can stop reading.
Under the hood, the call above will: create an IAsset and add it to the assets collection; create an IFileInfo to put in the asset; create an AccessPolicy with write permission, request a SAS locator for the asset container in Azure Storage using the AccessPolicy, upload the file into your storage account, and finally revoke the SAS url.
Fair warning: we’re probably pulling this one-liner version of create, we feel it is just a little too simple: we need to make all sorts of assumptions in the SDK for that under-the-hood work that just don’t have the granularity that advanced users will need. There is such a thing as too easy. I’ll update the blog post when the new SDK is out, we’re looking at maybe 5 lines of code instead. Meanwhile enjoy the simplicity.
So I hope you’ve had a chance to read about uploading into Azure Storage with Aspera. So how does that tie in here? Well, the first thing you’ll notice is that the above is a blocking call because of the upload. You can also create an asset in three steps:
- Create an Empty Asset
- Upload a file to the storage container
- Update the asset to included the added files.
To create an empty asset, it’s a simple call:
IAsset inputAsset = context.Assets.CreateEmptyAsset(“assetName”, AssetCreationOptions.None);
At this point, you’ll want to add a file to it. If you are not using our SDK to do the uploads as part of the create call, you can just get a SAS url for the asset container, add your file name to it and do a PUT on it. The Azure Storage REST Api will process that and create a file for you.
Here’s how to get that SAS UploadUri:
IAccessPolicy writePolicy = context.AccessPolicies.Create(“Policy For Copying”, TimeSpan.FromMinutes(estimatedUploadMaxTime), AccessPermissions.Write | AccessPermissions.List);
ILocator destinationLocator = context.Locators.CreateSasLocator(asset, writePolicy, DateTime.UtcNow.AddMinutes(-5));
Uri uploadUri = newUri(destinationLocator.Path);
Now you can craft your own HttpRequest from the uploadUri, add you filename and you’re in full control. Skip over the Aspera bit if that’s not for you, but you do need to do a final step, so read on.
So where does Aspera come in?
Well that uploadUri’s first Uri segment will be the asset contianer name, in the form: asset_guid. You can now browse to the asset container name using the Aspera Desktop Client and upload that 3Gig file at blazing fast speeds. Soon you’ll be using the Aspera SDK to script all that and you’ll really be off to the races.
Another upload option is the Azure Storage CloudBlob class, which can upload for you.
Once the file is in storage, either with an HttpRequest or Aspera, or the Azure Storage SDK, you need to let the Media Services know that you’ve added files that it isn’t aware of. To Media Services, that is still an empty asset.
Under the hood, this is enumerating the files in the asset container, creating IFileInfo objects for each file and adding these to the asset in our databases.
That .Publish call is another tricky call which we’ve found doesn’t cover all the user scenarios properly. I’ll blog about how to ‘do this right’ when the next Media Services SDK comes out.
We covered how to add an asset and populate it with a file in this post. There are several variations on doing this, which make more sense when they are taken in context of a larger application or workflow. I’m just trying to build a base that I can refer back to later.