我正在尝试使用适用于Azure Media Services的v3 API通过名称获取资产。带有ODataQuery的GetAsync和ListAsync都无法找到该项目,但是,使用ListAsync和ListNextAsync翻页所有资产最终将找到它。这是在一个拥有30至4万资产的媒体服务帐户中。我正在寻找有关我在这里做错事情的想法/建议。我将使用v3 API,因为这是一个dotnet核心应用程序,而v2 API似乎停留在框架4.5上。可以在下面找到示例程序。
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Management.Media;
using Microsoft.Azure.Management.Media.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest.Azure.Authentication;
using Microsoft.Rest.Azure.OData;
namespace MediaServicesTestApp
{
class Program
{
static async Task Main(string[] args)
{
var arm_endpoint = "https://management.azure.com";
var credentials = new ClientCredential("xxxxx", "yyyyy");
var token = await ApplicationTokenProvider.LoginSilentAsync("aaaaa", credentials, ActiveDirectoryServiceSettings.Azure);
IAzureMediaServicesClient client = new AzureMediaServicesClient(new Uri(arm_endpoint), token)
{
SubscriptionId = "sssss",
};
var resourceGroupName = "zzzzz";
var accountName = "bbbbb";
try
{
var name = "b98b7c8f-4ee3-42c5-b0bd-d1cb08a3776c";
// neither of these queries work
var cdataQuery = new ODataQuery<Asset>(a => a.Name == name);
/*
var cdataQuery = new ODataQuery<Asset>()
{
Filter = $"name eq '{name}'"
};
*/
var assets = await client.Assets.ListAsync(resourceGroupName, accountName, cdataQuery, CancellationToken.None);
if (assets.Any())
{
Console.WriteLine($"Found Asset with name={name} using ListAsync with Query");
}
// neither does this
if (await client.Assets.GetAsync(
resourceGroupName,
accountName,
name,
CancellationToken.None) != null)
{
Console.WriteLine($"Found Asset with name={name} using GetAsync");
}
// this works but is gross and inefficient
assets = await client.Assets.ListAsync(resourceGroupName, accountName, null, CancellationToken.None);
var i = 0;
while(assets.All(x => x.Name != name) && assets.NextPageLink != null)
{
assets = await client.Assets.ListNextAsync(assets.NextPageLink, CancellationToken.None);
}
var asset = assets.FirstOrDefault();
if (asset != null)
{
Console.WriteLine($"Found Asset with name={name} iterating over ListAsync");
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Management.Media;
using Microsoft.Azure.Management.Media.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest.Azure;
using Microsoft.Rest.Azure.Authentication;
using Microsoft.Rest.Azure.OData;
namespace ams_searchbyname
{
class Program
{
static async Task Main(string[] args)
{
string clientid = "c7f852b9-*****-4d60-83ab-********";
string clientsecret = "dGjeU?Qy.pYD7****rQv0=8gM/";
string tenantdomain = "*****.microsoft.com";
var arm_endpoint = "https://management.azure.com";
var credentials = new ClientCredential(clientid,clientsecret);
var token = await ApplicationTokenProvider.LoginSilentAsync(tenantdomain, credentials, ActiveDirectoryServiceSettings.Azure);
IAzureMediaServicesClient client = new AzureMediaServicesClient(new Uri(arm_endpoint), token)
{
SubscriptionId = "*******-e859-******-8d84-5e5fe29f4c68",
};
var resourceGroupName = "pa*********ei";
var accountName = "pan*******dia";
try
{
// searchname
var name = "2020";
// neither of these queries work
//var cdataQuery = new ODataQuery<Asset>(a => a.Name == name);
ODataQuery<Asset> odataQuery = new ODataQuery<Asset>();
//odataQuery.OrderBy = "Name desc";
//odataQuery.Filter = "name eq " + name;
odataQuery = "$filter=name gt '2020'&$orderby=Properties/Created desc";
/*
var cdataQuery = new ODataQuery<Asset>()
{
Filter = $"name eq '{name}'"
};
*/
var assets = await client.Assets.ListAsync(resourceGroupName, accountName, odataQuery);
if (assets.Any())
{
Console.WriteLine($"Found Asset with name={name} using ListAsync with Query");
}
// neither does this
// !!!I don't repair code in this region
if (await client.Assets.GetAsync(
resourceGroupName,
accountName,
name,
CancellationToken.None) != null)
{
Console.WriteLine($"Found Asset with name={name} using GetAsync");
Console.WriteLine($"Contain cdataQuery");
}
// this works but is gross and inefficient
assets = await client.Assets.ListAsync(resourceGroupName, accountName, null, CancellationToken.None);
var i = 0;
while (assets.All(x => x.Name != name) && assets.NextPageLink != null)
{
assets = await client.Assets.ListNextAsync(assets.NextPageLink, CancellationToken.None);
}
var asset = assets.FirstOrDefault();
if (asset != null)
{
Console.WriteLine($"Found Asset with name={name} iterating over ListAsync");
Console.WriteLine($"Asset Name: "+asset.Name+"");
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}