3. 快速入门

使用NOS C# SDK前,您可以先参照 API 手册 熟悉NOS的基本概念,如Bucket、Object、EndPoint、AccessKeyId和AccessKeySecret等。 本节您将看到如何快速的使用NOS C# SDK,完成常用的操作,上传文件、下载文件等。

3.1. 常用类

常用类 备注
NOS\NosClient NOS客户端类,用户通过NosClient调用服务
NOS\Exception\NosException NOS异常,在调用过程中去捕获这个异常,查看失败原因

3.2. 基本操作

3.2.1. 上传文件

对象(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

  1. 对象命名规则请参见 API 手册 对象
  2. 更多的上传文件信息,请参见 NOS-C#-SDK 上传文件

3.2.2. 下载文件

上传对象成功之后,您可以读取它的内容,以下代码可以实现文件的下载:

// 初始化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);
    }
}

更多的下载文件信息,请参见 NOS C# SDK 下载文件

3.2.3. 列举文件

当上传文件成功之后,可以查看桶中包含的文件列表,以下代码展示如何列举桶内的文件:

// 初始化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

  1. 上面的代码默认列举100个object

更多的管理文件信息,请参见 NOS C# SDK 文件管理

3.2.4. 删除文件

文件上传成功后,可以指定删除桶中的文件,以下代码实现桶中文件的删除:

// 初始化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);
    }
}

3.2.5. 返回结果处理

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);
}