You can use range download to download parts of the content of an object.

Specify a valid range to download data

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

# -*- coding: utf-8 -*-
import oss2
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# 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. 
# Set yourBucketName to the name of the bucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName')

# If an object is 1,000 bytes in size, the valid range is from byte 0 to byte 999. 
# Obtain data that is within the range from byte 0 to byte 999, which includes a total of 1,000 bytes. If the specified 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))

An invalid range is specified

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 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 set Range: bytes to 500-2000, the value at the end of the range is invalid. Then, 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. Then, OSS returns HTTP status code 200 and the data of the entire object.

Specify compatible behaviors to download data by range

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

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

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

# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# 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. 
# Set yourBucketName to the name of the bucket. 
bucket = oss2.Bucket(auth, 'https://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. 
# 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. 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:
    # If the value at the start of the range is invalid, exceptions are thrown. The HTTP status code 416 is returned and the 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)

References

  • For more information about the complete sample code for range download, visit GitHub.
  • For more information about the API operation that is called to perform range download, see GetObject.