.NET SDK 手册
快速入门
使用 NOS C# SDK 前,您可以先参照 API 手册 熟悉 NOS 的基本概念,如 Bucket、Object、EndPoint、AccessKeyId 和 AccessKeySecret 等。本节您将看到如何快速的使用 NOS C# SDK,完成常用的操作,上传文件、下载文件等。
常用类
常用类 | 备注 |
---|---|
NOS\NosClient | NOS 客户端类,用户通过 NosClient 调用服务 |
NOS\Exception\NosException | NOS 异常,在调用过程中去捕获这个异常,查看失败原因 |
基本操作
上传文件
对象(Object)是 NOS 中最基本的数据单元,您可以把它简单的理解为文件,以下代码可以实现简单的对象上传 :
// 初始化 NosClient
var nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
/// <summary>
/// 上传文件
/// </summary>
/// <param name="bucket">桶名</param>
/// <param name="key">对象名</param>
/// <param name="fileToUpload">上传的文件</param>
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);
}
}
Attention
下载文件
上传对象成功之后,您可以读取它的内容,以下代码可以实现文件的下载 :
// 初始化 NosClient
var nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
/// <summary>
/// 上传文件
/// </summary>
/// <param name="bucket">桶名</param>
/// <param name="key">对象名</param>
/// <param name="dirToDownload">下载文件存放的目录</param>
public void GetObject(string bucket, string key, string dirToDownload)
{
try
{
nosClient.GetObject(bucket, key, dirToDownload + "/sample.data");
Console.WriteLine("Get object succeeded");
}
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);
}
}
列举文件
当上传文件成功之后,可以查看桶中包含的文件列表,以下代码展示如何列举桶内的文件 :
// 初始化 NosClient
var nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
/// <summary>
/// 列举桶内文件
/// </summary>
/// <param name="bucket">桶名</param>
public void ListObjects(string bucket)
{
try
{
var keys = new List<string>();
var listObjectsRequest = new ListObjectsRequest(bucket);
ObjectListing result = nosClient.ListObjects(listObjectsRequest);
foreach (var summary in result.ObjectSummarise)
{
Console.WriteLine(summary.Key);
keys.Add(summary.Key);
}
Console.WriteLine("List objects of bucket:{0} succeeded ", bucket);
}
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);
}
}
Note
上面的代码默认列举 100 个 object,且会列出目录及其目录中的文件。
删除文件
文件上传成功后,可以指定删除桶中的文件,以下代码实现桶中文件的删除 :
// 初始化 NosClient
var nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
/// <summary>
/// 删除单个文件
/// </summary>
/// <param name="bucket">桶名</param>
/// <param name="key">对象名</param>
public void DeleteObject(string bucket, string key)
{
try
{
nosClient.DeleteObject(bucket, key);
Console.WriteLine("Delete object succeeded");
}
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);
}
}
返回结果处理
NosClient 中的接口对应返回数据分为两类 :
- Put:Delete 类接口返回 null,如果没有 NosException,即可认为操作成功
- Get:List 类接口返回对应的数据,如果没有 NosException,即可认为操作成功
例如 :
try
{
ObjectListing result = nosClient.ListObjects(listObjectsRequest);
foreach (var summary in result.ObjectSummarise)
{
Console.WriteLine(summary.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);
}