连接到媒体服务 v3 API - Node.jsConnect to Media Services v3 API - Node.js

媒体服务徽标 v3media services logo v3


本文介绍如何使用服务主体登录方法连接到 Azure 媒体服务 v3 node.js SDK。This article shows you how to connect to the Azure Media Services v3 node.js SDK using the service principal sign in method.

必备条件Prerequisites

重要

查看命名约定Review naming conventions.

创建 package.jsonCreate package.json

  1. 使用偏好的编辑器创建一个 package.json 文件。Create a package.json file using your favorite editor.

  2. 打开该文件并粘贴以下代码:Open the file and paste the following code:

    确保获取适用于 JavaScript 的 AzureMediaServices SDKMake sure to get the latest version of the AzureMediaServices SDK for JavaScript.

{
  "name": "media-services-node-sample",
  "version": "0.1.0",
  "description": "",
  "main": "./index.js",
  "dependencies": {
    "azure-arm-mediaservices": "^8.0.0",
    "azure-storage": "^2.8.0",
    "ms-rest": "^2.3.3",
    "ms-rest-azure": "^2.5.5"
  }
}

应指定以下包:The following packages should be specified:

程序包Package 说明Description
azure-arm-mediaservices Azure 媒体服务 SDK。Azure Media Services SDK.
为确保使用的是最新的 Azure 媒体服务包,请选中 NPM install azure-arm-mediaservicesTo make sure you are using the latest Azure Media Services package, check NPM install azure-arm-mediaservices.
azure-storage 存储 SDK。Storage SDK. 将文件上传到资产时使用。Used when uploading files into assets.
ms-rest-azure 用于登录。Used to sign in.

可以运行以下命令以确保使用的是最新的包:You can run the following command to make sure you are using the latest package:

npm install azure-arm-mediaservices

连接到 Node.js 客户端Connect to Node.js client

  1. 使用偏好的编辑器创建一个 .js 文件。Create a .js file using your favorite editor.
  2. 打开该文件并粘贴以下代码。Open the file and paste the following code.
  3. 将“终结点配置”部分中的值设置为从访问 API 获得的值。Set the values in the "endpoint config" section to values you got from access APIs.
'use strict';

const MediaServices = require('azure-arm-mediaservices');
const msRestAzure = require('ms-rest-azure');
const msRest = require('ms-rest');
const azureStorage = require('azure-storage');

// endpoint config
// make sure your URL values end with '/'
const armAadAudience = "";
const aadEndpoint = "";
const armEndpoint = "";
const subscriptionId = "";
const accountName = "";
const region = "";
const aadClientId = "";
const aadSecret = "";
const aadTenantId = "";
const resourceGroup = "";

let azureMediaServicesClient;

///////////////////////////////////////////
//     Entrypoint for sample script      //
///////////////////////////////////////////

msRestAzure.loginWithServicePrincipalSecret(aadClientId, aadSecret, aadTenantId, {
  environment: {
    activeDirectoryResourceId: armAadAudience,
    resourceManagerEndpointUrl: armEndpoint,
    activeDirectoryEndpointUrl: aadEndpoint
  }
}, async function(err, credentials, subscriptions) {
    if (err) return console.log(err);
    azureMediaServicesClient = new MediaServices(credentials, subscriptionId, armEndpoint, { noRetryPolicy: true });
    
    console.log("connected");

});

运行应用程序Run your app

打开命令提示符。Open a command prompt. 浏览到示例的目录,然后执行以下命令:Browse to the sample's directory, and execute the following commands:

npm install 
node index.js

另请参阅See also

后续步骤Next steps

浏览媒体服务 Node.js 参考文档并查看示例,了解如何将媒体服务 API 与 Node.js 配合使用。Explore the Media Services Node.js ref documentation and check out samples that show how to use Media Services API with node.js.