Encoding Video with .NET Core and Azure Media Services — Part 2

Jean-Marc Skopek
Jean-Marc’s Thoughts
3 min readApr 25, 2018

Part 2: Uploading Videos to Azure Storage

Let’s continue from where we left off in Part 1. We now have a MediaServices class that can communicate with Azure Media Services and establish an authentication token. Now let’s upload a video to Azure.

How files are uploaded to Azure

Before we can upload a video file to Azure, we need to define an Asset, an Access Policy, and a file Locator.

Assets are collections of video, audio, image, or text files with corresponding metadata. The files attached to an Asset instance can be encoded or streamed using AMS.

Access Policies define the permissions and access timeframes for an Asset. An Access Policy with upload permissions must be defined before a file can be uploaded to an Asset.

Locators provide an entry point to access files in an Asset. Locators can be configured with Shared Access Signatures for additional permissions, such as the ability to upload a file to the locator address. Locators require a corresponding Asset and Access Policy before they can be created.

Generating an asset

Let’s start by creating the method to generate an asset:

This method takes an asset name and a storage account name and creates an Asset instance in Azure Media Services. Note that the method returns an instance of an Asset class. This can be defined with the following code:

Generating an Access Policy

Next, let’s create a method that generates an Access Policy in AMS. Insert the following method in your MediaServices class:

Generating a Locator

Finally, let’s create a Locator instance that will be used when uploading the file. Insert the following code in your MediaServices class:

This method returns an instance of a Locator class, which can be defined with the following code:

Finally, let’s tie it all together by calling these methods from the main Program.cs file:

Uploading a video to Azure Media Services

Now that we have a locator instance for an asset with an upload-enabled access policy, we can upload a file to Azure. Let’s start by creating a method that takes a file stream, an Asset instance, and a Locator instance:

Once the file has been uploaded to the locator, we must generate a file info request before the file is registered to the asset. The following code generates a file info request:

Finally, let’s tie it all together in the Program.cs file. This code demonstrates an example upload:

Your should now have a video file uploaded and registered to an asset.

Your code should now look like this.

If you are having trouble determining the state of your Azure Media Services account, you may wish to install the Azure Media Services Explorer. This tool provides a nice GUI for navigating your Azure Media Services instance.

In the next step, we will encode our newly uploaded video file.

--

--