Azure媒体服务定位符

问题描述 投票:0回答:1

我正在尝试使用C#代码使用Azure媒体服务,大部分情况下,我可以使用示例来上传资产,发布资产,然后找到定位器以将其拉回。

我有点困惑如何生成新的定位器,例如如果旧的过期了?

例如,如果我现在在上传和编码时创建了一个定位器,则可以通过针对CloudMediaContext对象定义此URL来获取URL。

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

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

URL将在30天后过期,我现在如何获得新的URL?

非常感谢您可以提供的任何帮助:)

c# azure azure-media-services
1个回答
1
投票

看来您正在使用v2版本的Media Services API。定位器过期后,可以先定位要添加定位器的资产,然后为该资产创建定位器,以创建一个新的定位器。这是一个例子:

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();
        }
    }
}


© www.soinside.com 2019 - 2024. All rights reserved.