In this tutorial we are going to see how to upload a Video media file to the Windows Azure Media Service programmatically from the code using the Azure Media Services SDK for .Net. In Windows Azure Media Service the process of Uploading a media file or the asset to the Media Service is termed as Ingesting the Asset or media file. Basically when we upload a media file to the media service we use the IAsset to interface which has the collection of objects that has the metadata of the media file which we are going to upload. We need to use Asset to upload the media file and process the media, and at the same time we need to use the Asset again to output the media file to any device or any delivery medium.
Uploading the Asset we can do that in many ways like we can upload a single media file, or upload a collection of media files to a single asset or we can as well encrypt the asset and upload it to the media service based on the requirement. Encrypting a media file asset and uploading to the media service we have 3 possible options as AssetCreationoption enumeration which are None, StorageEncrypted and CommonEncryptionProtected. [more]
- None – Used when we want to provide the encryption option but the file should not be encrypted we can specify None.
- StorageEncrypted – Used when we want to encrypt a file and upload to the storage service (blob) we can specify this option.
- CommonEncryptionProtected – Used when we want to encrypt a file which is already encrypted using Common Encryption Protected scheme
In order to start uploading the vide media file (Asset) to the Windows Azure media services we need to use the server context which we have used and connected to the Media Service in our earlier article (). Once we able to connect to the Media Services with the Account name and Account key we can upload the asset easily to the media service. Let us see the step by step on how to upload the video media file to the Windows Azure Media Services. Open the Media Services sample project which we used in our earlier article (Getting Started with Windows Azure Media Services – Connecting to Azure Media Services Programmatically – #Meet Azure Edition) and create a button control where on click event we will write our code to upload a sample video file to the Azure Media Services as shown in the screen below.
Now we are going to write a wrapper class which plays a major role in our application, this wrapper class will have the complete code listing for connecting to the Azure Media Services, Uploading and downloading Assets, Creating jobs, Streaming asset etc. which we will be keep on adding one by one in this series. First let us add the code to upload a media file, where we hardcode the media file which we are going to upload in the code itself as shown in the screens below.
Code:
private static CloudMediaContext cmContext = null;
private static readonly string strMsAccountName = ConfigurationManager.AppSettings["msAccountName"];
private static readonly string strMsAccountKey = ConfigurationManager.AppSettings["msAccountKey"];
MediaSevicesWrappercs msWrapper = new MediaSevicesWrappercs();
//Sample Implementations:
private static readonly string strSupportFiles = ConfigurationManager.AppSettings["msConfigFolder"];
private static readonly string strsingleInputFilePath = strSupportFiles + @"\MediaFiles\Video2.wmv";
private static readonly string strprimaryFilePath = strSupportFiles + @"\MediaFiles\Video1.wmv";
private static readonly string strinputFilesFolder = strSupportFiles + @"\MediaFiles";
Now let us add the wrapper class where we are going to implement the Asser upload method using the IAsset interface as shown in the code below.
Code:
public string CreateAndUploadAsset(CloudMediaContext CloudContext, string inputMediaFilePath)
{
string strResult;
IAsset theAsset = CloudContext.Assets.Create(inputMediaFilePath);
strResult = "AssetName :- " + theAsset.Name.ToString() + " \n" + "Asset Created Time :- " + theAsset.Created.Date.ToString();
return strResult;
}
The above code will upload a single Asset to the Media Service, say if want to upload a number of media files from a directory we can use the below method which loops through the folder and uploads to the Media Services one by one as shown in the code below.
Code:
public string CreateAndUploadAssetsFromDirectory(CloudMediaContext CloudContext, string inputMediaFilesFolder, string primaryFilePath)
{
string strResult;
strResult = "Files are uploading!!! \n";
IAsset theAsset = CloudContext.Assets.CreateFromDirectory(inputMediaFilesFolder, primaryFilePath);
strResult += "Finished uploading!!! \n";
strResult += "Uploaded Asset ID :- " + theAsset.Id + "\n";
strResult += "******FILES IN ASSET****** \n";
foreach (IFileInfo file in theAsset.Files)
{
strResult += "File name :- " + file.Name + "\n";
strResult += "Is a primary file :- " + file.IsPrimary + "\n";
strResult += "File size :- " + file.ContentFileSize + "\n";
}
return strResult;
}
The above code will internally loop through the folder and upload the media files as Assets to the Azure Media Services one by one. Also we can customize our code to check the uploading progress where we can see how much bytes are transferred for each and every upload limit. IAsset interface plays the key role in uploading the asset files to the media service with Method and CreateFromDirCreate ectory methods.
In the above codes we have seen the uploading of Assets which are plain without encryptions, if we want to upload by assigning an encryption method we need to make use of the below code which basically encrypts the file before uploading to the media service. Encryption options varies as we discussed in the above section as None, StorageEncrypted and CommonEncryptionProtected.
Code:
public string CreateAndUploadEncryptedAsset(CloudMediaContext CloudContext, string inputMediaFilePath)
{
string strResult;
IAsset theAsset = CloudContext.Assets.Create(inputMediaFilePath, AssetCreationOptions.StorageEncrypted);
strResult = "Asset name :- " + theAsset.Name + "\n";
strResult += "Time created :- " + theAsset.Created.Date.ToString() + "\n";
strResult += "Encrypted status :- " + theAsset.Options.ToString() + "\n";
return strResult;
}
We can see we have assigned the encryption method as StorageEncrypted, this is the default encryption mechanism used. If we are not specifying any option also by default the SDK takes StorageEncrypted as the encryption mechanism. Now we need to write code to specify which media file to be used for uploading and which folder we will be used where media files resides. We are going to use the Azure Storage Explorer to identify if the media files are uploaded correctly. Write the below code which calls the wrapper and provide the required parameters that are very much required to process the Assets as shown in the code below.
Code:
private void button2_Click(object sender, RoutedEventArgs e)
{
if (cmContext == null)
{
cmContext = GetContext();
}
string strresult = msWrapper.CreateAndUploadAsset(cmContext, strsingleInputFilePath);
tbResult.Text = strresult.ToString();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
if (cmContext == null)
{
cmContext = GetContext();
}
string strresult = msWrapper.CreateAndUploadAssetsFromDirectory(cmContext, strinputFilesFolder, strprimaryFilePath);
tbResult.Text = strresult.ToString();
}
private void button4_Click(object sender, RoutedEventArgs e)
{
if (cmContext == null)
{
cmContext = GetContext();
}
string strresult = msWrapper.CreateAndUploadEncryptedAsset(cmContext, strsingleInputFilePath);
tbResult.Text = strresult.ToString();
}
Complete Code Listing:
MainWindow.Xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Configuration;
using Microsoft.WindowsAzure.MediaServices.Client;
namespace MeetAzureMediaServices
{
public partial class MainWindow : Window
{
private static CloudMediaContext cmContext = null;
private static readonly string strMsAccountName = ConfigurationManager.AppSettings["msAccountName"];
private static readonly string strMsAccountKey = ConfigurationManager.AppSettings["msAccountKey"];
MediaSevicesWrappercs msWrapper = new MediaSevicesWrappercs();
//Sample Implementations:
private static readonly string strSupportFiles = ConfigurationManager.AppSettings["msConfigFolder"];
private static readonly string strsingleInputFilePath = strSupportFiles + @"\MediaFiles\Video2.wmv";
private static readonly string strprimaryFilePath = strSupportFiles + @"\MediaFiles\Video1.wmv";
private static readonly string strinputFilesFolder = strSupportFiles + @"\MediaFiles";
public MainWindow()
{
InitializeComponent();
}
static CloudMediaContext GetContext()
{
return new CloudMediaContext(strMsAccountName, strMsAccountKey);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
cmContext = GetContext();
tbResult.Text = "Connection Successful!!!";
}
catch
{
tbResult.Text = "Connection Failed!!!";
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
if (cmContext == null)
{
cmContext = GetContext();
}
string strresult = msWrapper.CreateAndUploadAsset(cmContext, strsingleInputFilePath);
tbResult.Text = strresult.ToString();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
if (cmContext == null)
{
cmContext = GetContext();
}
string strresult = msWrapper.CreateAndUploadAssetsFromDirectory(cmContext, strinputFilesFolder, strprimaryFilePath);
tbResult.Text = strresult.ToString();
}
private void button4_Click(object sender, RoutedEventArgs e)
{
if (cmContext == null)
{
cmContext = GetContext();
}
string strresult = msWrapper.CreateAndUploadEncryptedAsset(cmContext, strsingleInputFilePath);
tbResult.Text = strresult.ToString();
}
}
}
MediaServicesWrapper.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.MediaServices.Client;
namespace MeetAzureMediaServices
{
public class MediaSevicesWrappercs
{
//
public string CreateAndUploadAsset(CloudMediaContext CloudContext, string inputMediaFilePath)
{
string strResult;
IAsset theAsset = CloudContext.Assets.Create(inputMediaFilePath);
strResult = "AssetName :- " + theAsset.Name.ToString() + " \n" + "Asset Created Time :- " + theAsset.Created.Date.ToString();
return strResult;
}
//
public string CreateAndUploadAssetsFromDirectory(CloudMediaContext CloudContext, string inputMediaFilesFolder, string primaryFilePath)
{
string strResult;
strResult = "Files are uploading!!! \n";
IAsset theAsset = CloudContext.Assets.CreateFromDirectory(inputMediaFilesFolder, primaryFilePath);
strResult += "Finished uploading!!! \n";
strResult += "Uploaded Asset ID :- " + theAsset.Id + "\n";
strResult += "******FILES IN ASSET****** \n";
foreach (IFileInfo file in theAsset.Files)
{
strResult += "File name :- " + file.Name + "\n";
strResult += "Is a primary file :- " + file.IsPrimary + "\n";
strResult += "File size :- " + file.ContentFileSize + "\n";
}
return strResult;
}
public string CreateAndUploadEncryptedAsset(CloudMediaContext CloudContext, string inputMediaFilePath)
{
string strResult;
IAsset theAsset = CloudContext.Assets.Create(inputMediaFilePath, AssetCreationOptions.StorageEncrypted);
strResult = "Asset name :- " + theAsset.Name + "\n";
strResult += "Time created :- " + theAsset.Created.Date.ToString() + "\n";
strResult += "Encrypted status :- " + theAsset.Options.ToString() + "\n";
return strResult;
}
}
}
Now we are done with our code, as we mentioned we are going to use Azure Storage Explorer by connection to the storage services. Initially we don’t see any of the files available with in the storage services as we see in the below screen.
Now let us run the code and try to explore by clicking on each of the buttons which upload single asset file, multiple asset file and asset files with encryption one by one.
Single File Upload:
Multiple File Upload:
Encryption File Upload :
No Comments
Hello Karthikeyan,
i m having an issue while downloading encrypted files from blob storage.
Will you give me some suggestion on this issue
http://stackoverflow.com/questions/23744500/downloading-encrypted-file-from-window-azure-storage
Thanks