.NET SDK
本章节介绍.NET SDK,您可以参考本章节进行快速集成开发。
准备工作
- 注册华为账号并开通华为云,并完成实名认证,账号不能处于欠费或冻结状态。
- 已具备开发环境,.NET SDK 适用于.NET Standard 2.0 及其以上版本;C# 4.0 及其以上版本。
- 登录“我的凭证 > 访问秘钥”页面,获取Access Key(AK)和Secret Access Key(SK)。
图1 获取AK、SK
- 登录“我的凭证”页面,获取“IAM用户名”、“账号名”以及待使用区域的“项目ID”。调用服务时会用到这些信息,请提前保存。
本样例以“华北-北京四”区域为例,获取对应的项目ID(project_id)。
图2 我的凭证
安装SDK
使用SDK前,需要安装“HuaweiCloud.SDK.Core”和“HuaweiCloud.SDK.Frs”,有两种安装方式,分别如下。
- 使用 .NET CLI 工具
dotnet add package HuaweiCloud.SDK.Core dotnet add package HuaweiCloud.SDK.Frs
- 使用 Package Manager
Install-Package HuaweiCloud.SDK.Core Install-Package HuaweiCloud.SDK.Frs
开始使用
在开始使用之前,请确保您安装的是最新版本的SDK。使用过时的版本可能会导致兼容性问题或无法使用最新功能。您可以通过运行以下命令来检查并更新SDK至最新版本。
dotnet list package dotnet add package HuaweiCloud.SDK.Core --version * dotnet add package HuaweiCloud.SDK.Frs --version *
详细的SDK介绍,使用异步客户端,配置日志等操作请参见SDK中心、.NET SDK使用指导、.NET SDK视频指导。
- 导入依赖模块
using System; using System.Collections.Generic; using HuaweiCloud.SDK.Core; using HuaweiCloud.SDK.Core.Auth; using HuaweiCloud.SDK.Frs; using HuaweiCloud.SDK.Frs.V2; using HuaweiCloud.SDK.Frs.V2.Model;
- 配置客户端连接参数
- 默认配置
// 使用默认配置 var config = HttpConfig.GetDefaultConfig();
- 网络代理(可选)
// 根据需要配置网络代理 config.ProxyHost = "proxy.huaweicloud.com"; config.ProxyPort = 8080; config.ProxyUsername = "test"; config.ProxyPassword = "test";
- 超时配置(可选)
// 默认超时时间为120秒,可根据需要调整 config.Timeout = 120;
- SSL配置(可选)
// 根据需要配置是否跳过SSL证书验证 config.IgnoreSslVerification = true;
- 默认配置
- 配置认证信息
配置AK、SK信息。华为云通过AK识别用户的身份,通过SK对请求数据进行签名验证,用于确保请求的机密性、完整性和请求者身份的正确性。AK、SK获取方法请参见准备工作。
const string ak = Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_AK"); const string sk = Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_SK"); var auth = new BasicCredentials(ak, sk);
- 认证用的 ak 和sk 硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全。
- 本示例以 ak 和 sk 保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
图3 Windows环境新建环境变量
- 初始化客户端
- 指定云服务region方式(推荐)
// 初始化指定云服务的客户端 {Service}Client ,以初始化FRS服务的 FrsClient 为例 var client = FrsClient.NewBuilder() .WithCredential(auth) .WithRegion(FrsRegion.ValueOf("cn-north-4")) .WithHttpConfig(config) .Build();
- 指定云服务endpoint方式
// 指定终端节点,以FRS服务北京四的 endpoint 为例 String endpoint = "https://face.cn-north-4.myhuaweicloud.com"; // 初始化客户端认证信息,需要填写相应 projectId,以初始化 BasicCredentials 为例 var auth = new BasicCredentials(ak, sk, projectId); // 初始化指定云服务的客户端 {Service}Client,以初始化FRS服务的 FrsClient 为例 var client = FrsClient.NewBuilder() .WithCredential(auth) .WithEndPoint(endpoint) .WithHttpConfig(config) .Build();
endpoint是华为云各服务应用区域和各服务的终端节点,详情请查看 地区和终端节点 。
- 指定云服务region方式(推荐)
- 发送请求并查看响应
// 以调用人脸检测接口 DetectFaceByBase64 为例 var req = new DetectFaceByBase64Request { }; req.Body = new FaceDetectBase64Req() { ImageBase64 = "图片的base64编码" }; try { var resp = client.DetectFaceByBase64(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); }
使用人脸比对SDK时,image1、image2参数需为相同类型,即同为url、base64或file。
- 异常处理
表1 异常处理 一级分类
一级分类说明
二级分类
二级分类说明
ConnectionException
连接类异常
HostUnreachableException
网络不可达、被拒绝。
SslHandShakeException
SSL认证异常。
RequestTimeoutException
响应超时异常
CallTimeoutException
单次请求,服务器处理超时未返回。
RetryOutageException
在重试策略消耗完成后,仍无有效的响应。
ServiceResponseException
服务器响应异常
ServerResponseException
服务端内部错误,Http响应码:[500,]。
ClientRequestException
请求参数不合法,Http响应码:[400, 500)
// 捕获和处理不同类型的异常 try { var resp = client.DetectFaceByBase64(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); }
SDK demo代码解析
- 人脸检测
var req = new DetectFaceByBase64Request { }; req.Body = new FaceDetectBase64Req() { ImageBase64 = "图片的base64编码" }; try { var resp = client.DetectFaceByBase64(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); }
- 人脸比对
var req = new CompareFaceByBase64Request { }; req.Body = new FaceCompareBase64Req() { Image1Base64 = "图片1的base64编码", Image2Base64 = "图片2的base64编码" }; try { var resp = client.CompareFaceByBase64(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); }
- 人脸搜索
var req = new SearchFaceByBase64Request { }; req.Body = new FaceSearchBase64Req() { ImageBase64 = "图片的base64编码" }; try { var resp = client.SearchFaceByBase64(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); }
- 创建人脸库
var req = new CreateFaceSetRequest { }; req.Body = new CreateFaceSetReq() { FaceSetName = "人脸库名称" }; try { var resp = client.CreateFaceSet(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); }
- 查询人脸库
var req = new ShowFaceSetRequest { FaceSetName = "人脸库名称" }; try { var resp = client.ShowFaceSet(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); }
- 查询所有人脸库
var req = new ShowAllFaceSetsRequest { }; try { var resp = client.ShowAllFaceSets(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); }
- 删除人脸库
var req = new DeleteFaceSetRequest { FaceSetName = "人脸库名称" }; try { var resp = client.DeleteFaceSet(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); }
- 添加人脸
var req = new AddFacesByBase64Request { }; req.Body = new AddFacesBase64Req() { ImageBase64 = "图片的base64编码" }; try { var resp = client.AddFacesByBase64(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); }
- 删除人脸
var req = new DeleteFaceByFaceIdRequest { FaceSetName = "人脸库名称", FaceId = "人脸ID" }; try { var resp = client.DeleteFaceByFaceId(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); }
- 批量删除人脸
var req = new BatchDeleteFacesRequest { }; req.Body = new DeleteFacesBatchReq() { Filter = "过滤条件" }; try { var resp = client.BatchDeleteFaces(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); }
- 更新人脸
var req = new UpdateFaceRequest { }; req.Body = new UpdateFaceReq() { FaceId = "人脸库ID" }; try { var resp = client.UpdateFace(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); } }
- 查询人脸
var req = new ShowFacesByFaceIdRequest { FaceSetName = "人脸库名称", FaceId = "人脸ID" }; try { var resp = client.ShowFacesByFaceId(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); }
- 动作活体检测
var req = new DetectLiveByBase64Request { }; req.Body = new LiveDetectBase64Req() { Actions = "动作代码顺序列表", VideoBase64 = "视频数据的base64编码" }; try { var resp = client.DetectLiveByBase64(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); }
- 静默活体检测
var req = new DetectLiveFaceByBase64Request { }; req.Body = new LiveDetectFaceBase64Req() { ImageBase64 = "图片的base64编码" }; try { var resp = client.DetectLiveFaceByBase64(req); var respStatusCode = resp.HttpStatusCode; Console.WriteLine(respStatusCode); } catch (RequestTimeoutException requestTimeoutException) { Console.WriteLine(requestTimeoutException.ErrorMessage); } catch (ServiceResponseException clientRequestException) { Console.WriteLine(clientRequestException.HttpStatusCode); Console.WriteLine(clientRequestException.ErrorCode); Console.WriteLine(clientRequestException.ErrorMsg); } catch (ConnectionException connectionException) { Console.WriteLine(connectionException.ErrorMessage); }
SDK代码自动生成
API Explorer提供API检索及平台调试,支持全量快速检索、可视化调试、帮助文档查看和在线咨询。
您只需要在API Explorer中修改接口参数,即可自动生成对应的代码示例。