本文介绍如何创建媒体服务资产。This article shows how to create a Media Services Asset.你将使用资产来保存用于编码和流式传输的媒体内容。You will use an asset to hold media content for encoding and streaming.若要了解有关媒体服务资产的详细信息,请参阅 Azure 媒体服务 v3 中的资产To learn more about Media Services assets, read Assets in Azure Media Services v3
先决条件Prerequisites
按照创建媒体服务帐户中的步骤操作,创建所需的媒体服务帐户和资源组以创建资产。Follow the steps in Create a Media Services account to create the needed Media Services account and resource group to create an asset.
以下 Azure CLI 命令创建新的媒体服务资产。The following Azure CLI command creates a new Media Services asset.将 assetName、amsAccountName 和 resourceGroup 值替换为当前正在使用的值。Replace the values assetNameamsAccountName and resourceGroup with values you are currently working with.
az ams asset create -n assetName -a amsAccountName -g resourceGroup
示例脚本Example script
#!/bin/bash
# Update the following variables for your own settings:
$resourceGroup=amsResourceGroup
$amsAccountName=amsmediaaccountname
$assetName="myAsset-uniqueID"
$expiry=$(date -u +"%Y-%m-%dT%TZ" -d "+23 hours")
# Create a Media Services Asset to upload content to.
# In the v3 API, Asset names are unique ARM resource identifiers and must be unique to the account.
# It's recommended to use short unique IDs or GUIDs to keep the names unique to the account.
az ams asset create \
-n $assetName \
-a $amsAccountName \
-g $resourceGroup \
# Get the SAS URLs to upload content to the container for the Asset
# Default is 23 hour expiration, but you can adjust with the --expiry flag.
# Max supported is 24 hours.
az ams asset get-sas-urls \
-n $assetName \
-a $amsAccountName \
-g $resourceGroup \
--expiry $expiry\
--permissions ReadWrite \
# Use the az storage modules to upload a local file to the container using the SAS URL from previous step.
# If you are logged in already to the subscription with access to the storage account, you do not need to use the --sas-token at all. Just eliminate it below.
# The container name is in the SAS URL path, and should be set with the -c option.
# Use the -f option to point to a local file on your machine.
# Use the -n option to name the blob in storage.
# Use the --account-name option to point to the storage account name to use
# Use the --sas-token option to place the SAS token after the query string from previous step.
# NOTE that the SAS Token is only good for up to 24 hours max.
# az storage blob upload \
# -c asset-84045780-a71c-4511-801b-711b1a2e76b2 \
# -f C:\Videos\ignite-short.mp4 \
# -n ignite-short.mp4 \
# --account-name mconverticlitest0003 \
# --sas-token "?sv=2015-07-08&sr=c&sig=BvMXDCOjR%2FOP2%2FYi6lVknC4Gcq7fIun5tst8jgED7zY%3D&se=2018-04-25T00:00:00Z&sp=rwl" \
echo "press [ENTER] to continue."
read continue
使用 RESTUsing REST
以下 Azure REST 命令创建新的媒体服务资产。The following Azure REST command creates a new Media Services asset.将 subscriptionID、resourceGroup 和 amsAccountName 值替换为当前正在使用的值。Replace the values subscriptionID, resourceGroup, and amsAccountName with values you are currently working with.通过在此处设置 assetName 来为资产提供名称。Give your asset a name by setting assetName here.
PUT https://management.chinacloudapi.cn/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Media/mediaServices/{amsAccountName}/assets/{assetName}?api-version=2018-07-01
使用 cURLUsing cURL
以下 Azure cURL 命令创建新的媒体服务资产。The following Azure cURL command creates a new Media Services asset.将 subscriptionID、resourceGroup 和 amsAccountName 值替换为当前正在使用的值。Replace the values subscriptionID, resourceGroup, and amsAccountName with values you are currently working with.通过在此处设置 assetName 来为资产提供名称。Give your asset a name by setting assetName here.
本部分介绍如何使用 Postman 创建新的资产。This section shows how to create a new Asset using Postman.
选择“资产” -> “创建或更新资产” 。Select Assets -> Create or update an Asset.
按“发送”。Press Send.将看到包含有关新创建资产的信息的“响应”。You will see the Response with the info about newly created asset.
以下 Azure .NET 命令创建新的媒体服务资产。The following Azure .NET command creates a new Media Services asset.将 subscriptionID、resourceGroup 和 amsAccountName 值替换为当前正在使用的值。Replace the values subscriptionID, resourceGroup, and amsAccountName with values you are currently working with.通过在此处设置 assetName 来为资产提供名称。Give your asset a name by setting assetName here.
private static async Task<Asset> CreateInputAssetAsync(
IAzureMediaServicesClient client,
string resourceGroupName,
string accountName,
string assetName,
string fileToUpload)
{
// In this example, we are assuming that the asset name is unique.
//
// If you already have an asset with the desired name, use the Assets.Get method
// to get the existing asset. In Media Services v3, the Get method on entities returns null
// if the entity doesn't exist (a case-insensitive check on the name).
// Call Media Services API to create an Asset.
// This method creates a container in storage for the Asset.
// The files (blobs) associated with the asset will be stored in this container.
Asset asset = await client.Assets.CreateOrUpdateAsync(resourceGroupName, accountName, assetName, new Asset());
// Use Media Services API to get back a response that contains
// SAS URL for the Asset container into which to upload blobs.
// That is where you would specify read-write permissions
// and the exparation time for the SAS URL.
var response = await client.Assets.ListContainerSasAsync(
resourceGroupName,
accountName,
assetName,
permissions: AssetContainerPermission.ReadWrite,
expiryTime: DateTime.UtcNow.AddHours(4).ToUniversalTime());
var sasUri = new Uri(response.AssetContainerSasUrls.First());
// Use Storage API to get a reference to the Asset container
// that was created by calling Asset's CreateOrUpdate method.
CloudBlobContainer container = new CloudBlobContainer(sasUri);
var blob = container.GetBlockBlobReference(Path.GetFileName(fileToUpload));
// Use Strorage API to upload the file into the container in storage.
await blob.UploadFromFileAsync(fileToUpload);
return asset;
}