GithubHelp home page GithubHelp logo

isabella232 / media-services-v2-dotnet-core-restsharp-sample Goto Github PK

View Code? Open in Web Editor NEW

This project forked from azure-samples/media-services-v2-dotnet-core-restsharp-sample

0.0 0.0 0.0 73.54 MB

Rest Sharp samples for the Azure Media Services v2 API

License: MIT License

C# 100.00%

media-services-v2-dotnet-core-restsharp-sample's Introduction

page_type languages products description
sample
csharp
azure
azure-media-services
azure-functions
dotnet-core
Azure Media Services V2 REST API example using RestSharp

Azure Media Services V2 REST API samples using RestSharp in .NetCore 3.1

Azure Media Services (AMS) does not have a .NetCore SDK for their legacy (V2) API. This project aims to provide some patterns and sample code to access the V2 REST API by using the popular RestSharp library. It also provides a yml build pipeline, storage access services, demonstrates dependency injection in Azure Functions and Console app, as well as setting up RBAC for a MSI.

Note : it is recommended to use AMS v3 REST API when possible. AMS v2 REST API still provides some features which are not yet in v3. This document describes the feature gaps. You may also read the v2 to v3 migration document.

About the solution

This sample contains a number of Azure Functions and a console app which demonstrate simple concepts:

\src\Sample.AzFunction.HelloWorld :

  • Function 'GetJobById' is an easy-to-read GET call to Media Services using RestSharp. More here.

\src\Sample.AzFunction.Advanced :

  • Function 'StartMediaEncoderStandardJob' uses a collection of services to start and track a job with MediaServices, read in detail below. \src\Sample.AzFunction.Advanced

  • Functions 'GetStorageAccountKey' and 'GetContentLength' demonstrate typical Azure service operations needed when building media solutions. \src\Sample.AzFunction.Advanced

\src\Sample.Console.UploadAndEncodeSprites :

  • This console app sample uploads a MP4 file, adds a S2 media reserved unit, encodes it using the Sprite preset, waits for the job completion and then releases the S2 media reserved unit.

What this solution is not

The IMediaServicesV2RestSharp is not exhaustive, it only includes those API calls needed to perform the work in this sample. It is easy to fork and extend, see contribution guidelines and licensing files.

Authentication and Authorization

When deployed in Azure

The ServiceCollectionExtensions include both AddAzureFluentManagement and AddDefaultAzureTokenCredential to set up the auth for the project.

When deployed to Azure as a Function App, with system assigned managed identity enabled, the web-host will provide the required credentials for both IAzure and TokenCredential objects; which are injected into the service container and used in the implementations.

This managed service principal must have appropriate role based access control to the storage accounts it needs to access via the code sample, and access to the media services resource.

The following are needed to access storage:

  • Storage Blob Data Contributor
  • Reader and Data Access

The following is needed for AMS:

  • Contributor on the AMS resource.

Run some commands from a shell.azure.com instance, or a bash shell with the Azure CLI tooling, to permit the application.

Follow this detailed example on how to add these roles.

Read the HelloWorld readme.md to review authentication flow.

Azure Function application settings

Add the following application settings :

  • AZURE_TENANT_ID
  • AZURE_SUBSCRIPTION_ID
  • ArmManagementUrl
  • AmsAccountName
  • AmsResourceGroup
  • AmsV2CallbackEndpoint

Functions Application Settings

Use "Advanced edit" mode and add :

{
  "name": "ArmManagementUrl",
  "value": "https://management.azure.com",
  "slotSetting": false
},
{
  "name": "AmsAccountName",
  "value": "<< Put your azure media services account name here. >>",
  "slotSetting": false
},
{
  "name": "AmsResourceGroup",
  "value": "<< Put your azure media services account resource group name here. >>",
  "slotSetting": false
},
{
  "name": "AmsV2CallbackEndpoint",
  "value": "<< Put your azure media services callback endpoint here. e.g https://my.service.com/amsv2statusendpoint or an empty sting when not using a callback endpoint.>>",
  "slotSetting": false
},
{
  "name": "AZURE_SUBSCRIPTION_ID",
  "value": "<< Put your Subscription Guid here. >>",
  "slotSetting": false
},
{
  "name": "AZURE_TENANT_ID",
  "value": "<< Put your Tenant Guid here. >>",
  "slotSetting": false
}

Running locally the Azure functions or the console app

From the template, create a local.settings.json file (for Azure Functions) or a appsettings.json file (for the console app) and set

  • AZURE_TENANT_ID
  • AZURE_SUBSCRIPTION_ID
  • ArmManagementUrl
  • AmsAccountName
  • AmsResourceGroup

These will allow for the local user to log in interactively and use their identity for Azure operations, ensure they have sufficient privilege.

Interfaces

The function and the console app uses an instance of IMediaServicesV2Encoder to start a job with a number of parameters. That encoder performs several operations to support that request. Those operations are implemented in IMediaServicesV2EncodeOperations. In turn the EncodeService calls into the Azure Media Services API via the IMediaServicesV2RestSharp, it also uses IAzureStorageOperations and IAzureStorageManagement for it's storage interactions.

Service Interfaces

How to call the StartMediaEncoderStandardJob function

To test the StartMediaEncoderStandardJob function:

  • copy a mp4 file into a Azure storage account,
  • call the StartMediaEncoderStandardJob function from Curl or Postman. Please specify the path of the mp4 file in the blobUri parameter.

Curl command line is at the end of StartMediaEncoderStandardJob.cs

Postman : Service Interfaces

How to run the console app

In Visual Studio, select Sample.Console.UploadAndEncodeSprites project and run it. An mp4 file is provided and will be uploaded by the app. Service Interfaces

Service Interfaces

media-services-v2-dotnet-core-restsharp-sample's People

Contributors

microsoft-github-operations[bot] avatar microsoftopensource avatar nickdrouin avatar xpouyat avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.