教程:使用媒体服务托管标识访问存储

Media Services logo v3


警告

Azure 媒体服务将于 2024 年 6 月 30 日停用。 有关详细信息,请参阅 AMS 停用指南

注意

托管标识仅适用于使用 v3 API 创建的媒体服务帐户。 如果你使用的是 v2 API,并且想要使用托管标识,请从 v2 迁移到 v3 关于从媒体服务 v2 迁移到 v3 的介绍

如果要在存储帐户配置为阻止来自未知 IP 地址的请求时访问该存储帐户,必须向媒体服务帐户授予访问该存储帐户的权限。 按照下列步骤为媒体服务帐户创建一个托管标识,然后使用媒体服务 CLI 向该标识授予访问存储的权限。

Media Services account uses a Managed Identity to access storage

本教程使用 2020-05-01 媒体服务 API。

概述

警告

不能将 JobInputHTTP 或 SAS URL 用于未与媒体服务帐户关联的存储帐户。 这些内容旨在为拥有可使用 HTTP(S) 访问的现有内容的客户提供便利,例如,如果你有媒体文件在面向公众的服务器上或存储在其他云提供商处。 如果正在构建新的解决方案,请使用“资产”作为作业输入。

若要保护对媒体服务使用的存储帐户的访问,请执行以下操作:

  • 将存储帐户配置为拒绝所有 IP 地址(或仅允许客户网络中的 IP 地址)
  • 将存储帐户配置为允许访问“AzureServices”
  • 将媒体服务配置为使用托管标识来访问存储帐户
  • 将媒体内容上传到“媒体服务资产”
  • 创建使用“媒体服务资产”作为作业输入的编码作业。 不要使用 SAS URLS 或 JobInputHTTP。

登录 Azure

要使用本文中的任何命令,首先必须登录到要使用的订阅。

登录 Azure。 使用此命令时,系统会提示你输入想要使用的订阅。

az login

设置订阅

使用此命令设置要使用的订阅。

使用 CLI 设置 Azure 订阅

在以下命令中,为媒体服务帐户提供想要使用的 Azure 订阅 ID。

az account set --subscription <subscriptionName>

资源名称

在开始之前,确定将要创建的资源的名称。 这些名称应该容易被识别成一组,尤其是如果你在完成测试后不打算使用它们。 许多资源类型的命名规则是不同的,因此最好始终采用全部小写的形式。 例如,“mediatest1rg”表示资源组名称,而“mediatest1stor”表示存储帐户名称。 对本文中的每个步骤使用相同的名称。

你将看到以下命令中引用了这些名称。 所需资源名称包括:

  • myRG
  • myStorageAccount
  • myAmsAccount
  • location

注意

上述连字符仅用于分隔引导词。 由于 Azure 服务中的命名资源不一致,因此在命名资源时请勿使用连字符。 此外,也不用创建区域名称。 区域名称由 Azure 确定。

列出 Azure 区域

如果不确定要使用的实际区域名称,请使用此命令获取列表:

使用此命令列出适用于你的帐户的区域。

az account list-locations --query "[].{DisplayName:displayName, Name:name}" -o table

序列

以下每个步骤都是按特定顺序完成的,因为你在序列的下一步中使用了来自 JSON 响应的一个或多个值。

创建存储帐户

你将创建的媒体服务帐户必须具有与之关联的存储帐户。 首先为媒体服务帐户创建存储帐户。 你将在后续步骤中使用替换 myStorageAccount 的存储帐户名称。

使用 CLI 创建 Azure 存储帐户

使用以下命令创建一个 Azure 存储帐户。

若要创建存储帐户,必须先在某个位置创建一个资源组。

若要列出可用位置,请使用以下命令:

使用 CLI 列出可用位置

若要列出可用位置,请使用以下命令:

az account list-locations

使用 CLI 创建资源组

若要创建资源组,请使用以下命令:

az group create -n <resourceGroupName> --location chooseLocation

选择 SKU

还需要为存储帐户选择 SKU。 可以列出存储帐户。

从以下列表中选择 SKU:Standard_LRS、Standard_GRS、Standard_RAGRS、Premium_LRS。

  • myStorageAccount 更改为长度少于 24 个字符的唯一名称。
  • chooseLocation 更改为要使用的区域。
  • chooseSKU 更改为首选 SKU。
az storage account create -n <myStorageAccount> -g <resourceGroup>  --location <chooseLocation> --sku <chooseSKU>

创建包含服务主体(托管标识)的媒体服务帐户

现在,创建包含服务主体(也称为“托管标识”)的媒体服务帐户。

重要

请务必记住在命令中使用 --mi 标志。 否则,你将无法找到 principalId 供稍后步骤使用。

以下 Azure CLI 命令创建新的媒体服务帐户。 将下列值替换为你要使用的名称:your-media-services-account-nameyour-storage-account-nameyour-resource-group-name。 该命令假定你已创建了资源组和存储帐户。

它为媒体服务帐户提供了一个具有 --mi-system-assigned 标记的系统分配的托管标识。


az ams account create --name <your-media-services-account-name> --resource-group <your-resource-group-name> --mi-system-assigned --storage-account <your-storage-account-name>

示例 JSON 响应:

{
  "encryption": {
    "keyVaultProperties": null,
    "type": "SystemKey"
  },
  "id": "/subscriptions/00000000-0000-0000-0000-00000000/resourceGroups/your-resource-group/providers/Microsoft.Media/mediaservices/your-media-services-account-name",
  "identity": {
    "principalId": "00000000-0000-0000-0000-00000000",
    "tenantId": "00000000-0000-0000-0000-00000000",
    "type": "SystemAssigned"
  },
  "location": "your-region",
  "mediaServiceId": "00000000-0000-0000-0000-00000000",
  "name": "your-media-services-account-name",
  "resourceGroup": "your-resource-group",
  "storageAccounts": [
    {
      "id": "/subscriptions/00000000-0000-0000-0000-00000000/resourceGroups/mediatest1rg/providers/Microsoft.Storage/storageAccounts/your-storage-account-name",
      "resourceGroup": "your-resource-group",
      "type": "Primary"
    }
  ],
  "storageAuthentication": "System",
  "systemData": {
    "createdAt": "2021-05-14T21:25:12.3492071Z",
    "createdBy": "you@example.com",
    "createdByType": "User",
    "lastModifiedAt": "2021-05-14T21:25:12.3492071Z",
    "lastModifiedBy": "you@example.com",
    "lastModifiedByType": "User"
  },
  "tags": null,
  "type": "Microsoft.Media/mediaservices"
}

向媒体服务托管标识授予访问存储帐户的权限

向媒体服务托管标识授予访问存储帐户的权限。 有三个命令:

获取(显示)媒体服务帐户的托管标识

下面的第一个命令显示媒体服务帐户的托管标识,它是命令返回的 JSON 中列出的 principalId

此命令显示媒体服务帐户的所有属性。

az ams account show --name <your-media-services-account-name> --resource-group <your-resource-group>

注意

如果已将访问角色分配给媒体服务帐户,则此行会返回 "storageAuthentication": "ManagedIdentity"

示例 JSON 响应:

{
  "encryption": {
    "keyVaultProperties": null,
    "type": "SystemKey"
  },
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/your-resource-group-name/providers/Microsoft.Media/mediaservices/your-media-services-account",
  "identity": {
    "principalId": "00000000-0000-0000-0000-000000000000",
    "tenantId": "00000000-0000-0000-0000-000000000000",
    "type": "SystemAssigned"  //Type will show "Managed Identity" if you have assigned a role to the Media Services account.
  },
  "location": "your-region",
  "mediaServiceId": "00000000-0000-0000-0000-000000000000",
  "name": "your-media-services-account",
  "resourceGroup": "your-resource-group-name",
  "storageAccounts": [
    {
      "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/your-resource-group-name/providers/Microsoft.Storage/storageAccounts/your-storage-account-name",
      "resourceGroup": "your-resource-group-name",
      "type": "Primary"
    }
  ],
  "storageAuthentication": "System", //If you have assigned access roles to the account, this line will return storageAuthentication": "ManagedIdentity"
  "systemData": {
    "createdAt": "2021-05-14T21:25:12.3492071Z",
    "createdBy": "you@example.com",
    "createdByType": "User",
    "lastModifiedAt": "2021-05-14T21:25:12.3492071Z",
    "lastModifiedBy": "you@example.com",
    "lastModifiedByType": "User"
  },
  "tags": null,
  "type": "Microsoft.Media/mediaservices"
}

创建存储 Blob 参与者角色分配

以下命令创建存储 Blob 参与者角色。

assignee 更改为 principalId。 该命令假定你已创建了资源组和存储帐户。 请将 your-resource-group-nameyour-storage-account-name 用作 scope 值的一部分,如以下命令所示:

az role assignment create --assignee 00000000-0000-0000-000000000000 --role "Storage Blob Data Contributor" --scope "/subscriptions/00000000-0000-0000-000000000000/resourceGroups/<your-resource-group-name>/providers/Microsoft.Storage/storageAccounts/<your-storage-account-name>"

示例 JSON 响应:

{
  "canDelegate": null,
  "condition": null,
  "conditionVersion": null,
  "description": null,
  "id": "/subscriptions/00000000-0000-0000-000000000000/resourceGroups/your-resource-group-name/providers/Microsoft.Storage/storageAccounts/your-storage-account-name/providers/Microsoft.Authorization/roleAssignments/00000000-0000-0000-000000000000",
  "name": "00000000-0000-0000-000000000000",
  "principalId": "00000000-0000-0000-000000000000",
  "principalType": "ServicePrincipal",
  "resourceGroup": "your-resource-group-name",
  "roleDefinitionId": "/subscriptions/00000000-0000-0000-000000000000/providers/Microsoft.Authorization/roleDefinitions/00000000-0000-0000-000000000000",
  "scope": "/subscriptions/00000000-0000-0000-000000000000/resourceGroups/your-resource-group-name/providers/Microsoft.Storage/storageAccounts/your-storage-account-name",
  "type": "Microsoft.Authorization/roleAssignments"
}

创建读取者角色分配

以下命令创建读者角色。

assignee 更改为 principalId。 该命令假定你已创建了资源组和存储帐户。 请将 your-resource-group-nameyour-storage-account-name 用作 scope 值的一部分,如以下命令所示:

az role assignment create --assignee 00000000-0000-0000-000000000000 --role "Reader" --scope "/subscriptions/00000000-0000-0000-000000000000/resourceGroups/your-resource-group-name/providers/Microsoft.Storage/storageAccounts/your-storage-account-name"

示例 JSON 响应:

{
  "canDelegate": null,
  "condition": null,
  "conditionVersion": null,
  "description": null,
  "id": "/subscriptions/00000000-0000-0000-000000000000/resourceGroups/your-resource-group-name/providers/Microsoft.Storage/storageAccounts/your-storage-account-name/providers/Microsoft.Authorization/roleAssignments/00000000-0000-0000-000000000000",
  "name": "00000000-0000-0000-000000000000",
  "principalId": "00000000-0000-0000-000000000000",
  "principalType": "Reader",
  "resourceGroup": "your-resource-group-name",
  "roleDefinitionId": "/subscriptions/00000000-0000-0000-000000000000/providers/Microsoft.Authorization/roleDefinitions/00000000-0000-0000-000000000000",
  "scope": "/subscriptions/00000000-0000-0000-000000000000/resourceGroups/your-resource-group-name/providers/Microsoft.Storage/storageAccounts/your-storage-account-name",
  "type": "Microsoft.Authorization/roleAssignments"
}

使用托管标识访问存储帐户

以下命令为媒体服务托管标识授予对存储帐户的访问权限。

在下面的命令中,请将 your-resource-group-name 更改为资源组名称,将 your-media-services-account-name 更改为要使用的媒体服务帐户名称:

az ams account storage set-authentication --storage-auth ManagedIdentity --resource-group <your-resource-group_name> --account-name <your-media-services-account-name>

示例 JSON 响应:

{
  "encryption": {
    "keyVaultProperties": null,
    "type": "SystemKey"
  },
  "id": "/subscriptions/00000000-0000-0000-00000000/resourceGroups/your-resource-group-name/providers/Microsoft.Media/mediaservices/your-storage-account-name",
  "identity": null,
  "location": "China East 2",
  "mediaServiceId": "00000000-0000-0000-00000000",
  "name": "your-media-services-account",
  "resourceGroup": "your-resource-group-name",
  "storageAccounts": [
    {
      "id": "/subscriptions/2b461b25-f7b4-4a22-90cc-d640a14b5471/resourceGroups/your-resource-group-name/providers/Microsoft.Storage/storageAccounts/your-storage-account-name",
      "resourceGroup": "your-resource-group-name",
      "type": "Primary"
    }
  ],
  "storageAuthentication": "ManagedIdentity",
  "systemData": {
    "createdAt": "2021-05-17T19:15:00.8850297Z",
    "createdBy": "you@example.com",
    "createdByType": "User",
    "lastModifiedAt": "2021-05-17T21:23:11.3863627Z",
    "lastModifiedBy": "you@example.com",
    "lastModifiedByType": "User"
  },
  "tags": null,
  "type": "Microsoft.Media/mediaservices"
}

验证

若要验证帐户是否使用客户管理的密钥进行加密,请查看帐户加密属性:

此命令显示媒体服务帐户的所有属性。

az ams account show --name <your-media-services-account-name> --resource-group <your-resource-group>

注意

如果已将访问角色分配给媒体服务帐户,则此行会返回 "storageAuthentication": "ManagedIdentity"

示例 JSON 响应:

{
  "encryption": {
    "keyVaultProperties": null,
    "type": "SystemKey"
  },
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/your-resource-group-name/providers/Microsoft.Media/mediaservices/your-media-services-account",
  "identity": {
    "principalId": "00000000-0000-0000-0000-000000000000",
    "tenantId": "00000000-0000-0000-0000-000000000000",
    "type": "SystemAssigned"  //Type will show "Managed Identity" if you have assigned a role to the Media Services account.
  },
  "location": "your-region",
  "mediaServiceId": "00000000-0000-0000-0000-000000000000",
  "name": "your-media-services-account",
  "resourceGroup": "your-resource-group-name",
  "storageAccounts": [
    {
      "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/your-resource-group-name/providers/Microsoft.Storage/storageAccounts/your-storage-account-name",
      "resourceGroup": "your-resource-group-name",
      "type": "Primary"
    }
  ],
  "storageAuthentication": "System", //If you have assigned access roles to the account, this line will return storageAuthentication": "ManagedIdentity"
  "systemData": {
    "createdAt": "2021-05-14T21:25:12.3492071Z",
    "createdBy": "you@example.com",
    "createdByType": "User",
    "lastModifiedAt": "2021-05-14T21:25:12.3492071Z",
    "lastModifiedBy": "you@example.com",
    "lastModifiedByType": "User"
  },
  "tags": null,
  "type": "Microsoft.Media/mediaservices"
}

storageAuthentication 属性应显示“ManagedIdentity”。

若要进行其他验证,可检查 Azure 存储日志,查看每个请求使用了哪种身份验证方法。

清理资源

如果不打算使用已创建的资源,请删除资源组。

使用 CLI 删除资源组

az group delete --name <your-resource-group-name>