for solutions to issues with using Media Services.
page.
This article shows how to create a Media Services Asset. You will use an asset to hold media content for encoding and streaming. To learn more about Media Services assets, read Assets in Azure Media Services v3
Use the following methods to create a Media Services asset.
Creating assets in the portal is as simple as uploading a file.
Upload videos
You should have a media services account, a storage account, and a default streaming endpoint.
- Navigate to the Media Services account you want to work with.
- Select Assets. Assets are the containers that are used to house your media content.
- Select Upload. The Upload new assets screen will appear.
- Select the storage account you created for the Media Services account from the Storage account dropdown menu. It should be selected by default.
- Select the file folder icon next to the Upload files field.
- Select the media files you want to use. An asset will be created for every video you upload. The name of the asset will start with the name of the video and will be appended with a unique identifier. You could upload the same video twice and it will be located in two different assets.
- You must agree to the statement "I have all the rights to use the content/file, and agree that it will be handled per the Online Services Terms and the Microsoft Privacy Statement." Select I agree and upload.
- Select Continue upload and close, or Close if you want to watch the video upload progress.
- Repeat this process for each of the files you want to stream.
Create an asset
az ams asset create -a <amsAccountName> -g <resourceGroupName> -n <myAsset>
For more information about this command, see the CLI reference.
Using REST
Create an asset with REST
See the REST API for creating an asset.
Using cURL
The following Azure cURL command creates a new Media Services asset. Replace the values subscriptionID, resourceGroup, and amsAccountName with values you are currently working with. Give your asset a name by setting assetName here.
curl -X PUT 'https://management.azure.com/subscriptions/00000000-0000-0000-000000000000/resourceGroups/resourceGroupName/providers/Microsoft.Media/mediaServices/amsAccountName/assets/myOutputAsset?api-version=2018-07-01' -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"properties": {"description": "",}}'
Create an asset with .NET
The following Azure .NET command creates a new Media Services asset. Replace the values subscriptionID, resourceGroup, and amsAccountName with values you are currently working with. 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.
BlobContainerClient container = new BlobContainerClient(sasUri);
BlobClient blob = container.GetBlobClient(Path.GetFileName(fileToUpload));
// Use Strorage API to upload the file into the container in storage.
await blob.UploadAsync(fileToUpload);
return asset;
}
Create an asset with Python
The functions in the Python code snippets assume that you have:
- Imported the necessary modules. You may not need all of the modules shown here. If the code below doesn't use the module, you can omit it.
- Created and edited an .env file that contains your authentication values. You can get a sample.env file from the Media Services Python samples.
- Read and instantiated environment variables by using load_env() as below. Depending on what you are doing, you may or may not need some of the variables.
- Created a Media Services client as below.
from dotenv import load_dotenv
from azure.identity import DefaultAzureCredential
from azure.mgmt.media import AzureMediaServices
from azure.mgmt.media.models import (Asset)
import os
#Get environment variables
load_dotenv()
subscriptionId = os.getenv("SUBSCRIPTIONID")
accountName=os.getenv("ACCOUNTNAME")
resourceGroupName=os.getenv("RESOURCEGROUP")
clientId = os.getenv("AZURE_CLIENT_ID")
storageAccountName=os.getenv("STORAGEACCOUNTNAME")
# Create the Media Services client and authenticate using the DefaultAzureCredential
default_credential = DefaultAzureCredential()
client = AzureMediaServices(default_credential, subscriptionId)
#Create an Asset object
#From SDK
# Asset(*, alternate_id: Optional[str] = None, description: Optional[str] = None, container: Optional[str] = None,
# storage_account_name: Optional[str] = None, **kwargs)
assetName = "MyAsset"
assetObj = Asset(alternate_id="myAlternateId",description="My description")
#From SDK
#create_or_update(resource_group_name: str, account_name: str, asset_name: str, parameters: "_models.Asset", **kwargs: Any) -> _models.Asset
def createAsset(account_name, resource_group_name, asset_name,asset):
thisAsset = client.assets.create_or_update(account_name, resource_group_name, asset_name,asset)
createAsset(resourceGroupName,accountName,assetName,assetObj)