7. 使用https

NOS C# SDK支持使用https的方式调用相关的接口,以保证安全性

7.1. 使用https

NOS C# SDK默认使用http协议,若需使用https协议,只需在初始化ClientConfiguration实例时修改传输协议并配置客户端证书路径,代码如下:

using Netease.Cloud.NOS;
using Netease.Cloud.NOS.Util;

ClientConfiguration conf = new ClientConfiguration(){
    //设置为https协议
    Protocol = Protocol.Https,
    //设置证书路径
    CaPath = "您的证书路径"
};

var nosClient = new NosClient(endponit, accessKeyId, accessKeySecret, conf);

Attention

  1. NOS C# SDK默认使用http协议,也可以在初始化ClientConfiguration实例时通过设置Protocol = Protocol.Http设置http协议。

7.2. 实例

以下代码实现以https的方式上传本地文件,具体实现如下:

using Netease.Cloud.NOS;
using Netease.Cloud.NOS.Util;

ClientConfiguration conf = new ClientConfiguration(){
    //设置为https协议
    Protocol = Protocol.Https,
    //设置证书路径
    CaPath = "您的证书路径"
};

var nosClient = new NosClient(endponit, accessKeyId, accessKeySecret, conf);

// 上传文件
public void PutObject(string bucket, string key, string fileToUpload)
{
    try
    {
        nosClient.PutObject(bucket, key, fileToUpload);
        Console.WriteLine("Put object:{0} succeeded", key);
    }
    catch (NosException ex)
    {
        Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3}; \nResource:{4}",
            ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId, ex.Resource);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed with error info: {0}", ex.Message);
    }
}