[英]Azure Media Services Locator
我正在尝试使用 azure 媒体服务,使用 c# 代码,大多数情况下使用示例我可以上传资产,发布它,然后获取定位器将其拉回。
我有点困惑如何生成一个新的定位器,例如如果旧的定位器过期了?
例如,如果我现在在上传和编码时创建一个定位器,我可以通过针对 CloudMediaContext object 定义它来获得 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 天后过期,此时我如何才能获得新的?
非常感谢您提供的任何帮助:)
看起来您正在使用媒体服务 API 的 v2 版本。 定位器过期后,您可以通过首先定位要添加定位器的资产然后为该资产创建定位器来创建新定位器。 这是一个例子:
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.