Search code examples
c#azureazure-media-services

Azure Media Services Locators


base of my previous question I still don't get locators.

Previous Locator Question

If I create a locator to last 1 hour, when it expires how do I know that the current locator has expired?

I believe I will need to store the assetId so I can later request it.

  • Do I store the expiry date my side too?
  • Or is it when I request it, I get an invalid http status and based of this I create a new locator?
  • What happens to all the expired locators, I read that you can only have 5 of then, so do I need to clean up the expired locators?

Think the main question is how do I create a new locator when the old locator is expired, but how do I know that the old locator is expired?

The code below is what I have for getting a locator, but no way of finding out if the locator has expired.

        // Try and get an access policy and reuse it if found
        var tempPolicyId = from a in _context.AccessPolicies
            where a.Name == PolicyName
            select a;

        IAccessPolicy accessPolicy = null;

        if (tempPolicyId.Count() < 1)
        {
            accessPolicy = _context.AccessPolicies.Create(
                PolicyName,
                TimeSpan.FromMinutes(5), // expire in 5 minutes
                AccessPermissions.List | AccessPermissions.Read);
        }
        else
        {
            accessPolicy = tempPolicyId.FirstOrDefault();
        }
        _context.Locators.CreateLocator(
            LocatorType.OnDemandOrigin,
            asset,
            accessPolicy,
            DateTime.UtcNow.AddMinutes(-5));

        _context.Locators.CreateLocator(
            LocatorType.Sas,
            asset,
            accessPolicy,
            DateTime.UtcNow.AddMinutes(-5));
    }

Your help would be greatly appreciated, thanks


Solution

  • I would highly recommend minimally storing the asset ID. From that you can find out just about anything about the asset. It wouldn't be bad to store the expiry date if you are using short lived locators like you're suggesting. I'd consider 1 hour short lived. Often I create 10 or 100 year locators and then manage access through DRM.

    To tell when a locator is expiring you can call ILocator.ExpirationDateTime.ToUniversalTime() to find the date/time that it will or did expire.

    When a locator is expired you need to delete if it you are at the 5 locator limit before you can add a new one. You can also call ILocator.Update(DateTime newExpiryTime)