- 产品文档
- > 存储与 CDN
- > 对象存储
- > AWS S3 兼容
- > AWS S3 SDK
- > S3 NET SDK
- > 文件下载
文件下载
S3 C# SDK提供了丰富的文件下载接口,用户可以通过以下方式从NOS获取文件:
- 下载文件到内存
- 下载到本地文件
- 分段下载
- 条件下载
下载文件到内存
以下源代码实现下载文件到内存中:
public void GetObject(string bucket,string key){
GetObjectRequest request = new GetObjectRequest
{
BucketName = bucket,
Key = key
};
try
{
using (GetObjectResponse response = s3Client.GetObject(request))
{
using (StreamReader reader = new StreamReader(response.ResponseStream))
{
string contents = reader.ReadToEnd();
Console.WriteLine(" Object - " + response.Key);
Console.WriteLine(" Contents - " + contents);
}
}
} catch(AmazonS3Exception ex){
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3};",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId);
} catch(Exception ex){
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
下载文件到本地文件
以下源代码实现将NOS文件下载到本地文件:
public void GetObjectToFile(string bucket, string key, string filePath) { GetObjectRequest request = new GetObjectRequest { BucketName = bucket, Key = key,
};
try
{
using (GetObjectResponse response = s3Client.GetObject(request))
{
response.WriteResponseStreamToFile(filePath);
}
}
catch (AmazonS3Exception ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3};",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
指定范围下载
如果存储在NOS中的文件较大,并且您只需要其中的一部分内容,您可以使用范围下载,下载指定范围的数据,如果指定的下载范围为"0-100",则返回结果为第0字节到第100字节的数据,返回的数据包含第100字节, 即range=0-100,如果指定的范围无效则下载整个文件,以下源代码获取range=0-100字节的内容:
public void GetObjectToFileByRange(string bucket, string key, string filePath)
{
GetObjectRequest request = new GetObjectRequest
{
BucketName = bucket,
Key = key,
ByteRange = new ByteRange(0,3)//根据业务需求,自行设置
};
try
{
using (GetObjectResponse response = s3Client.GetObject(request))
{
response.WriteResponseStreamToFile(filePath);
}
}
catch (AmazonS3Exception ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3};",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}
Attention
- 下载内容也可以存储到文件中
- 注意下载的区间为闭区间
条件下载
下载文件时,可以指定限定条件,满足限定条件时下载,不满足时报错,不下载文件。可以使用的限定条件如下:
参数 | 说明 | GetObjectRequest对应值 |
---|---|---|
If-Modified-Since | 如果指定的时间早于实际修改时间,则正常传送。否则返回错误 | GetObjectRequest.IfModifiedSince |
代码示例如下:
public void GetObjectToFileByModifiedSince(string bucket, string key, string filePath,DateTime dateTime)
{
GetObjectRequest request = new GetObjectRequest
{
BucketName = bucket,
Key = key,
ModifiedSinceDate = dateTime
};
try
{
using (GetObjectResponse response = s3Client.GetObject(request))
{
response.WriteResponseStreamToFile(filePath);
}
}
catch (AmazonS3Exception ex)
{
Console.WriteLine("Failed with HTTPStatus: {0}; \nErrorCode: {1}; \nErrorMessage: {2}; \nRequestID:{3};",
ex.StatusCode, ex.ErrorCode, ex.Message, ex.RequestId);
}
catch (Exception ex)
{
Console.WriteLine("Failed with error info: {0}", ex.Message);
}
}