.NET SDK 手册
文件下载
NOS C# SDK 提供了丰富的文件下载接口,用户可以通过以下方式从 NOS 获取文件:
- 下载文件到内存
- 下载到本地文件
- 分段下载
- 条件下载
下载文件到内存
以下源代码实现下载文件到内存中:
// 初始化 NosClient
var nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
/// <summary>
/// 下载文件到内存
/// </summary>
/// <param name="bucket">桶名</param>
/// <param name="bucket">对象名</param>
public void GetObject(string bucket, string key)
{
try
{
var result = nosClient.GetObject(bucket, key);
Console.WriteLine(result.Content);
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 文件下载到本地文件:
// 初始化 NosClient
var nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
/// <summary>
/// 下载文件到本地
/// </summary>
/// <param name="bucket">桶名</param>
/// <param name="bucket">对象名</param>
/// <param name="bucket">下载到的本地文件</param>
public void GetObject(string bucket, string key, string fileToDownload)
{
try
{
nosClient.GetObject(bucket, key, fileToDownload);
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);
}
}
Note
参数 fileToDownload 中需要指定具体的文件名。
指定范围下载
如果存储在 NOS 中的文件较大,并且您只需要其中的一部分内容,您可以使用范围下载,下载指定范围的数据,如果指定的下载范围为”0-100”,则返回结果为第 0 字节到第 100 字节的数据,返回的数据包含第 100 字节,即 range=0-100,如果指定的范围无效则下载整个文件,以下源代码获取 range=0-100 字节的内容:
// 初始化 NosClient
var nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
/// <summary>
/// 指定范围下载
/// </summary>
/// <param name="bucket">桶名</param>
/// <param name="bucket">对象名</param>
/// <param name="bucket">下载到的本地文件</param>
public void GetObject(string bucketName, string key, string fileToDownload)
{
try
{
var request = new GetObjectRequest(bucketName, key);
request.Range = "bytes=0-100";
var result = nosClient.GetObject(request,fileToDownload);
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);
}
}
Attention
下载内容也可以存储到文件中,下载的区间为闭区间
条件下载
下载文件时,可以指定限定条件,满足限定条件时下载,不满足时报错,不下载文件。可以使用的限定条件如下:
参数 | 说明 | GetObjectRequest 对应值 |
---|---|---|
If-Modified-Since | 如果指定的时间早于实际修改时间,则正常传送。否则返回错误 | GetObjectRequest.IfModifiedSince |
// 初始化 NosClient
var nosClient = new NosClient(endpoint, accessKeyId, accessKeySecret);
/// <summary>
/// 指定条件下载
/// </summary>
/// <param name="bucket">桶名</param>
/// <param name="bucket">对象名</param>
/// <param name="bucket">下载到的本地文件</param>
public void GetObject(string bucketName, string key, string fileToDownload)
{
try
{
var request = new GetObjectRequest(bucketName, key);
request.IfModifiedSince = new DateTime(2016, 11, 13, 14, 47, 53);;
var result = nosClient.GetObject(request,fileToDownload);
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);
}
}
Attention
下载内容也可以存储到文件中