【Azure 媒体服务】Media Service的编码示例 -- 创建缩略图子画面的.NET代码调试问题
问题描述
在中国区Azure上,使用Media Service服务,想要使用.NET的代码来对上传视频创建缩略图(Thumbnail) 。
通过官网文档(https://docs.azure.cn/zh-cn/media-services/latest/samples/samples-encoding-reference#create-a-thumbnail-sprite)下载.NET示例,配置 appsettings.json 中的参数,运行却出现(Azure.Identity.AuthenticationFailedException: 'ClientSecretCredential authentication failed: AADSTS90002: )异常。
Azure.Identity.AuthenticationFailedException: 'ClientSecretCredential authentication failed: AADSTS90002: Tenant '********-****-****-****-************' not found. Check to make sure you have the correct tenant ID and are signing into the correct cloud. Check with your subscription administrator, this may happen if there are no active subscriptions for the tenant.
Trace ID: 99b963f7-86a5-4cde-a890-8828eff73000
Correlation ID: 62d4fa3b-92ad-4411-850c-87f562a256b3
Timestamp: 2023-05-10 07:25:55Z'
问题解答
查看.NET项目中的源码,发现获取Credential的代码使用的是 DefaultAzureCredential()。并且 ArmClient 对象也没有指定Azure的运行环境。
var mediaServicesResourceId = MediaServicesAccountResource.CreateResourceIdentifier( subscriptionId: options.AZURE_SUBSCRIPTION_ID.ToString(), resourceGroupName: options.AZURE_RESOURCE_GROUP, accountName: options.AZURE_MEDIA_SERVICES_ACCOUNT_NAME); var credential = new DefaultAzureCredential(includeInteractiveCredentials: true); var armClient = new ArmClient(credential); var mediaServicesAccount = armClient.GetMediaServicesAccountResource(mediaServicesResourceId);
默认情况下,它们都是指向Global Azure,而非China Azure。
所以,解决当前问题的方法就是在DefaultAzureCredential和ArmClient方法中指定中国区Azure为运行环境。
修改这部分代码为为:
var mediaServicesResourceId = MediaServicesAccountResource.CreateResourceIdentifier( subscriptionId: options.AZURE_SUBSCRIPTION_ID.ToString(), resourceGroupName: options.AZURE_RESOURCE_GROUP, accountName: options.AZURE_MEDIA_SERVICES_ACCOUNT_NAME); DefaultAzureCredentialOptions dacOptions = new DefaultAzureCredentialOptions() { AuthorityHost = AzureAuthorityHosts.AzureChina }; var credential = new DefaultAzureCredential(dacOptions); ArmClientOptions armOptions = new ArmClientOptions() { Environment = ArmEnvironment.AzureChina}; var armClient = new ArmClient(credential, options.AZURE_SUBSCRIPTION_ID.ToString(), armOptions); var mediaServicesAccount = armClient.GetMediaServicesAccountResource(mediaServicesResourceId);
注意:使用 DefaultAzureCredential 认证,需要设置以下的环境变量
- AZURE_CLIENT_ID
- AZURE_TENANT_ID
- AZURE_CLIENT_SECRET
关于DefaultAzureCredential方法获取认证参数的顺序,如下图所示:
参考资料
DefaultAzureCredential : https://learn.microsoft.com/en-us/dotnet/api/overview/azure/identity-readme?view=azure-dotnet#defaultazurecredential
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-05-10 【Azure 应用服务】部署Azure Web App时,是否可以替换hostingstart.html文件呢?
2021-05-10 【Azure 应用服务】添加自定义域时,Domain ownership 验证无法通过
2021-05-10 【Azure Spring Cloud】部署Azure spring cloud 失败