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

媒体服务徽标 v3


警告

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

注意

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

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

媒体服务帐户使用托管标识访问存储

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

概述

警告

不能将 JobInputHTTP 或 SAS URL 与未与媒体服务帐户关联的存储帐户一起使用。 这些内容旨在方便 现有内容 的客户,例如,如果你有面向公众的服务器上的媒体文件或与其他云提供商一起存储。 如果要生成新解决方案,请使用资产进行作业输入。

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

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

登录到 Azure

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

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

az login

设置订阅

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

使用 CLI 设置 Azure 订阅

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

az account set --subscription <subscriptionName>

资源名称

在开始之前,请确定要创建的资源的名称。 它们应易于识别为一组,尤其是在完成测试后不打算使用它们时。 许多资源类型的命名规则不同,因此最好坚持所有小写。 例如,资源组名称的“mediatest1rg”和存储帐户名称的“mediatest1stor”。 对本文中的每个步骤使用相同的名称。

你将看到以下命令中引用的这些名称。 需要的资源名称如下:

  • myRG
  • myStorageAccount
  • myAmsAccount
  • 位置

注意

上述连字符仅用于分隔指导词。 由于 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、Standard_ZRS、Premium_LRS、Premium_ZRS、Standard_GZRS、Standard_RAGZRS。

  • 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-name,并将 your-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": "West US 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>

获取帮助和支持

可以通过以下方法之一联系媒体服务,并关注我们的更新: