Sibeesh Passion

Top Menu

  • Home
  • Search
  • About
  • Privacy Policy

Main Menu

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Home
  • Search
  • About
  • Privacy Policy

logo

Sibeesh Passion

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins

  • Post Messages to Microsoft Teams Using Python

  • Get Azure Blob Storage Blob Metadata Using PowerShell

  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines

.NETAzureVisual Studio
Home›.NET›Working With Azure Media Service Account

Working With Azure Media Service Account

By SibeeshVenu
May 25, 2016
1752
4
Share:
Media Service Created

[toc]

Introduction

In this article we are going to work with Media Service Account in Azure. Once we create a media service account, we will create a console application in which we will add the operations like creating the media assets dynamically. You can also upload some files to the assets created. We will discuss here that too. I hope you will like this.

Download the source code

You can always download the source code here: Azure Media Service

Background

Few days back I have got a requirement of storing some files in the cloud. As you all know, Azure will be the right choice if you talk about the cloud services. And I had account with Azure already, so the things were pretty much easy for me. Thus I decided to create a media service account for this requirement. Here we are going to see how we can create an Azure media service account and how to use the same. Following are the prerequisites.

What is media service account?

  • A media service account is a Azure based account which gives you access to cloud based media services in Azure.
  • Stores metadata of the media files you create, instead saving the actual media content.
  • To work with media service account, you must have an associated storage account.
  • While creating a media service account, you can either select the storage account you already have or you can create a new one.
  • Since the media service account and storage account is treated separately, the content will be available in your storage account even if you delete your media service account
  • Please be noted that your storage account region must be same as your media service account region.

    Prerequisites

  • Visual Studio
  • Azure account with active subscription
  • If you are a start up company or you are in thinking to start a one, you can always go for BizSpark(Software and services made for the start ups), to join please check here: How to join bizspark. Or you can always create a free account here.

    Things we are going to do

    The following are the tasks we are going to do.

  • Creating an Azure media service account.
  • If you don’t have an Azure account with active subscription, you must creates it first before going to do this task.

  • Creating a Console application to use Media service account
  • Create an Asset dynamically via console application
  • Uploading image to the assets
  • Retrieving the items from the Asset
  • Now we will go and create our media service account.

    Steps to create an Azure media service account

    To create an azure media service account, please follows the preceding steps.

    Step 1: Login to your Azure Portal (https://portal.azure.com)

    Azure Home

    Azure Home

    Step 2: Click New and select Media + CDN

    Media And CDN

    Media And CDN

    Step 3: Click on the media service

    This will redirect you to old azure portal, If you are not redirected, no worries. It is fine.

    Media Service

    Media Service

    Step 4: Give the details.

    New Storage Account

    New Storage Account

    Here you can select if you have a storage account or a new one will be created for you automatically.

    Step 5: Finish

    Once you give the needed details, you can click on the tick symbol.

    Media Service Created

    Media Service Created

    Now we have our storage account and media service account with us. You can always download the sample applications given there. What you need to do all is just select the language you wish and click on the download link. We will create a console application to use this media service account. Sounds cool?

    Creating a Console application to use Media service account

    To create a console application in Visual Studio click File->New project-> Select language->Select Console Application.

    Now go back to your Azure portal and click on Manage Keys at the bottom, copy the MEDIA SERVICE ACCOUNT NAME and MEDIA SERVICE ACCESS KEY (Either primary or secondary). Then you need to add the settings as appSettings in your App.config file as follows.

    [xml]
    <appSettings>
    <add key="MediaServicesAccountName" value="****" />
    <add key="MediaServicesAccountKey" value="***************************" />
    </appSettings>
    [/xml]

    The next thing you need to do is installing windowsazure.mediaservices from package manager console. For that please go to your package manger console from NuGet Package Manager. And run the below query.

    [csharp]
    install-package windowsazure.mediaservices
    [/csharp]

    Now you need to include the preceding namespace to get started with.

    [csharp]
    using Microsoft.WindowsAzure.MediaServices.Client;
    [/csharp]

    Now change your Program.cs codes as follows

    [csharp]
    using Microsoft.WindowsAzure.MediaServices.Client;
    using System;
    using System.Configuration;

    namespace AzureMediaServiceApp
    {
    class Program
    {
    #region Constants
    private static string mediaServicesAccountName = ConfigurationManager.AppSettings["MediaServicesAccountName"];
    private static string mediaServicesAccountKey = ConfigurationManager.AppSettings["MediaServicesAccountKey"];
    #endregion
    static void Main(string[] args)
    {
    string input = string.Empty;
    Console.WriteLine("Enter the asset name to be created…");
    input = Console.ReadLine();
    CreateBLOBContainer(input);
    }
    public static string CreateBLOBContainer(string containerName)
    {
    try
    {
    string result = string.Empty;
    CloudMediaContext mediaContext;
    mediaContext = new CloudMediaContext(mediaServicesAccountName, mediaServicesAccountKey);
    IAsset asset = mediaContext.Assets.Create(containerName, AssetCreationOptions.None);
    return asset.Uri.ToString();
    }
    catch (Exception ex)
    {
    return ex.Message;
    }
    }
    }
    }
    [/csharp]

    What we are doing in the above code is, we are fetching the account details from the App.config and calling a function CreateBLOBContainer with the name of the asset as a parameter. We will ask for this name in the console window. Sounds good?

    Media Service Created Output

    Media Service Created Output

    Now go back to your azure portal and click on content tab, you can see the asset you are just created.

    Media Service Created Azure Portal Output

    Media Service Created Azure Portal Output

    Now we will create a function which will upload some files to asset. So that we will change our Program.cs file as follows.

    [csharp]
    using Microsoft.WindowsAzure.MediaServices.Client;
    using System;
    using System.Configuration;
    using System.IO;

    namespace AzureMediaServiceApp
    {
    class Program
    {
    #region Constants
    private static readonly string mediaServicesAccountName = ConfigurationManager.AppSettings["MediaServicesAccountName"];
    private static readonly string mediaServicesAccountKey = ConfigurationManager.AppSettings["MediaServicesAccountKey"];
    private static readonly string myAzureCon = ConfigurationManager.ConnectionStrings["myAzureStorageCon"].ConnectionString;
    private static MediaServicesCredentials _mediaServiceCredentials = null;
    #endregion
    static void Main(string[] args)
    {
    string input = string.Empty;
    Console.WriteLine("Enter the asset name to be created…");
    input = Console.ReadLine();
    _mediaServiceCredentials = new MediaServicesCredentials(mediaServicesAccountName, mediaServicesAccountKey);
    IAsset asset = CreateBLOBContainer(input, _mediaServiceCredentials);
    UploadImages(asset, _mediaServiceCredentials);
    }
    public static IAsset CreateBLOBContainer(string containerName, MediaServicesCredentials _medServCredentials)
    {
    try
    {
    string result = string.Empty;
    CloudMediaContext mediaContext;
    mediaContext = new CloudMediaContext(_medServCredentials);
    IAsset asset = mediaContext.Assets.Create(containerName, AssetCreationOptions.None);
    return asset;

    }
    catch (Exception)
    {
    throw;
    }
    }
    public static string UploadImages(IAsset asset, MediaServicesCredentials _medServCredentials)
    {
    try
    {
    string _singleInputFilePath = Path.GetFullPath(@"E:\X7Md4VB.JPG");
    CloudMediaContext mediaContext;
    mediaContext = new CloudMediaContext(_medServCredentials);
    var fileName = Path.GetFileName(_singleInputFilePath);
    var assetFile = asset.AssetFiles.Create(fileName);
    var policy = mediaContext.AccessPolicies.Create("policy for upload", TimeSpan.FromMinutes(30), AccessPermissions.Read | AccessPermissions.Write | AccessPermissions.List);
    var locator = mediaContext.Locators.CreateSasLocator(asset, policy, DateTime.UtcNow.AddDays(1));
    assetFile.Upload(_singleInputFilePath);
    return "Success!";
    }
    catch (Exception)
    {
    throw;
    }
    }
    }
    }
    [/csharp]

    Here we calls a function UploadImages which accepts IAsset and MediaServicesCredentials as a parameter to upload the images to the asset we created. Once you run this, you can see that image has been uploaded in your asset. To check that, please go to your Azure portal and see your asset size in content tab. I am sure that the size must be changed.

    Image uploaded to Media Service

    Image uploaded to Media Service

    Now we will list down all the items we saved to the asset. Shall we?

    Retrieving the items from the Asset

    To retrieve the items we will add an another function as follows.

    [csharp]
    private static void GetAllTheAssetsAndFiles(MediaServicesCredentials _medServCredentials)
    {
    try
    {
    string result = string.Empty;
    CloudMediaContext mediaContext;
    mediaContext = new CloudMediaContext(_medServCredentials);
    StringBuilder myBuilder = new StringBuilder();
    foreach (var item in mediaContext.Assets)
    {
    myBuilder.AppendLine(Environment.NewLine);
    myBuilder.AppendLine("–My Assets–");
    myBuilder.AppendLine("Name: " + item.Name);
    myBuilder.AppendLine("++++++++++++++++++++");

    foreach (var subItem in item.AssetFiles)
    {
    myBuilder.AppendLine("File Name: "+subItem.Name);
    myBuilder.AppendLine("Size: " + subItem.ContentFileSize);
    myBuilder.AppendLine("++++++++++++++++++++++");
    }
    }
    Console.WriteLine(myBuilder);
    }
    catch (Exception)
    {
    throw;
    }
    }
    [/csharp]

    And it will give you an output as follows.

    My Assets and contents

    My Assets and contents

    Conclusion

    Did I miss anything that you may think which is needed? Have you ever tried Azure media service account? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.

    Your turn. What do you think?

    A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.

    Kindest Regards
    Sibeesh Venu

    TagsAzureCreate an Asset dynamically via console applicationMedia ServiceMedia service accountRetrieving the items from the AssetUploading image to the assetsVisual Studio
    Previous Article

    Visual C# Technical Guru – April 2016

    Next Article

    Client Side Exporting In HighChart

    0
    Shares
    • 0
    • +
    • 0
    • 0
    • 0

    SibeeshVenu

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer.

    Related articles More from author

    • AzureHow to

      Fix for 404 ResourceNotFound Error After Uploading to Azure Container

      May 27, 2017
      By SibeeshVenu
    • .NETAzureUWP

      UWP Application Signin, Release, Distribute, Deploy With Azure DevOps Pipeline

      November 3, 2019
      By SibeeshVenu
    • Azure

      Post Messages to Microsoft Teams Using Python

      October 1, 2022
      By SibeeshVenu
    • .NETVideosVisual StudioVisual Studio 2017

      The Cool New Refactoring and Text Editor Options in VS2017

      April 2, 2017
      By SibeeshVenu
    • Create_Virtual_Machine_In_Azure
      AzureVirtual Machine

      Create Virtual Machine In Azure

      September 17, 2015
      By SibeeshVenu
    • .NETASP.NETAzure

      Asp.Net Core Windows Service Task Scheduler Daily, Weekly, Monthly

      August 12, 2019
      By SibeeshVenu
    0

    My book

    Asp Net Core and Azure with Raspberry Pi Sibeesh Venu

    YouTube

    MICROSOFT MVP (2016-2022)

    profile for Sibeesh Venu - Microsoft MVP

    Recent Posts

    • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment
    • Build, Deploy, Configure CI &CD Your Static Website in 5 mins
    • Easily move data from one COSMOS DB to another
    • .NET 8 New and Efficient Way to Check IP is in Given IP Range
    • Async Client IP safelist for Dot NET
    • Post Messages to Microsoft Teams Using Python
    • Get Azure Blob Storage Blob Metadata Using PowerShell
    • Deploy .net 6 App to Azure from Azure DevOps using Pipelines
    • Integrate Azure App Insights in 1 Minute to .Net6 Application
    • Azure DevOps Service Connection with Multiple Azure Resource Group

    Tags

    Achievements (35) Angular (14) Angular 5 (7) Angular JS (15) article (10) Article Of The Day (13) Asp.Net (14) Azure (65) Azure DevOps (10) Azure Function (10) Azure IoT (7) C# (17) c-sharp corner (13) Career Advice (11) chart (11) CSharp (7) CSS (7) CSS3 (6) HighChart (10) How To (9) HTML5 (10) HTML5 Chart (11) Interview (6) IoT (11) Javascript (10) JQuery (82) jquery functions (9) JQWidgets (15) JQX Grid (17) Json (7) Microsoft (8) MVC (20) MVP (9) MXChip (7) News (18) Office 365 (7) Products (10) SQL (20) SQL Server (15) Visual Studio (10) Visual Studio 2017 (7) VS2017 (7) Web API (12) Windows 10 (7) Wordpress (9)
    • .NET
    • Achievements
    • ADO.NET
    • Android
    • Angular
    • Arduino
    • Article Of The Day
    • ASP.NET
    • Asp.Net Core
    • Automobile
    • Awards
    • Azure
    • Azure CDN
    • azure devops
    • Blockchain
    • Blog
    • Browser
    • C-Sharp Corner
    • C#
    • Career Advice
    • Code Snippets
    • CodeProject
    • Cognitive Services
    • Cosmos DB
    • CSS
    • CSS3
    • Data Factory
    • Database
    • Docker
    • Drawings
    • Drill Down Chart
    • English
    • Excel Programming
    • Exporting
    • Facebook
    • Fun
    • Gadgets
    • GitHub
    • GoPro
    • High Map
    • HighChart
    • How to
    • HTML
    • HTML5
    • Ignite UI
    • IIS
    • Interview
    • IoT
    • JavaScript
    • JQuery
    • jQuery UI
    • JQWidgets
    • JQX Grid
    • Json
    • Knockout JS
    • Linux
    • Machine Learning
    • Malayalam
    • Malayalam Poems
    • MDX Query
    • Microsoft
    • Microsoft ADOMD
    • Microsoft MVP
    • Microsoft Office
    • Microsoft Technologies
    • Microsoft Windows
    • Microsoft Windows Server
    • Mobile
    • MongoDB
    • Monthly Winners
    • MVC
    • MVC Grid
    • MySQL
    • News
    • Node JS
    • npm
    • Number Conversions
    • October 2015
    • Office 365
    • Office Development
    • One Plus
    • Outlook
    • Page
    • PHP
    • Poems
    • PowerShell
    • Products
    • Q&A
    • Raspberry PI
    • React
    • SEO
    • SharePoint
    • Skype
    • Social Media
    • Software
    • Spire.Doc
    • Spire.PDF
    • Spire.XLS
    • SQL
    • SQL Server
    • SSAS
    • SSMS
    • Storage In HTML5
    • Stories
    • Third Party Software Apps
    • Tips
    • Tools
    • Translator Text
    • Uncategorized
    • Unit Testing
    • UWP
    • VB.Net
    • Videos
    • Virtual Machine
    • Visual Studio
    • Visual Studio 2017
    • Wamp Server
    • Web API
    • Web Platform Installer
    • Webinars
    • WebMatrix
    • Windows 10
    • Windows 7
    • Windows 8.1
    • Wordpress
    • Writing

    ABOUT ME

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer. If you would like to know more about me, you can read my story here.

    Contact Me

    • info@sibeeshpassion.com

    Pages

    • About
    • Search
    • Privacy Policy
    • About
    • Search
    • Privacy Policy
    © Copyright Sibeesh Passion 2014-2025. All Rights Reserved.
    Go to mobile version