Connect to Media Services v3 API - Node.js
Looking for Media Services v2 documentation?
Having trouble? See the Troubleshooting guide for solutions to issues with using Media Services.
Code samples can be found on the Samples page.
This article shows you how to connect to the Azure Media Services v3 node.js SDK using the service principal sign-in method. You will work with files in the media-services-v3-node-tutorials samples repository. The HelloWorld-ListAssets sample contains the code for connecting then list Assets in the account.
Prerequisites
- An installation of Visual Studio Code.
- Install Node.js.
- Install TypeScript.
- Create a Media Services account. Be sure to remember the resource group name and the Media Services account name.
- Create a service principal for your application. See access APIs.
Pro tip! Keep this window open or copy everything in the JSON tab to Notepad. - Make sure to get the latest version of the AzureMediaServices SDK for JavaScript.
Important
Review the Azure Media Services naming conventions to understand the important naming restrictions on entities.
Clone the Node.JS samples repo
You will work with some files in Azure Samples. Clone the Node.JS samples repository.
git clone https://github.com/Azure-Samples/media-services-v3-node-tutorials.git
Install the Node.js packages
Install @azure/arm-mediaservices
npm install @azure/arm-mediaservices
For this example, you will use the following packages in the package.json file.
| Package | Description |
|---|---|
@azure/arm-mediaservices |
Azure Media Services SDK. To make sure you are using the latest Azure Media Services package, check npm install @azure/arm-mediaservices. |
@azure/identity |
Required for Azure AD authentication using Service Principal or Managed Identity |
@azure/storage-blob |
Storage SDK. Used when uploading files into assets. |
@azure/abort-controller |
Used along with the storage client to time out long running download operations |
Create the package.json file
- Create a
package.jsonfile using your favorite editor. - Open the file and paste the following code:
{
"name": "media-services-node-sample",
"version": "0.1.0",
"description": "",
"main": "./index.ts",
"dependencies": {
"@azure/arm-mediaservices": "^10.0.0",
"@azure/abort-controller": "^1.0.2",
"@azure/identity": "^2.0.0",
"@azure/storage-blob": "^12.4.0"
}
}
Connect to Node.js client using TypeScript
Sample .env file
Copy the content of this file to a file named .env. It should be stored at the root of your working repository. These are the values you got from the API Access page for your Media Services account in the portal.
To access the values needed for entering into the .env file, it is recommended to first read and review the how-to article Access the API. You can use either the Azure portal or the CLI to get the values needed to enter into this sample's environment variables file.
Once you have created the .env file, you can start working with the samples.
# Values from the API Access page in the portal
AADCLIENTID="00000000-0000-0000-0000-000000000000"
AADSECRET="00000000-0000-0000-0000-000000000000"
AADTENANTID="00000000-0000-0000-0000-000000000000"
# Change this to match your Azure AD Tenant domain name.
AADTENANTDOMAIN="microsoft.onmicrosoft.com"
# Set this to your Media Services Account name, resource group it is contained in, and location
ACCOUNTNAME="amsaccount"
RESOURCEGROUP="amsResourceGroup"
# Set this to your Azure Subscription ID
SUBSCRIPTIONID="00000000-0000-0000-0000-000000000000"
# You must change this if you are using Gov Cloud, China, or other non standard cloud regions
AADENDPOINT="https://login.microsoftonline.com"
# DRM Testing
DRMSYMMETRICKEY="add random base 64 encoded string here"
Run the sample application HelloWorld-ListAssets
- Launch Visual Studio Code from the root Folder.
cd media-services-v3-node-tutorials
code .
- Install the packages used in the package.json file from a Terminal
npm install
Make a copy of the sample.env file, rename it to .env and update the values in the file to match your account and subscription information. You may need to gather this information from the Azure portal first.
Change directory into the HelloWorld-ListAssets folder
cd HelloWorld-ListAssets
- Open the list-assets.ts file in the HelloWorld-ListAssets folder and press the F5 key in Visual Studio code to begin running the script. You should see a list of assets displayed if you have assets already in the account. If the account is empty, you will see an empty list.
To quickly see assets listed, use the portal to upload a few video files. Assets will automatically be created each one and running this script again will then return their names.
A closer look at the HelloWorld-ListAssets sample
The HelloWorld-ListAssets sample shows you how to connect to the Media Services client with a Service Principal and list Assets in the account. See the comments in the code for a detailed explanation of what it does.
import { DefaultAzureCredential } from "@azure/identity";
import {
AzureMediaServices
} from '@azure/arm-mediaservices';
// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();
export async function main() {
// Copy the samples.env file and rename it to .env first, then populate it's values with the values obtained
// from your Media Services account's API Access page in the Azure portal.
const clientId: string = process.env.AADCLIENTID as string;
const secret: string = process.env.AADSECRET as string;
const tenantDomain: string = process.env.AADTENANTDOMAIN as string;
const subscriptionId: string = process.env.SUBSCRIPTIONID as string;
const resourceGroup: string = process.env.RESOURCEGROUP as string;
const accountName: string = process.env.ACCOUNTNAME as string;
// This sample uses the default Azure Credential object, which relies on the environment variable settings.
// If you wish to use User assigned managed identity, see the samples for v2 of @azure/identity
// Managed identity authentication is supported via either the DefaultAzureCredential or the ManagedIdentityCredential classes
// https://docs.microsoft.com/javascript/api/overview/azure/identity-readme?view=azure-node-latest
// See the following examples for how to authenticate in Azure with managed identity
// https://github.com/Azure/azure-sdk-for-js/blob/@azure/identity_2.0.1/sdk/identity/identity/samples/AzureIdentityExamples.md#authenticating-in-azure-with-managed-identity
// const credential = new ManagedIdentityCredential("<USER_ASSIGNED_MANAGED_IDENTITY_CLIENT_ID>");
const credential = new DefaultAzureCredential();
let mediaServicesClient = new AzureMediaServices(credential, subscriptionId)
// List Assets in Account
console.log("Listing assets in account:")
for await (const asset of mediaServicesClient.assets.list(resourceGroup, accountName, { top:1000 })){
console.log(asset.name);
}
}
main().catch((err) => {
console.error("Error running sample:", err.message);
});
More samples
Many more samples are available in the repository. Please review the readme file for the latest updated samples.
References for Media Services JavaScript/TypeScript developers
- npm install @azure/arm-mediaservices
- Reference documentation for Azure Media Services modules for Node.js
- Azure for JavaScript & Node.js developers
- Media Services source code in the @azure/azure-sdk-for-js Git Hub repo
- Azure Package Documentation for Node.js developers
- Media Services concepts
- Azure for JavaScript & Node.js developers
- Media Services source code in the @azure/azure-sdk-for-js repo