All Products
Search
Document Center

Object Storage Service:Range download (C++ SDK)

Last Updated:Nov 29, 2025

If you need only a portion of a file's data, you can use a range download to retrieve a specific range of data.

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.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

  • To perform range download, you must have the oss:GetObject permission. For more information, see Attach a custom policy to a RAM user.

Specify a normal download range

The following sample code provides an example on how to specify a valid range to download data:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize OSS account information. */
            
    /* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com for a bucket in the China (Hangzhou) region. */
    std::string Endpoint = "yourEndpoint";
    /* Set yourRegion to the region where the bucket is located. For example, set the region to cn-hangzhou for a bucket in the China (Hangzhou) region. */
    std::string Region = "yourRegion";
    /* Specify the bucket name. For example, examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. */
    std::string ObjectName = "exampledir/exampleobject.txt";

     /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Get the file. */
    GetObjectRequest request(BucketName, ObjectName);
    /* Set the download range. */
    request.setRange(0, 1);
    auto outcome = client.GetObject(request);

    if (!outcome.isSuccess ()) {    
        /* Handle the exception. */
        std::cout << "getObject fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
         return -1;  
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

Specify an invalid range to download data

For an object whose size is 1,000 bytes, the valid range is from byte 0 to byte 999. If the specified range is not within the range of byte 0 to byte 999, the range does not take effect. In this case, OSS returns HTTP status code 200 and the data of the entire object. The following examples show invalid requests and the returned results:

  • If you set Range: bytes to 500-2000, the value at the end of the range is invalid. In this case, OSS returns HTTP status code 200 and the data of the entire object.

  • If you set Range: bytes to 1000-2000, the value at the start of the range is invalid. In this case, OSS returns HTTP status code 200 and the data of the entire object.

Standard behavior for range downloads

If you add x-oss-range-behavior:standard to the request header, the download behavior is modified when the specified range is not within the valid range. For an object whose size is 1,000 bytes:

  • If you set Range: bytes to 500-2000, the value at the end of the range is invalid. In this case, OSS returns HTTP status code 206 and the data that is within the range of byte 500 to byte 999.

  • If you set Range: bytes to 1000-2000, the value at the start of the range is invalid. In this case, OSS returns HTTP status code 416 and the InvalidRange error code.

The following sample code provides an example on how to specify standard behaviors to download data by range:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize OSS account information. */
            
    /* Set yourEndpoint to the endpoint of the region where the bucket is located. For example, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com for a bucket in the China (Hangzhou) region. */
    std::string Endpoint = "yourEndpoint";
    /* Set yourRegion to the region where the bucket is located. For example, set the region to cn-hangzhou for a bucket in the China (Hangzhou) region. */
    std::string Region = "yourRegion";
    /* Specify the bucket name. For example, examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify a file that is 1000 bytes in size. */
    /* Specify the full path of the object. The full path cannot contain the bucket name. For example, exampledir/exampleobject.txt. */
    std::string ObjectName = "yourObjectName";

     /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    conf.signatureVersion = SignatureVersionType::V4;
    /* Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);

    /* Get the file. */
    GetObjectRequest request(BucketName, ObjectName);
    /* Set the download range. */    
    /* The end of the range is outside the valid interval. The content from byte 500 to 999 is returned, and the HTTP status code is 206. */
    request.setRange(500, 2000, true);
    auto outcome = client.GetObject(request);

    /* The start of the range is outside the valid interval. An exception is thrown. The HTTP status code 416 is returned with the InvalidRange error code. */
    request.setRange(1000, 2000, true);
    outcome = client.GetObject(request);

    if (!outcome.isSuccess ()) {    
        /* Handle the exception. */
        std::cout << "getObject fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
         return -1;  
    }

    /* Release network resources. */
    ShutdownSdk();
    return 0;
}

References

For more information about the API operation for range downloads, see GetObject.