Range download allows you to download a specific byte range from an object instead of the entire object.
Usage notes
-
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and Endpoints.
-
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Initialization.
-
To perform range download, you must have the
oss:GetObjectpermission. For more information, see Grant a custom policy.
Sample code
The following sample code shows how to download a specific byte range from an object.
using Aliyun.OSS;
using Aliyun.OSS.Common;
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt.
var objectName = "exampledir/exampleobject.txt";
// Download the object to D:\\localpath. After the object is downloaded, the local file is named examplefile.txt. If a file with the same name already exists, the downloaded object overwrites the file. Otherwise, the downloaded object is saved in the path.
// If you do not specify a path for the downloaded object, the downloaded object is saved to the path of the project to which the sample program belongs.
var downloadFilename = "D:\\localpath\\examplefile.txt";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters based on your requirements.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
var getObjectRequest = new GetObjectRequest(bucketName, objectName);
// Set the range to byte 20 to byte 100.
getObjectRequest.SetRange(20, 100);
// Start range download. You can use setRange in getObjectRequest to perform range download and resumable download.
var obj = client.GetObject(getObjectRequest);
// Download data and write the data to the file.
using (var requestStream = obj.Content)
{
byte[] buf = new byte[1024];
var fs = File.Open(downloadFilename, FileMode.OpenOrCreate);
var len = 0;
while ((len = requestStream.Read(buf, 0, 1024)) != 0)
{
fs.Write(buf, 0, len);
}
fs.Close();
}
Console.WriteLine("Get object succeeded");
}
catch (Exception ex)
{
Console.WriteLine("Get object failed. {0}", ex.Message);
}
The following table describes the parameters of GetObjectRequest.
|
Parameter |
Description |
|
Range |
The byte range to download. |
|
ModifiedSinceConstraint |
If the specified time is earlier than the actual modification time of the object, the object is returned. Otherwise, HTTP status code 304 Not Modified is returned. |
|
UnmodifiedSinceConstraint |
If the specified time is equal to or later than the actual modification time of the object, the object is returned. Otherwise, HTTP status code 412 Precondition Failed is returned. |
|
MatchingETagConstraints |
If the specified ETag matches the ETag of the object, the object is returned. Otherwise, HTTP status code 412 Precondition Failed is returned. |
|
NonmatchingEtagConstraints |
If the specified ETag does not match the ETag of the object, the object is returned. Otherwise, HTTP status code 304 Not Modified is returned. |
|
ResponseHeaderOverrides |
Custom response headers to include in the download response. |