ucloud-csharp-sdk是使用C#开发,用于请求UCloud API的.Net SDK。现已覆盖:
- 云主机 UHost
- 网络 UNet
- 负载均衡 ULB
- 云数据库 UDB
- 对象存储 UFile
- 接入云 UCDN
- 云监控 UMon
- 云硬盘 UDisk
- 短信包 SMS
NuGet(简单方便,自动添加相关配置内容)
PM> Install-Package UCloudSDK
using UCloudSDK;
using UCloudSDK.Models;
使用API前,需要对PublicKey(用户公钥) PrivateKey(用户私钥) BaseUrl(API地址 ,可选,默认为https://api.ucloud.cn) 进行配置。 配置方法有两种:一种是在config文件中;一种是在程序中初始化对象时进行设置。
使用NuGet方式会在app(web).config中自动添加以下内容,根据注释进行配置。
<configSections>
<section name="UcloudSetting" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<UcloudSetting>
<!--用户公钥-->
<add key="PublicKey" value="[email protected]" />
<!--用户私钥-->
<add key="PrivateKey" value="46f09bb9fab4f12dfc160dae12273d5332b5debe" />
<!--API请求地址-->
<add key="BaseUrl" value="https://api.ucloud.cn" />
</UcloudSetting>
构造函数:
UCloud(string publicKey, string privateKey)
UCloud(string publicKey, string privateKey, string baseUrl)
使用方法:
var ucloud=new UCloud("xxx","yyy")
var ucloud=new UCloud("xxx","yyy","https://api.ucloud.cn")
//或实例化后对属性进行设置
ucloud.PublicKey="xxx";
ucloud.PrivateKey="yyy";
ucloud.BaseUrl="https://api.ucloud.cn";
为便于区别和使用,SDK采用了以下约定:
- 类名对应于产品名。比如云主机UHost产品对应的类名为
UHost
- API方法名与文档中方法名一致。具体方法名请参见:http://docs.ucloud.cn/api/apilist.html
- 请求的实体类型名称为方法名+Request。如创建UHost实例,则请求类型为
CreateUHostInstanceRequest
- 返回值的类型名称为方法名+Response。如创建UHost实例,则返回类型为
CreateUHostInstanceResponse
- 请求参数Param.n的类型为
NList
,继承于List<string>
。- 返回值的Array类型:string类型的为
List<string>
,object类型的为List<方法名+object名称>,如在DescribeUHostInstance 中,返回值UHostSet为object集合,则其对应的C#类型为List<DescribeUHostInstanceUHostSet>
- 请求实体类型的构造函数包含所有Required为Yes的参数。比如创建UHost实例方法的构造函数:
CreateUHostInstanceRequest(string region, string imageid, string loginmode)
请求API有三种方法:
//强类型请求及返回,对应于各个产品类
T Execute<T>(IRestRequest request)
//自定义请求参数,强类型返回,使用UCloud基类
T Execute<T>(Dictionary<string, string> dictionary, Method method = Method.GET)
//自定义请求参数,动态内容返回,使用UCloud基类 只支持.Net4及以上
RestResponse<dynamic> Execute(Dictionary<string, string> dictionary, Method method = Method.GET)
注意: 以下以云主机创建UHost实例CreateUHostInstance为例。 配置方式采用的config配置。 其它API类似,或参见
UCloudSDK.Test
中的测试方法。
请求参数及返回值都是强类型。 按照SDK设置约定,所有的方法都类似下形式:
public MethodResponse Method(MethodRequest requestParams)
{
var request = new RestRequest(Method.GET);
request.AddUObject(requestParams);
return Execute<MethodResponse>(request);
}
使用方法如下:
var uhost=new UHost();
//该方法有三个必需(Required为Yes)的请求参数
var entity=new CreateUHostInstanceRequest("cn-north-03","uimage-3wrk30","Password");
//设置其它参数
entity.CPU=2;
entity.Memory=2048;
entity.DiskSpace=10;
//密码需要进行BASE64编码,SDK自动编码
entity.Password="Password1";
entity.Name="UCloudExample01";
entity.ChargeType="Month";
entity.Quantity=1;
//请求API,返回类型为CreateUHostInstanceResponse
var response=uhost.CreateUHostInstance(entity);
//获取RetCode
var retCode=response.RetCode;
//若RetCode不为0,说明API请求失败错误,使用Message查看错误内容
var error = response.RetCode!=0 ? response.Message : "";
请求参数为字典,返回值为强类型
var ucloud=new UCloud();
var dict=new Dictionary<string, string>();
//设置请求参数
dict.Add("Action","CreateUHostInstance");
dict.Add("Region","cn-north-03");
dict.Add("ImageId","uimage-3wrk30");
dict.Add("CPU","2");
dict.Add("Memory","2048");
dict.Add("DiskSpace","10");
dict.Add("LoginMode","Password");
//密码需要进行BASE64编码
var password="Password1";
//使用string.ToBase64()进行编码
dict.Add("Password",password.ToBase64());
dict.Add("Name","UCloudExample01");
dict.Add("ChargeType","Month");
dict.Add("Quantity","1");
//请求API,返回类型为CreateUHostInstanceResponse
var response=ucloud.Execute<CreateUHostInstanceResponse>(dict);
//获取RetCode
var retCode=response.RetCode;
//若RetCode不为0,说明API请求失败错误,使用Message查看错误内容
var error = response.RetCode!=0 ? response.Message : "";
请求参数为字典,返回值为动态类型。适用于SDK未覆盖到的API。
var ucloud=new UCloud();
var dict=new Dictionary<string, string>();
//设置请求参数
dict.Add("Action","CreateUHostInstance");
dict.Add("Region","cn-north-03");
dict.Add("ImageId","uimage-3wrk30");
dict.Add("CPU","2");
dict.Add("Memory","2048");
dict.Add("DiskSpace","10");
dict.Add("LoginMode","Password");
//密码需要进行BASE64编码
var password="Password1";
//使用string.ToBase64()进行编码
dict.Add("Password",password.ToBase64());
dict.Add("Name","UCloudExample01");
dict.Add("ChargeType","Month");
dict.Add("Quantity","1");
//请求API,返回类型为RestResponse<dynamic>
var response=ucloud.Execute(dict);
//之后可用数组的形式获取数据
var entity=response.Data;
//获取RetCode
var retCode=entity["RetCode"];
注意 这里Bucket已创建,名称为bucketName,未创建的话,请先创建Bucket CreateBucket
// 初始化UFile对象
UFile ufile=new UFile(){Bucket="bucketName"};
// 小文件本地路径(小于4M)
string filePath = @"";
// 大文件本地路径(大于4M)
private string bigfilePath = @"";
/// 下载文件保存路径
private string savePath = @"";
// 普通上传文件
ufile.PutFile(filePath);
// 表单上传文件
uflie.PostFile(filePath);
// 分片上传
// 初始化分片上传
var entity = ufile.InitiateMultipartUpload(bigfilePath);
for (int i = 0; i < 100000; i++)
{
if (ufile.PartFile.IsLast)
{
break;
}
// 上传文件分片
ufile.UploadPart(i);
}
// 完成分片上传
var response = ufile.FinishMultipartUpload("newKey");
//若RetCode不为0,说明API请求失败错误,使用ErrMsg查看错误内容
var error = response.RetCode!=0 ? response.ErrMsg: "";
// 下载文件 key为文件在Bucket中的名称
ufile.GetFile("key", savePath);
SMSResponse SendSms(SMSRequest requestParams)
注意
- 发送短信前请先在此设置短信签名,并购买短信包。
- 该方法作为
UCloud
基类方法,在其子类如UHost
,UDB
等类中均可直接调用。
UCloud ucloud = new UCloud();
//号码以Phone.0=xxxx&Phone.1=xxxx的形式发送,所以使用NList
var phone = new NList();
phone.Add("138xxxxxxxx");
phone.Add("189xxxxxxxx");
var content = "你好,Ucloud";
var entity = new SMSRequest(phone, content);
var response = ucloud.SendSms(entity);
除UFile部分Response外,所以的Response继承于UResponse
public class UResponse
{
/// <summary>
/// 执行结果代码
/// <para>
/// 执行成功与否,0 表示成功,其他值则为错误代码.
/// </para>
/// </summary>
public int RetCode { get; set; }
/// <summary>
/// 错误消息.
/// </summary>
public string Message { get; set; }
}
UFile的一部分API,如上传下载等返回类型为FileResponse
,同时操作还根据请求结果返回Header,继承于ResponseHeader
/// <summary>
/// 对象存储UFile操作的返回对象
/// </summary>
public partial class FileResponse : ResponseHeader
{
/// <summary>
/// 执行结果代码
/// <para>
/// 执行成功与否,0 表示成功,其他值则为错误代码.
/// </para>
/// </summary>
public int RetCode { get; set; }
/// <summary>
/// 消息.
/// </summary>
public string ErrMsg { get; set; }
}
/// <summary>
/// 对象存储UFile操作后返回的HTTP Header
/// </summary>
public partial class ResponseHeader
{
/// <summary>
/// 文件哈希值.
/// </summary>
public string ETag { get; set; }
/// <summary>
/// 会话Id.
/// </summary>
public string XSessionId { get; set; }
/// <summary>
/// 文件长度.
/// </summary>
public int ContentLength { get; set; }
/// <summary>
/// 文件类型.
/// </summary>
public string ContentType { get; set; }
/// <summary>
/// 文件的范围.
/// </summary>
public string ContentRange { get; set; }
}
SDK的HTTP请求使用了RestSharp,需要对HTTP请求进行设置,比如代理、过期时间等,可对UCloud的Client属性进行设置。更多RestSharp使用方法请参见其官方文档。
var uhost=new UHost();
//设置HTTP代理
uhost.Client.Proxy=new WebProxy("http://proxy.com");
1.根据官方API文档添加了详细的注释,配合Visual Studio使用更加方便。 2.添加了大部分枚举值以便于使用。
注意: 由于C#命名规则,枚举值不允许使用“-”,部分枚举值(比如Region)需要使用
string.ToHyphen()
来取值。在枚举的注释中有注明需要用此方法的。 由于C#命名规则,枚举值不允许使用数字,部分枚举值(比如Priority)需要使用(int)Priority.High
来取值。在枚举的注释中有注明需要用此方法的。
3.需要BASE64编码的请使用string.ToBase64()
扩展方法。
4.NList类型转换后会自动在属性后添加.n,使用时无需再添加。使用方法如下:
以更新防火墙规则UpdateSecurityGroup为例
NList rules=new NList();
//添加规则
rules.Add("TCP|3306|0.0.0.0/0|DROP|50");
//另一种添加规则的方式
//该Rule是一个对象,SDK内置了此对象,方便使用
var rule = new SecurityGroupRule()
{
Proto = "UDP",
Dst_port = "53",
Src_ip = "0.0.0.0/0",
Action = "ACCEPT",
//内置Priority枚举
Priority = (int) Priority.Medium
};
//ToString() 直接生成“Proto|Dst_port|Src_ip|Action|Priority”
rules.Add(rule.ToString());
var entity = new UpdateSecurityGroupRequest("cn-north-03", "6583",rules);
- 使用测试方法请先在UCloud.Test中Config.cs配置相关参数。
- UDisk的一些测试方法返回错误结果,因为没有相关权限,已在测试中注明。除此之外,其它方法都经过测试并返回了正确结果。
- UCloud现在未提供沙箱环境,而一些测试需要进行支付,所以测试并非纯粹的单元测试,有的测试需要依赖其它测试的结果。具体请查看每个测试所需参数上方的注释。
UCloud: http://www.ucloud.cn/ UCloud API: http://docs.ucloud.cn/api/index.html