Search code examples
c#azureazure-media-services

Azure Media Services, GetLocators with V3 api and ODataQuery


I am trying to get all the streaming locators for a given asset using the v3 API and the Microsoft.Azure.Management.Media package but I am getting a bad request error using Odata Queries:

It fails on this line: var locator = client.StreamingLocators.List("webinars", "webinars", new ODataQuery<StreamingLocator>(x=>x.AssetName == assetId));

Microsoft.Azure.Management.Media.Models.ApiErrorException: Operation returned an invalid status code 'BadRequest'

When I use it without the ODataQuery, it returns fine.

public IList<string> GetLocatorForAsset() {
            var assetId = "bb4953cf-4793-4b3c-aed8-ae1bec88a339";
            IList<string> streamingUrls = new List<string>();      

            var locator = client.StreamingLocators.List("webinars", "webinars", new ODataQuery<StreamingLocator>(x=>x.AssetName == assetId));
            ListPathsResponse paths = client.StreamingLocators.ListPaths("webinars", "webinars", locator.FirstOrDefault().Name);

            foreach (StreamingPath path in paths.StreamingPaths) {
                UriBuilder uriBuilder = new UriBuilder();
                uriBuilder.Scheme = "https";
                uriBuilder.Host = "webinars-use2.streaming.media.azure.net";

                uriBuilder.Path = path.Paths[0];
                streamingUrls.Add(uriBuilder.ToString());
            }

            return streamingUrls;

        }
    }

Solution

  • As per media services filtering documentation, user can filter "Streaming Locators" only by "name", "properties.created" and "properties.endTime".

    https://learn.microsoft.com/en-us/azure/media-services/latest/entities-overview#streaming-locators

    enter image description here

    In your example, you are trying to filter using assetId/assetName which is not supported. hence 400 Bad request error. see detailed error example in postman

    enter image description here

    Here is the valid filtering example using Streaming Locator "name" tag.

    NOTE : This is not an asset Tag

    enter image description here

    C# example used to successfully filter streaming locator using "name"

        try
        {
            // GUID need to be specified in single quote. using OData v 3.0
            var odataquery = new ODataQuery<StreamingLocator>("name eq '65a1cb0d-ce7c-4470-93ac-fedf66450ea0'");
            IPage<StreamingLocator> locators = client.StreamingLocators.List("mediatest", "mymediatestaccount", odataquery);
    
            Console.WriteLine(locators.FirstOrDefault().Name);
            Console.WriteLine(locators.FirstOrDefault().StreamingLocatorId);
            Console.WriteLine(locators.FirstOrDefault().Id);
    
            ListPathsResponse paths = client.StreamingLocators.ListPaths("mediatest", "mymediatestaccount", locators.FirstOrDefault().Name);
    
            foreach (StreamingPath path in paths.StreamingPaths)
            {
                UriBuilder uriBuilder = new UriBuilder();
                uriBuilder.Scheme = "https";
                uriBuilder.Host = "webinars-use2.streaming.media.azure.net";
    
                uriBuilder.Path = path.Paths[0];
                Console.WriteLine(uriBuilder.ToString());
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    

    I hope this helps.