If you need only part of the data in an object, you can use range download to download data within a specified range.

Specify a valid range

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

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

int main(void)
{
     /* Initialize the OSS account information. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string ObjectName = "yourObectName ";

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

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret,  conf );

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

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

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

Specify an invalid range

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 from byte 0 to byte 999, the range does not take effect. Then, OSS returns the HTTP status code 200 and the data of the entire object. The following list provides examples of invalid requests and the returned results:

  • If you specify Range: bytes to 500-2000, the value at the end of the range is invalid. Then, OSS returns the HTTP status code 200 and the data of the entire object.
  • If you specify Range: bytes to 1000-2000, the value at the start of the range is invalid. Then, OSS returns the HTTP status code 200 and the data of the entire object.

Compatible download

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. Example: Use range download to download an object whose size is 1,000 bytes.

  • If you specify Range: bytes to 500-2000, the value at the end of the range is invalid. Then, OSS returns the HTTP status code 206 and the data that is within the range from byte 500 to byte 999.
  • If you specify Range: bytes to 1000-2000, the value at the start of the range is invalid. Then, OSS returns the HTTP status code 416 and the error code InvalidRange.

The following code provides an example on how to perform compatible download:

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

int main(void)
{
     /* Initialize the OSS account information. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
     /* Specify an object whose size is 1,000 bytes. */
    std::string ObjectName = "yourObectName ";

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

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret,  conf );

    /* Obtain the object. */
    GetObjectRequest request(BucketName,  ObjectName);
    /* Set the download range. */
    /* Specify the compatible behavior. */
    /* When the value at the end of the range is invalid, OSS returns the data that is within the range from byte 500 to byte 999. The HTTP status code 206 is also returned. */
    request.setRange(500, 2000, true);
    auto outcome = client.GetObject(request);

    /* When the value at the start of the range is invalid, exceptions are thrown. OSS returns the HTTP status code 416 and the InvalidRange error code. */
    request.setRange(1000, 2000, true);
    outcome = client.GetObject(request);

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

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