Deli z drugimi prek


Connect to Media Services v3 API - Node.js

Media Services logo v3


Warning

Azure Media Services will be retired June 30th, 2024. For more information, see the AMS Retirement Guide.

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

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

  1. Create a package.json file using your favorite editor.
  2. 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

You will keep your authentication values in a file named .env. (That's right, no file name, just the extension.) Read Access the API to learn how to get and copy these values. You can get the values from the API Access page for your Media Services account in the portal, or use the CLI to get the values needed.

Copy and paste the values into a file named .env. The file should be stored at the root of your working repository.

Once you have created the .env file, you can start working with the samples.

Run the sample application HelloWorld-ListAssets

  1. Launch Visual Studio Code from the root Folder.
cd media-services-v3-node-tutorials
code .
  1. Install the packages used in the package.json file from a Terminal
npm install
  1. 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.

  2. Change directory into the HelloWorld-ListAssets folder

cd HelloWorld-ListAssets
  1. 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://learn.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

Get help and support

You can contact Media Services with questions or follow our updates by one of the following methods: