If you only need a portion of the data in a file, you can use a range download to retrieve data within a specified range.
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.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
To perform range download, you must have the
oss:GetObjectpermission. For more information, see Attach a custom policy to a RAM user.
Specify a normal download 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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the endpoint is located, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Replace yourBucketName with the bucket name.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# For a 1000 byte file, the normal download range is 0 to 999.
# Get data within the range of 0 to 999 bytes, including byte 0 and byte 999, for a total of 1000 bytes. If the specified range is invalid (for example, the start or end position is a negative value or is greater than the file size), the entire file 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.
Standard behavior for range 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. 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 this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the endpoint is located, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Replace yourBucketName with the bucket name.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Create an object that is 1000 bytes in size.
object_name = 'range_test.txt'
content = 'a' * 1000
bucket.put_object(object_name, content)
headers = {'x-oss-range-behavior': 'standard'}
# If the end of the range is outside the valid interval, the content from byte 500 to byte 999 is returned, and the HTTP status code is 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 content_length:', object_stream.content_length)
try:
# If the start of the range is outside the valid interval, an exception is thrown. The HTTP status code is 416, 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 the complete sample code for a range download, see GitHub examples.
For more information about the API operation for range download, see GetObject.