All Products
Search
Document Center

Object Storage Service:Range download using OSS SDK for Python 1.0

Last Updated:Jun 08, 2026

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.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials using OSS SDK for Python 1.0.

  • 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:GetObject permission. For more information, see Grant a custom policy.

Download a valid byte range

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

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# If an object is 1,000 bytes in size, the valid range is from byte 0 to byte 999. 
# Query data that is within the range from byte 0 to byte 999, which includes a total of 1,000 bytes. If the specified value range is invalid, the entire object is downloaded. For example, if the start or end of the specified range is a negative number or the specified value is greater than the object size, all content of the object is downloaded. 
object_stream = bucket.get_object('<yourObjectName>', byte_range=(0, 999))

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.

Use standard range behavior

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:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# 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. 
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)


# Create an object whose size is 1,000 bytes. 
object_name = 'rangeTest.txt'
content = 'a' * 1000
bucket.put_object(object_name, content)

headers = {'x-oss-range-behavior': 'standard'}

# If 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 and the HTTP status code 206. 
object_stream = bucket.get_object(object_name, byte_range=(500, 2000), headers=headers)
print('standard get 500~2000 http status code:', object_stream.status)
print('standard get 500~2000 contnet_length:', object_stream.content_length)

try:
    # If the value at the start of the range is invalid, an exception is thrown. Then, OSS returns the HTTP status code 416 and the error code InvalidRange. 
    object_stream = bucket.get_object(object_name, byte_range=(1000, 2000), headers=headers)
except oss2.exceptions.ServerError as e:
    print('standard get 1000~2000 http status code:', e.status)
    print('standard get 1000~2000 error code:', e.code)  

References

  • Complete range download sample code on GitHub.

  • For more information about the API operation that you can call to perform range download, see GetObject.