All Products
Search
Document Center

:How to Obtain OSS Resources by Segmenting HTTP Range Requests

Last Updated:Jul 11, 2023

Note

Disclaimer: This topic may contain information about third-party products. The information is for reference only. Alibaba Cloud does not make a guarantee in any form of the performance and reliability of the third-party products, and potential impacts of operations on these products.

Overview

This topic describes how to obtain OSS resources by segmenting HTTP Range requests.

Message

When you upload or download a large object (larger than 100 MB) from OSS, if the object is affected by the network environment during the transfer, the transfer fails. During the upload process, you can call the MultipartUpload operation to perform multipart upload. When you download a resource, you can use an HTTP Range request to obtain some content of a large file. Example:

Get /ObjectName HTTP/1.1
Host:xxxx.oss-cn-hangzhou.aliyuncs.com
Date:Tue, 17 Nov 2015 17:27:45 GMT
Authorization:SignatureValue
Range:bytes=[$ByteRange]



Note

Note :[$ByteRange] indicates the range of requested resources. Unit: bytes. Example:

  • Range: bytes=0-499 specifies the first 500 bytes.
  • Range: bytes=500-999 specifies the second 500 bytes.
  • Range: bytes=-500 specifies the last 500 bytes.
  • Range: bytes=500- specifies data from the 500th byte to the end of the object.
  • Range: bytes=0- indicates the first byte to the last byte, which is the complete file content.
  • You can specify only one range in a GetObject request. If you specify multiple ranges, OSS returns only the data in the first range. For example, Range:bytes=0-499,500-999 ,OSS returns only the data in the range of 0 to 499 bytes.
  • [$ByteRange] The valid interval is in the range of 0 to content-length - 1.

If the HTTP Range request is valid, the response returns 206 and contains Content-Range in the response header. If the HTTP Range request is invalid or the specified range is not within the valid range, the range does not take effect. The response returns a value of 200 and the entire object content is transmitted. The following example shows the invalid HTTP Range request and the error description.

Note

Note: Assume that the resource size of an object is 1000 bytes and the range range is 0 to 999. To avoid the specified Range being out of range, you can make a HeadObject request to obtain the object size before the Range is read.

  • Range: byte=0-499: The format is invalid. The byte value must be bytes.
  • Range: bytes=0-1000: The 1000 of the last byte exceeds the valid interval.
  • Range: bytes=1000-2000: The specified range exceeds the valid range.
  • Range: bytes=1000-: The first byte exceeds the valid interval.
  • Range: bytes=-2000: The specified range exceeds the valid range.

You can run the following command to test the validity of the Range parameter:

curl -r 0-100 http://xxxx.oss-cn-hangzhou.aliyuncs.com/xx.zip -o /tmp/xx1.zip -v

Compatible behavior

When HTTP Range is used, the request header x-oss-range-behavior:standard is added to change the behavior of OSS when the specified range is not within the valid range. Examples of behavior changes are as follows:

Note

Note: Assume that the resource size of the object is 1000 bytes and the range range is 0-999. For example, OSS returns an InvalidRange error code because an invalid range is selected when you use an HTTP Range request to obtain some content of a large file. For more information, see OSS returns a 416 error to resolve the error. The detailed error information is as follows:

The requested range cannot be satisfied

  • Range: bytes=500-2000: If the last byte exceeds the valid range, 500-999 bytes are returned.
  • Range: bytes=1000-2000: The error 416 (InvalidRange) is returned if the first byte exceeds the valid interval.
  • Range: bytes=1000-: The error 416 (InvalidRange) is returned if the first byte exceeds the valid interval.
  • Range: bytes=-2000: If the specified range exceeds the valid range, 0-999 bytes are returned, which is the complete file content.

Examples

This topic provides the following sample HTTP Range request.

Note

Note: Assume that the resource size of the object is 1000 bytes and the range range is 0-999.

Normal requests

  • The content in the 0-499 byte range of the requested Object resource.
    GET /ObjectName
    Range: bytes=0-499
    Host: bucket.oss-cn-hangzhou.aliyuncs.com
    Date: Fri, 18 Oct 2019 02:51:30 GMT
    Authorization: Sigature
    
    206 (Partial Content)
    content-length: 500
    content-range: bytes 0-499/1000
    connection: keep-alive
    etag: "CACF99600561A31D494569C979E6FB81"
    x-oss-request-id: 5DA928B227D52731327DE078
    date: Fri, 18 Oct 2019 02:51:30 GMT
    [500 bytes of object data]
  • The content from the 500th byte of the requested Object resource to the end of the file.
    GET /ObjectName
    Range: bytes=500-
    Host: bucket.oss-cn-hangzhou.aliyuncs.com
    Date: Fri, 18 Oct 2019 03:24:39 GMT
    Authorization: Signature
    
    206 (Partial Content)
    content-length: 500
    content-range: bytes 500-999/1000
    etag: "CACF99600561A31D494569C979E6FB81"
    x-oss-request-id: 5DA9307750EBE33332E3720A
    date: Fri, 18 Oct 2019 03:24:39 GMT
    [500 bytes of object data]
  • The content of the last 500 byte of the requested Object resource.
    GET /ObjectName
    Range: bytes=-500
    Host: bucket.oss-cn-hangzhou.aliyuncs.com
    Date: Fri, 18 Oct 2019 03:23:22 GMT
    Authorization: Signature
    
    206 (Partial Content)
    content-length: 500
    content-range: bytes 500-999/1000
    etag: "CACF99600561A31D494569C979E6FB81"
    x-oss-request-id: 5DA9302A6646AC37397F7039
    date: Fri, 18 Oct 2019 03:23:22 GMT
    [500 bytes of object data]

Example of an out-of-scope request

  • If the 1000 of the last byte exceeds the valid interval, the Range does not take effect. The response returns a value of 200 and transmits the entire object content.
    GET /ObjectName
    Range: bytes=0-1000
    Host: bucket.oss-cn-hangzhou.aliyuncs.com
    Date: Fri, 18 Oct 2019 03:00:02 GMT
    Authorization: Signature
    
    200 (OK)
    content-length: 1000
    etag: "CACF99600561A31D494569C979E6FB81"
    x-oss-request-id: 5DA92AB204321E36347F3E7D
    date: Fri, 18 Oct 2019 03:00:02 GMT
    [1000 bytes of object data]
  • If the specified range exceeds the valid range, the Range does not take effect. The response returns a value of 200 and transmits the entire object content.
    GET /ObjectName
    Range: bytes=1000-2000
    Host: bucket.oss-cn-hangzhou.aliyuncs.com
    Date: Fri, 18 Oct 2019 02:56:24 GMT
    Authorization: Sigature
    
    200 (OK)
    content-length: 1000
    etag: "CACF99600561A31D494569C979E6FB81"
    x-oss-request-id: 5DA929D9CCCC823835CBE134
    date: Fri, 18 Oct 2019 02:56:25 GMT
    [1000 bytes of object data]

Compatible behavior request example

  • Add the x-oss-range-behavior:standard request header. The last byte exceeds the valid interval and returns the content in the 500-999 byte range.
    GET /ObjectName
    x-oss-range-behavior: standard
    Range: bytes=500-2000
    Host: bucket.oss-cn-hangzhou.aliyuncs.com
    Date: Fri, 18 Oct 2019 07:02:23 GMT
    Authorization: Signature
    
    206 (Partial Content)
    content-length: 500
    content-range: bytes 500-999/1000
    etag: "CACF99600561A31D494569C979E6FB81"
    x-oss-request-id: 5DA9637FB3B1C73234CC59EB
    date: Fri, 18 Oct 2019 07:02:23 GMT
    [500 bytes of object data]
  • Add the x-oss-range-behavior:standard request header. If the first byte exceeds the valid interval, a 416 error is returned.
    GET /ObjectName
    x-oss-range-behavior: standard
    Range: bytes=1000-2000
    Host: bucket.oss-cn-hangzhou.aliyuncs.com
    Date: Fri, 18 Oct 2019 07:04:23 GMT
    Authorization: Signature
    
    416 (Requested Range Not Satisfiable)
    content-length: 345
    x-oss-request-id: 5DA963F7CEBFAA3931BF91F5
    date: Fri, 18 Oct 2019 07:04:23 GMT
    content-type: application/xml
    <?xml version="1.0" encoding="UTF-8"?>
    <Error>
      <Code>InvalidRange</Code>
      <Message>The requested range cannot be satisfied</Message>
      <RequestId>5DA963F7CEBFAA3931BF91F5</RequestId>
      <HostId>bucket.oss-cn-hangzhou.aliyuncs.com</HostId>
      <ActualObjectSize>1000</ActualObjectSize>
      <RangeRequested>bytes=1000-2000</RangeRequested>
    </Error>
  • Add a x-oss-range-behavior:standard request header. If the specified range exceeds the valid range, 0-999 bytes are returned, that is, the complete file content.
    GET /ObjectName
    x-oss-range-behavior: standard
    Range: bytes=-2000
    Host: bucket.oss-cn-hangzhou.aliyuncs.com
    Date: Fri, 18 Oct 2019 07:06:39 GMT
    Authorization: Signature
    
    206 (Partial Content)
    content-length: 1000
    content-range: bytes 0-999/1000
    etag: "CACF99600561A31D494569C979E6FB81"
    x-oss-request-id: 5DA9647FC4334F3534AF9A83
    date: Fri, 18 Oct 2019 07:06:39 GMT
    [1000 bytes of object data]

Applicable scope

  • OSS