5. 文件下载¶
NOS C# SDK提供了丰富的文件下载接口,用户可以通过以下方式从NOS获取文件:
- 下载文件到内存
- 下载到本地文件
- 分段下载
- 条件下载
5.1. 下载文件到内存¶
以下源代码实现下载文件到内存中:
// 初始化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);
}
}
5.2. 下载文件到本地文件¶
以下源代码实现将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);
}
}
5.3. 指定范围下载¶
如果存储在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
- 下载内容也可以存储到文件中
- 注意下载的区间为闭区间
5.4. 条件下载¶
下载文件时,可以指定限定条件,满足限定条件时下载,不满足时报错,不下载文件。可以使用的限定条件如下:
参数 | 说明 | 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.If-Modified-Since = "Fri, 13 Nov 2016 14:47:53 GMT";
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
- 下载内容也可以存储到文件中