大容量のオブジェクトをダウンローズする場合、またはオブジェクト全体を一度にダウンロードするのに時間がかかる場合は、ストリーミングダウンロードを使用します。 ストリーミングダウンロードでは、オブジェクト全体をダウンロードし終わるまで、オブジェクトのパーツをその都度ダウンロードし続けます。
ストリーミングダウンロードの完全なコードについては、『GitHub』をご参照ください。
次のコードを実行して、指定した OSS オブジェクトをストリームにダウンロードします。
using Aliyun.OSS;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
var objectName = "<yourObjectName>";
var downloadFilename = "<yourDownloadFilename>";
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
// Download an object to a stream. OssObject includes object information, such as the bucket that contains the object, object name, metadata, and an input stream.
var obj = client.GetObject(bucketName, objectName);
using (var requestStream = obj.Content)
{
byte[] buf = new byte[1024];
var fs = File.Open(downloadFilename, FileMode.OpenOrCreate);
var len = 0;
// Use the input stream to read the object content into a file or the memory.
while ((len = requestStream.Read(buf, 0, 1024)) ! = 0)
{
fs.Write(buf, 0, len);
}
rs.close();
}
Console.WriteLine("Get object succeeded");
}
} catch (Exception e) {
{
Console.WriteLine("Get object failed. {0}", ex.Message);
}