Azure Media Services Part 1: Creating a Media Asset and Uploading from Local Storage

This is the first technical post of many to come!! In this post, I’ll detail exactly what Azure Media Services offers as well as review how to upload your videos as assets programmatically. This post will be aimed primarily at beginner devs, so if this moves a bit slowly for you, you can find some useful information and documentation here. Additionally, I have to give reference for the resource I had access to in learning this information myself; Mingfei Yan, Program Manager of the Azure Media Services team at Microsoft has provided some wonderful resources for learning about Media Services. You can find the first in a series of technical talks she’s done here.

What is Azure Media Services?

For those that may not know, Azure Media Services is the media solution component of the overall cloud solution known as Azure. There are many high end features Media Services sports, such as Live and Video-on-Demand(VoD), encoding and streaming, Digital Rights Management (DRM) support, and multiple platform support. With Media Services, you can supply videos for iOS, HTML5, Flash, Xbox, and Android.

A short while ago, providing these services to customers was an expensive task, often so much so that only giant companies could handle the cost. With Media Services, you can scale up or down when you’re ready. This allows you to pay for what you use and manage resources more intelligently. Specific pricing information can be found here. You can also sign up for a free trial with media services.

Uploading a Video Programmatically: Prerequisites

This walkthrough assumes that you have the following:

  • A Media Services account in an Azure Subscription
  • Visual Studio 2013, 2012, or 2010 SP1 (Pro, Premium, Ultimate, or Express)
  • .NET Framework 4.5 or .NET Framework 4
  • Operating Systems: Windows 8, Windows 7, Windows 2008 R2
  • A file to upload. You can right click and save this video as a test.

Before you start coding, there are a few things that must be done first. Start by opening Visual Studio and starting a new C# Console Application. You’ll have to install the Azure Media Services .NET SDK NuGet package. To do that, in Visual Studio, click “Tools” -> “NuGet Package Manager” -> “Package Manager Console.” In the console, type “Install-Package windowsazure.mediaservices“. Wait a few seconds for the install to finish.

Next, you’ll have to add a reference for System.Configuration in your project. To do that, right click References in the Solution Explorer pane, select “Add Reference”, and select “System.Configuration.”

Uploading a Video Programmatically: Code

We are ready to get rolling! The first thing to do is to overwrite the existing “using” statements at the beginning of the Program.CS file and replace them with these:

These “using statements will ensure that you’ll be referencing all the necessities to get started with uploading a video to media services. The next thing to be done is assign your account name and account key to string variables. This keeps you from having to repeatedly type in the name and key whenever you need to access your account.  You can find your account name and key by logging into your portal, clicking the “Media Services” tab, and clicking “Manage Keys” at the bottom of the portal. A dialog appears, and it looks like this:


Simply copy and paste these fields into two strings in your project before your “Main” method. It should look like this:

The next thing to be done is to create a CloudMediaContext object. This allows us to communicate directly with Media Services. Most of the things we do with Media Services require references to the CloudMediaContext object. Additionally, we should save the desired file path as a string as well. Again, this can be done before the “Main” method:

Now, we can use the CloudMediaContext object in “Main” to reference the Media Services account. We do this by passing in the account name and account key as parameters. Additionally, we declare and instantiate an IAsset. Every video is handled with using assets in Media Services. The “CreateAssetAndUploadSingleFile” function allows us to upload the video as an asset by passing in the file path as a parameter. We’ll walk through this function very soon. The code is shown below:

We’re maing serious headway! There are 5 steps that are required in order to upload a file to Media Services. This will be done in the “CreateAssetAndUploadSingleFile” function. The steps are listed as follows:

  1. Create Asset
  2. Create file object(s) associated with asset
  3. Create access policy and locator for uploading from local storage.
  4. Upload files from local storage to corresponding asset file objects
  5. Select which file will be the primary file within the asset (Important!)
Creating an Asset

To create an asset, we simply have to define the asset name and the asset itself. The code below shows how to do this with “var” types.

Notice in creating the asset, we pass in the asset name and AssetCreationoptions. AssetCreationOptions allow us to encrypt our video. There are 4 options: StorageEncryption, CommonEncryption, EnvelopeEncryption, and None. We’re not concerned about encrypting our file for this tutorial, but it’s very important to know that safety options exist.

Create file object(s) associated with asset

In this step, we define which file or files we associate with the asset. One important note is that there may be instances where there are more than one file associated with a singular asset. Start by retrieving the filename from the specified path. This is done by declaring a “var” type and using the “GetFileName” method from the “Path”object. From there we can create another “var” type which creates an asset file. We then use “WriteLine” to confirm that the asset file has been created. The code is below:

Create access policy and locator for uploading from local storage

The access policy determines how the file will be manipulated. This determines how long we can allow the access window to be available as well as read/write permissions. This is derived from the “AccessPolicies” method from the CloudMediaContext object. “Locator” acts as an entry point into our asset. Notice we only allow five minutes worth of write time. We then pass that policy and the asset into our locator. Again, the code is below:

 Upload files from local storage to corresponding asset file objects

Asset has a handy method called “Upload”. This does exactly what you would think; it uploads the asset to your Media Services account. The code is below:

 Select the primary file in the asset

Sometimes, we deal with multiple files within one asset, so it’s important that the primary file be the actual video. We can do this very easily by using the “IsPrimary” method. Again, the code is below:

Finally, we delete the locator after completing the upload and return the asset.


And there you have it! Entire program is below. I hope that this walkthrough helped!





One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *