All Products
Search
Document Center

Object Storage Service:Range download (PHP SDK V2)

Last Updated:Mar 20, 2026

Download a specific byte range of an object using GetObjectRequest with the rangeHeader parameter.

Prerequisites

Before you begin, ensure that you have:

The sample code in this topic uses the public endpoint for the China (Hangzhou) (cn-hangzhou) region. If you access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For endpoint mappings, see OSS regions and endpoints.

How out-of-range requests behave

OSS handles an invalid Range header differently depending on whether you include the x-oss-range-behavior:standard request header. Assume the object is 1,000 bytes (valid range: bytes=0-999):

ScenarioWithout x-oss-range-behavior:standardWith x-oss-range-behavior:standard
End exceeds object size (bytes=500-2000)Returns the entire object, HTTP 200Returns bytes 500-999, HTTP 206
Start exceeds object size (bytes=1000-2000)Returns the entire object, HTTP 200Returns HTTP 416, error code InvalidRange
Important

If you specify an invalid range without the x-oss-range-behavior:standard header, the Range header is ignored and the entire object is returned with HTTP 200.

Sample code

The following example uses GetObjectRequest with rangeBehavior set to standard and rangeHeader set to bytes=0-10. Credentials are loaded from environment variables.

<?php

// Import the autoloader file to ensure that dependency libraries are correctly loaded.
require_once __DIR__ . '/../vendor/autoload.php';

use AlibabaCloud\Oss\V2 as Oss;

// Define the description of command line arguments.
$optsdesc = [
    "region"   => ['help' => 'The region in which the bucket is located.', 'required' => True],
    "endpoint" => ['help' => 'The domain names that other services can use to access OSS.', 'required' => False],
    "bucket"   => ['help' => 'The name of the bucket', 'required' => True],
    "key"      => ['help' => 'The name of the object', 'required' => True],
];

// Convert the argument descriptions to the long option format required by getopt.
// A colon (:) after each argument indicates that the argument requires a value.
$longopts = \array_map(function ($key) {
    return "$key:";
}, array_keys($optsdesc));

// Parse the command line arguments.
$options = getopt("", $longopts);

// Verify that all required arguments are specified.
foreach ($optsdesc as $key => $value) {
    if ($value['required'] === True && empty($options[$key])) {
        $help = $value['help'];
        echo "Error: the following arguments are required: --$key, $help" . PHP_EOL;
        exit(1);
    }
}

// Extract values from the parsed arguments.
$region = $options["region"];
$bucket = $options["bucket"];
$key    = $options["key"];

// Load credentials from environment variables.
$credentialsProvider = new Oss\Credentials\EnvironmentVariableCredentialsProvider();

// Use the default SDK configuration.
$cfg = Oss\Config::loadDefault();
$cfg->setCredentialsProvider($credentialsProvider);
$cfg->setRegion($region);
if (isset($options["endpoint"])) {
    $cfg->setEndpoint($options["endpoint"]);
}

// Create an OSS client instance.
$client = new Oss\Client($cfg);

// Set RangeBehavior to standard and request the first 11 bytes (bytes 0 through 10).
$request = new Oss\Models\GetObjectRequest(
    bucket:        $bucket,
    key:           $key,
    rangeBehavior: 'standard',
    rangeHeader:   'bytes=0-10'
);

// Execute the GetObject operation.
$result = $client->getObject($request);

// Output the HTTP status code, request ID, and object content.
printf(
    'status code:' . $result->statusCode . PHP_EOL .
    'request id:' . $result->requestId . PHP_EOL .
    'object content:' . $result->body->getContents() . PHP_EOL
);

Replace the following placeholders when running the script:

ParameterRequiredDescriptionExample
--regionYesThe region where the bucket is locatedcn-hangzhou
--bucketYesThe bucket nameexamplebucket
--keyYesThe object nameexampledir/exampleobject.txt
--endpointNoA custom endpointoss-cn-hangzhou.aliyuncs.com