Azure Media Services Locator

放肆的年华 提交于 2020-01-25 06:48:21

问题


I am trying to use azure media services, using c# code, for the most part using the examples I can upload an asset, publish it and then get a locator to pull it back down.

I am a bit confused how I can generate a new locator e.g. if the old one expires?

For example If I create a locator now at the time of uploading and encoding, I can get a url by defining this against the CloudMediaContext object.

_context.Locators.Create(
    LocatorType.OnDemandOrigin,
    asset,
    AccessPermissions.Read,
    TimeSpan.FromDays(30));

_context.Locators.Create(
    LocatorType.Sas,
    asset,
    AccessPermissions.Read,
    TimeSpan.FromDays(30));

After 30 days the url will expire, how can I get a new one at this time?

Many thanks for any help you can give :)


回答1:


It looks like you're using the v2 version of the Media Services API. After a locator expires you can create a new one by first locating the asset you want to add the locator to and then creating the locator for that asset. Here's an example:

using Microsoft.WindowsAzure.MediaServices.Client;
using System;
using System.Linq;

namespace GenerateLocatorV2
{
    class Program
    {
        private static CloudMediaContext _context = null;
        static void Main(string[] args)
        {
            string tenantDomain = args[0];
            string RESTendpoint = args[1];
            string assetId = args[2];

            // Specify your AAD tenant domain, for example "microsoft.onmicrosoft.com"
            AzureAdTokenCredentials tokenCredentials = new AzureAdTokenCredentials(tenantDomain, AzureEnvironments.AzureCloudEnvironment);

            AzureAdTokenProvider tokenProvider = new AzureAdTokenProvider(tokenCredentials);

            // Specify your REST API endpoint, for example "https://accountname.restv2.westcentralus.media.azure.net/API"
            _context = new CloudMediaContext(new Uri(RESTendpoint), tokenProvider);

            IAsset asset = GetAsset(assetId);

            // Always try to reuse access policies.  You only need to configure one per type of access (30 day, read for example). 
            var tempPolicyId = from a in _context.AccessPolicies
                               where a.Name == "30DayRead"
                               select a;

            IAccessPolicy policy = null;

            if (tempPolicyId.Count() < 1)
            {
                // This will likely only run once ever to create the policy with this specific name.
                policy = _context.AccessPolicies.Create("30DayRead",
                TimeSpan.FromDays(30),
                AccessPermissions.Read);
            }
            else
            {
                // The policy exists already and has been found.
                policy = tempPolicyId.FirstOrDefault();
            }

            // Create a locator to the streaming content on an origin. 
            ILocator originLocator = _context.Locators.CreateLocator(LocatorType.OnDemandOrigin, asset,
                policy,
                DateTime.UtcNow.AddMinutes(-5));
        }
        private static IAsset GetAsset(string assetId)
        {
            var tempAsset = from a in _context.Assets
                            where a.Id == assetId
                            select a;
            IAsset asset = tempAsset.SingleOrDefault();
            return asset;
            // This function can be done in a single line by code like:
            // IAsset asset = _context.Assets.Where(a => a.Id == assetId).FirstOrDefault();
        }
    }
}




来源:https://stackoverflow.com/questions/58607084/azure-media-services-locator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!