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:

# -*- coding: utf-8 -*-
import oss2

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# For an object whose size is 1,000 bytes, the valid range is from byte 0 to byte 999.
# Query the 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, for example, the specified range includes a negative number, or the specified value is larger than the object size, the entire object is downloaded.
object_stream = bucket.get_object('<yourObjectName>', byte_range=(0, 999))

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:

# -*- coding: utf-8 -*-
import oss2

# Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>')
# The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>')

# 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'}

# 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.
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:
    # When the value at the start of the range is invalid, exceptions are thrown. The returned HTTP status code is 416 and the returned error code is 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)